forked from telia-oss/terraform-aws-static-site
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
198 lines (163 loc) · 4.95 KB
/
main.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# ------------------------------------------------------------------------------
# Resources
# ------------------------------------------------------------------------------
provider "aws" {
region = "us-east-1"
alias = "virginia"
}
data "aws_caller_identity" "current" {}
resource "aws_acm_certificate" "cert_website" {
domain_name = var.site_name
validation_method = "DNS"
provider = aws.virginia
tags = var.tags
lifecycle {
create_before_destroy = true
}
}
data "aws_route53_zone" "main" {
name = var.hosted_zone_name
}
resource "aws_route53_record" "cert_website_validation" {
for_each = {
for dvo in aws_acm_certificate.cert_website.domain_validation_options : dvo.domain_name => {
name = dvo.resource_record_name
record = dvo.resource_record_value
type = dvo.resource_record_type
}
}
allow_overwrite = true
name = each.value.name
records = [each.value.record]
ttl = 60
type = each.value.type
zone_id = data.aws_route53_zone.main.id
}
resource "aws_acm_certificate_validation" "main" {
certificate_arn = aws_acm_certificate.cert_website.arn
validation_record_fqdns = [for record in aws_route53_record.cert_website_validation : record.fqdn]
provider = aws.virginia
}
resource "aws_s3_bucket" "website_bucket" {
bucket = var.bucket_name == null ? "${data.aws_caller_identity.current.account_id}-${var.name_prefix}-static-website-bucket" : var.bucket_name
}
resource "aws_s3_bucket_versioning" "website_bucket_versioning" {
bucket = aws_s3_bucket.website_bucket.id
versioning_configuration {
status = var.bucket_versioning
}
}
resource "aws_s3_bucket_website_configuration" "website_bucket_configuration" {
bucket = aws_s3_bucket.website_bucket.id
index_document {
suffix = "index.html"
}
error_document {
key = "index.html"
}
}
resource "aws_s3_bucket_acl" "website_bucket_acl" {
bucket = aws_s3_bucket.website_bucket.id
acl = "private"
}
resource "aws_s3_bucket_server_side_encryption_configuration" "website_bucket_sse" {
bucket = aws_s3_bucket.website_bucket.id
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
resource "aws_s3_bucket_policy" "website_bucket_policy" {
bucket = aws_s3_bucket.website_bucket.id
policy = data.aws_iam_policy_document.s3_policy.json
}
resource "aws_cloudfront_origin_access_identity" "origin_access_identity" {
comment = "origin access identity for s3/cloudfront"
}
resource "aws_cloudfront_distribution" "s3_distribution" {
depends_on = [
aws_s3_bucket.website_bucket,
aws_acm_certificate_validation.main,
]
origin {
domain_name = aws_s3_bucket.website_bucket.bucket_regional_domain_name
origin_id = aws_cloudfront_origin_access_identity.origin_access_identity.id
s3_origin_config {
origin_access_identity = aws_cloudfront_origin_access_identity.origin_access_identity.cloudfront_access_identity_path
}
}
enabled = true
is_ipv6_enabled = true
default_root_object = "index.html"
aliases = [aws_acm_certificate.cert_website.domain_name]
custom_error_response {
error_code = 404
response_code = 200
response_page_path = "/index.html"
}
default_cache_behavior {
allowed_methods = [
"DELETE",
"GET",
"HEAD",
"OPTIONS",
"PATCH",
"POST",
"PUT",
]
cached_methods = [
"GET",
"HEAD",
]
target_origin_id = aws_cloudfront_origin_access_identity.origin_access_identity.id
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
viewer_protocol_policy = "redirect-to-https"
min_ttl = 0
default_ttl = 0
max_ttl = 0
}
price_class = "PriceClass_200"
viewer_certificate {
acm_certificate_arn = aws_acm_certificate.cert_website.arn
ssl_support_method = "sni-only"
}
restrictions {
geo_restriction {
restriction_type = "none"
}
}
}
resource "aws_route53_record" "www_a" {
name = "${var.site_name}."
type = "A"
zone_id = data.aws_route53_zone.main.id
alias {
name = aws_cloudfront_distribution.s3_distribution.domain_name
zone_id = aws_cloudfront_distribution.s3_distribution.hosted_zone_id
evaluate_target_health = false
}
}
data "aws_iam_policy_document" "s3_policy" {
statement {
actions = ["s3:GetObject"]
resources = ["${aws_s3_bucket.website_bucket.arn}/*"]
principals {
type = "AWS"
identifiers = [aws_cloudfront_origin_access_identity.origin_access_identity.iam_arn]
}
}
statement {
actions = ["s3:ListBucket"]
resources = [aws_s3_bucket.website_bucket.arn]
principals {
type = "AWS"
identifiers = [aws_cloudfront_origin_access_identity.origin_access_identity.iam_arn]
}
}
}