/
root.tf
168 lines (132 loc) · 3.92 KB
/
root.tf
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# Creates bucket to store the static website
resource "aws_s3_bucket" "website_root" {
bucket = "${var.website_domain}-root"
# Remove this line if you want to prevent accidential deletion of bucket
force_destroy = true
website {
index_document = "index.html"
error_document = "404.html"
}
tags = {
ManagedBy = "terraform"
Changed = formatdate("YYYY-MM-DD hh:mm ZZZ", timestamp())
}
lifecycle {
ignore_changes = [tags]
}
}
resource "aws_s3_bucket_public_access_block" "website_root" {
bucket = aws_s3_bucket.website_root.id
block_public_acls = false
block_public_policy = false
ignore_public_acls = false
restrict_public_buckets = false
}
resource "aws_s3_bucket_ownership_controls" "website_root" {
bucket = aws_s3_bucket.website_root.id
rule {
object_ownership = "BucketOwnerPreferred"
}
}
resource "aws_s3_bucket_acl" "website_root" {
depends_on = [
aws_s3_bucket_public_access_block.website_root,
aws_s3_bucket_ownership_controls.website_root,
]
bucket = aws_s3_bucket.website_root.id
acl = "public-read"
}
resource "aws_s3_bucket_policy" "website_root" {
bucket = aws_s3_bucket.website_root.id
depends_on = [
aws_s3_bucket_public_access_block.website_root,
aws_s3_bucket_ownership_controls.website_root,
]
policy = data.aws_iam_policy_document.website_root.json
}
data "aws_iam_policy_document" "website_root" {
statement {
principals {
type = "AWS"
identifiers = ["*"]
}
actions = [
"s3:GetObject",
]
resources = [
"arn:aws:s3:::${var.website_domain}-root/*"
]
}
}
# Creates the CloudFront distribution to serve the static website
resource "aws_cloudfront_distribution" "website_cdn_root" {
enabled = true
price_class = "PriceClass_All" # Select the correct PriceClass depending on who the CDN is supposed to serve (https://docs.aws.amazon.com/AmazonCloudFront/ladev/DeveloperGuide/PriceClass.html)
aliases = [var.website_domain]
provider = aws.us-east-1
depends_on = [
]
origin {
domain_name = aws_s3_bucket.website_root.website_endpoint
origin_id = "origin-bucket-${aws_s3_bucket.website_root.id}"
custom_origin_config {
http_port = 80
https_port = 443
origin_protocol_policy = "http-only"
origin_ssl_protocols = ["TLSv1.2"]
}
}
default_root_object = "index.html"
default_cache_behavior {
allowed_methods = ["GET", "HEAD", "OPTIONS"]
cached_methods = ["GET", "HEAD"]
target_origin_id = "origin-bucket-${aws_s3_bucket.website_root.id}"
min_ttl = "0"
default_ttl = tostring(var.default_cache_duration)
max_ttl = "1200"
viewer_protocol_policy = "redirect-to-https" # Redirects any HTTP request to HTTPS
compress = true
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
}
restrictions {
geo_restriction {
restriction_type = "none"
}
}
viewer_certificate {
acm_certificate_arn = data.aws_acm_certificate.wildcard_website.arn
ssl_support_method = "sni-only"
}
custom_error_response {
error_caching_min_ttl = 60
error_code = 404
response_page_path = "/404.html"
response_code = 404
}
tags = {
ManagedBy = "terraform"
Changed = formatdate("YYYY-MM-DD hh:mm ZZZ", timestamp())
}
lifecycle {
ignore_changes = [
tags,
viewer_certificate,
]
}
}
# Creates the DNS record to point on the main CloudFront distribution ID
resource "aws_route53_record" "website_cdn_root_record" {
zone_id = data.aws_route53_zone.main.zone_id
name = var.website_domain
type = "A"
alias {
name = aws_cloudfront_distribution.website_cdn_root.domain_name
zone_id = aws_cloudfront_distribution.website_cdn_root.hosted_zone_id
evaluate_target_health = false
}
}