-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
191 lines (170 loc) · 5.53 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
# Bucket
resource "aws_s3_bucket" "bucket" {
bucket_prefix = null
force_destroy = true
object_lock_enabled = false
bucket = var.bucket_name
tags = {
Application = var.application
}
}
resource "aws_s3_bucket_policy" "bucket_policy" {
bucket = aws_s3_bucket.bucket.id
policy = jsonencode({
Version = "2008-10-17",
Id = "PolicyForCloudFrontPrivateContent",
Statement = [
{
Sid = "AllowCloudFrontServicePrincipal",
Effect = "Allow",
Principal = {
Service = "cloudfront.amazonaws.com"
},
Action = "s3:GetObject",
Resource = "arn:aws:s3:::${aws_s3_bucket.bucket.id}/*",
Condition = {
StringEquals = {
"AWS:SourceArn" = aws_cloudfront_distribution.distribution.arn
}
}
}
]
})
}
# Cloudfront
resource "aws_cloudfront_distribution" "distribution" {
aliases = var.domains
comment = var.application
continuous_deployment_policy_id = null
default_root_object = "index.html"
enabled = true
http_version = "http2"
is_ipv6_enabled = true
price_class = "PriceClass_All"
retain_on_delete = false
staging = false
tags = {
Application = var.application
}
wait_for_deployment = true
web_acl_id = null
default_cache_behavior {
allowed_methods = ["GET", "HEAD"]
cache_policy_id = data.aws_cloudfront_cache_policy.disabled.id
cached_methods = ["GET", "HEAD"]
compress = false
default_ttl = 0
field_level_encryption_id = null
max_ttl = 0
min_ttl = 0
origin_request_policy_id = null
realtime_log_config_arn = null
response_headers_policy_id = null
smooth_streaming = false
# Note that target_origin_id must match origin.origin_id
target_origin_id = var.application
trusted_key_groups = []
trusted_signers = []
viewer_protocol_policy = "redirect-to-https"
function_association {
event_type = "viewer-request"
function_arn = aws_cloudfront_function.function.arn
}
}
origin {
connection_attempts = 3
connection_timeout = 10
domain_name = aws_s3_bucket.bucket.bucket_regional_domain_name
origin_access_control_id = aws_cloudfront_origin_access_control.access_control.id
# Note that origin_id is a label
origin_id = var.application
origin_path = null
}
restrictions {
geo_restriction {
locations = []
restriction_type = "none"
}
}
viewer_certificate {
acm_certificate_arn = data.aws_acm_certificate.certificate.arn
cloudfront_default_certificate = false
iam_certificate_id = null
minimum_protocol_version = "TLSv1.2_2021"
ssl_support_method = "sni-only"
}
}
resource "aws_cloudfront_origin_access_control" "access_control" {
description = var.application
name = aws_s3_bucket.bucket.bucket_regional_domain_name
origin_access_control_origin_type = "s3"
signing_behavior = "always"
signing_protocol = "sigv4"
}
resource "aws_cloudfront_function" "function" {
code = templatefile(
"${path.module}/function.tftpl",
{
redirectable_domains = module.records.redirects
domain_name = var.primary_domain
}
)
comment = "Directory serving index.html and subdomain redirection function for ${var.application}"
name = var.cloudfront_function_name
publish = true
runtime = "cloudfront-js-2.0"
}
# Route53
resource "aws_route53_record" "records" {
for_each = {
for zone_record in module.records.zones :
"${zone_record.zone}_${zone_record.record}_${zone_record.record_type}" => zone_record
if zone_record.record_type != "A"
}
allow_overwrite = null
health_check_id = null
name = each.value.record
records = [aws_cloudfront_distribution.distribution.domain_name]
set_identifier = null
ttl = local.ttl.short
type = each.value.record_type
zone_id = data.aws_route53_zone.zones[each.value.zone].id
}
resource "aws_route53_record" "aliases" {
for_each = {
for zone_record in module.records.zones : "${zone_record.zone}_${zone_record.record}_${zone_record.record_type}" => zone_record
if zone_record.record_type == "A"
}
allow_overwrite = null
health_check_id = null
name = each.value.record
set_identifier = null
type = "A"
zone_id = data.aws_route53_zone.zones[each.value.zone].id
alias {
evaluate_target_health = true
name = aws_cloudfront_distribution.distribution.domain_name
zone_id = aws_cloudfront_distribution.distribution.hosted_zone_id
}
}
# Data
data "aws_caller_identity" "account" {}
data "aws_acm_certificate" "certificate" {
domain = var.certificate_name
}
data "aws_route53_zone" "zones" {
for_each = module.records.mapped_zones
name = each.key
}
data "aws_cloudfront_cache_policy" "disabled" {
name = "Managed-CachingDisabled"
}
data "aws_cloudfront_cache_policy" "optimized" {
name = "Managed-CachingOptimized"
}
# Records
module "records" {
source = "./modules/records"
primary = var.primary_domain
domains = var.domains
}