-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
183 lines (152 loc) · 4.67 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
resource "aws_cloudfront_function" "rewrite_sub_folder_index" {
name = "${var.app_name}-rewrite-sub-folder-index"
runtime = "cloudfront-js-1.0"
comment = "Rewrites each request to the subfolder index.html"
publish = true
code = templatefile("rewrite_subfolder.js.tftpl",
{
applications = jsonencode(keys(var.applications))
}
)
}
module "ssm" {
source = "github.com/Flaconi/terraform-aws-ssm-store?ref=v1.1.0"
for_each = var.applications
tags = merge(each.value.tags, { Project = each.key })
kms_alias = "alias/aws/ssm"
name_prefix = "${var.ssm_name_prefix}/${each.key}/"
parameters = concat([
{
name = "cloudfront_domain_name"
value = module.cdn.cloudfront_distribution_domain_name
},
{
name = "bucket_name"
value = module.s3.s3_bucket_id
},
{
name = "application_id"
value = each.value.application_id
},
], each.value.additional_ssm_parameters)
}
module "cdn" {
create_distribution = length(var.applications) > 0
source = "github.com/terraform-aws-modules/terraform-aws-cloudfront?ref=v3.4.0"
enabled = true
is_ipv6_enabled = true
default_root_object = "index.html"
price_class = "PriceClass_100"
tags = var.tags
create_origin_access_control = true
origin_access_control = {
"${var.app_name}-origin-access-control" = {
description = "Origin access control for s3 bucket ${module.s3.s3_bucket_id}"
origin_type = "s3"
signing_behavior = "always"
signing_protocol = "sigv4"
}
}
origin = {
ctCustomerAppBucket = {
domain_name = module.s3.s3_bucket_bucket_regional_domain_name
origin_access_control = "${var.app_name}-origin-access-control"
}
}
logging_config = {
bucket = var.cdn_logging_bucket_name
prefix = var.app_name
}
geo_restriction = {
restriction_type = "none"
locations = []
}
ordered_cache_behavior = [
{
path_pattern = "/*.js"
target_origin_id = "ctCustomerAppBucket"
viewer_protocol_policy = "redirect-to-https"
allowed_methods = ["GET", "HEAD", "OPTIONS"]
cached_methods = ["GET", "HEAD"]
use_forwarded_values = false
compress = true
cache_policy_name = "Managed-CachingOptimized"
},
{
path_pattern = "/*.css"
target_origin_id = "ctCustomerAppBucket"
viewer_protocol_policy = "redirect-to-https"
allowed_methods = ["GET", "HEAD", "OPTIONS"]
cached_methods = ["GET", "HEAD"]
use_forwarded_values = false
compress = true
cache_policy_name = "Managed-CachingOptimized"
},
]
default_cache_behavior = {
allowed_methods = ["GET", "HEAD", "OPTIONS"]
cached_methods = ["GET", "HEAD"]
target_origin_id = "ctCustomerAppBucket"
cache_policy_name = "Managed-CachingDisabled"
query_string_cache_keys = []
use_forwarded_values = false
compress = true
viewer_protocol_policy = "redirect-to-https"
function_association = {
viewer-request = {
function_arn = aws_cloudfront_function.rewrite_sub_folder_index.arn
}
}
}
custom_error_response = [
{
error_caching_min_ttl = 300
error_code = 404
response_code = 200
response_page_path = "/index.html"
},
{
error_caching_min_ttl = 300
error_code = 403
response_code = 200
response_page_path = "/index.html"
}]
viewer_certificate = {
cloudfront_default_certificate = true
minimum_protocol_version = "TLSv1"
}
}
module "s3" {
source = "github.com/terraform-aws-modules/terraform-aws-s3-bucket?ref=v4.1.2"
create_bucket = length(var.applications) > 0
bucket = var.bucket_name
tags = var.tags
acl = "private"
attach_public_policy = false
control_object_ownership = true
object_ownership = "ObjectWriter"
}
resource "aws_s3_bucket_policy" "cloudfront" {
bucket = module.s3.s3_bucket_id
policy = data.aws_iam_policy_document.cloudfront.json
}
data "aws_iam_policy_document" "cloudfront" {
statement {
sid = "AllowCloudFrontServicePrincipalReadOnly"
principals {
type = "Service"
identifiers = ["cloudfront.amazonaws.com"]
}
actions = [
"s3:GetObject",
]
resources = [
"${module.s3.s3_bucket_arn}/*",
]
condition {
test = "StringEquals"
values = ["arn:aws:cloudfront::${var.aws_account_id}:distribution/${module.cdn.cloudfront_distribution_id}"]
variable = "AWS:SourceArn"
}
}
}