-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.tf
169 lines (142 loc) · 4.26 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
provider "template" {
version = "~> 2.1.0"
}
data "template_file" "bucket_name" {
count = length(var.brands)
template = "$${name}"
vars = {
name = substr(
format(
"%s-%s-%s-%s",
var.project,
var.brands[count.index],
var.region,
var.environment,
),
0,
length(
format(
"%s-%s-%s-%s",
var.project,
var.brands[count.index],
var.region,
var.environment,
),
) > 45 ? 45 : -1,
)
}
}
data "aws_iam_policy_document" "s3_policy" {
count = length(var.brands)
statement {
actions = ["s3:GetObject"]
resources = ["arn:aws:s3:::tierra-${element(data.template_file.bucket_name.*.rendered, count.index)}-cloudfront/*"]
principals {
type = "AWS"
identifiers = ["*"]
}
}
}
resource "aws_s3_bucket" "bucket_app" {
count = length(var.brands)
bucket = "tierra-${element(data.template_file.bucket_name.*.rendered, count.index)}-cloudfront"
policy = element(data.aws_iam_policy_document.s3_policy.*.json, count.index)
website {
index_document = "index.html"
}
cors_rule {
allowed_headers = ["*"]
allowed_methods = ["GET"]
allowed_origins = ["http://*", "https://*"]
}
force_destroy = true
tags = {
Name = "tierra-${var.project}-${element(var.brands, count.index)}-${var.region}-${var.environment}-cloudfront"
Project = var.project
Environment = var.environment
Brand = element(var.brands, count.index)
}
}
resource "aws_cloudfront_distribution" "s3_distribution" {
count = length(var.brands)
origin {
domain_name = "tierra-${element(data.template_file.bucket_name.*.rendered, count.index)}-cloudfront.s3-website-${var.region}.amazonaws.com"
origin_id = "${element(data.template_file.bucket_name.*.rendered, count.index)}-origin"
custom_origin_config {
http_port = 80
https_port = 443
origin_protocol_policy = "http-only"
origin_ssl_protocols = ["TLSv1.2"]
}
}
enabled = true
comment = "Cloud Front for ${var.project} [Brand: ${element(var.brands, count.index)}] (${var.environment})"
aliases = split(
",",
element(var.list_public_register_alias_domain, count.index),
)
default_cache_behavior {
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
cached_methods = ["GET", "HEAD"]
target_origin_id = "${var.project}-${element(var.brands, count.index)}-${var.region}-${var.environment}-origin"
compress = var.cache_compress
forwarded_values {
query_string = false
headers = ["Origin"]
cookies {
forward = "none"
}
}
viewer_protocol_policy = var.viewer_protocol_policy
min_ttl = var.min_ttl
default_ttl = var.default_ttl
max_ttl = var.max_ttl
}
default_root_object = "${var.default_root_path}index.html"
price_class = "PriceClass_200"
restrictions {
geo_restriction {
restriction_type = "none"
}
}
viewer_certificate {
iam_certificate_id = var.ssl_cert_id
acm_certificate_arn = var.ssl_cert_arn
ssl_support_method = "sni-only"
minimum_protocol_version = "TLSv1"
}
custom_error_response {
error_caching_min_ttl = "0"
error_code = "400"
response_code = "200"
response_page_path = "/index.html"
}
custom_error_response {
error_caching_min_ttl = "0"
error_code = "404"
response_code = "200"
response_page_path = "/index.html"
}
custom_error_response {
error_caching_min_ttl = "0"
error_code = "403"
response_code = "200"
response_page_path = "/index.html"
}
tags = {
Project = var.project
Environment = var.environment
Brand = element(var.brands, count.index)
}
}
resource "aws_route53_record" "cdn-cname" {
count = length(var.brands)
zone_id = var.route53_zone_id
name = "${var.project}-${var.environment}-${element(var.brands, count.index)}.${var.alias_domain_suffix}"
type = "CNAME"
ttl = "300"
records = [element(
aws_cloudfront_distribution.s3_distribution.*.domain_name,
count.index,
)]
}