1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117locals {
name = "${var.project_name}-${var.environment}-web"
}
# Bucket is private; CloudFront reaches it via Origin Access Control. Content
# is deployed by the existing `npm run build:prod`/`build:staging` scripts
# (vite build + `aws s3 sync`) — Terraform only provisions the bucket and CDN.
resource "aws_s3_bucket" "this" {
bucket = local.name
}
resource "aws_s3_bucket_public_access_block" "this" {
bucket = aws_s3_bucket.this.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
resource "aws_s3_bucket_server_side_encryption_configuration" "this" {
bucket = aws_s3_bucket.this.id
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
resource "aws_cloudfront_origin_access_control" "this" {
name = local.name
origin_access_control_origin_type = "s3"
signing_behavior = "always"
signing_protocol = "sigv4"
}
resource "aws_cloudfront_distribution" "this" {
enabled = true
default_root_object = "index.html"
price_class = var.price_class
aliases = var.domain_name != null ? [var.domain_name] : []
origin {
domain_name = aws_s3_bucket.this.bucket_regional_domain_name
origin_id = "s3-${local.name}"
origin_access_control_id = aws_cloudfront_origin_access_control.this.id
}
default_cache_behavior {
allowed_methods = ["GET", "HEAD", "OPTIONS"]
cached_methods = ["GET", "HEAD"]
target_origin_id = "s3-${local.name}"
viewer_protocol_policy = "redirect-to-https"
compress = true
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
}
# SPA client-side routing: unknown paths fall back to index.html instead
# of surfacing S3's raw 403 for a missing key.
custom_error_response {
error_code = 403
response_code = 200
response_page_path = "/index.html"
}
custom_error_response {
error_code = 404
response_code = 200
response_page_path = "/index.html"
}
restrictions {
geo_restriction {
restriction_type = "none"
}
}
viewer_certificate {
cloudfront_default_certificate = var.domain_name == null
acm_certificate_arn = var.domain_name != null ? var.acm_certificate_arn : null
ssl_support_method = var.domain_name != null ? "sni-only" : null
minimum_protocol_version = var.domain_name != null ? "TLSv1.2_2021" : null
}
}
data "aws_iam_policy_document" "bucket_policy" {
statement {
sid = "AllowCloudFrontOAC"
effect = "Allow"
actions = ["s3:GetObject"]
resources = ["${aws_s3_bucket.this.arn}/*"]
principals {
type = "Service"
identifiers = ["cloudfront.amazonaws.com"]
}
condition {
test = "StringEquals"
variable = "AWS:SourceArn"
values = [aws_cloudfront_distribution.this.arn]
}
}
}
resource "aws_s3_bucket_policy" "this" {
bucket = aws_s3_bucket.this.id
policy = data.aws_iam_policy_document.bucket_policy.json
}