-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
209 lines (169 loc) · 5.06 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
198
199
200
201
202
203
204
205
206
207
208
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}
locals {
s3_origin_id = "myS3Origin"
all_domains = concat([var.domain.domain], [
for v in var.domain_aliases : v.domain
])
unique_domains = distinct(local.all_domains)
all_zones = concat([var.domain.zone], [
for v in var.domain_aliases : v.zone
])
distinct_zones = distinct(local.all_zones)
zone_name_to_id_map = zipmap(local.distinct_zones, data.aws_route53_zone.domain[*].zone_id)
domain_to_zone_map = zipmap(local.all_domains, local.all_zones)
}
data "aws_route53_zone" "domain" {
count = length(local.distinct_zones)
name = local.distinct_zones[count.index]
private_zone = false
}
data "aws_cloudfront_cache_policy" "managed_disabled" {
name = "Managed-CachingDisabled"
}
# Our S3 Bucket. Hosts our static app
resource "aws_s3_bucket" "bucket" {
bucket = var.bucket
tags = var.tags
}
resource "aws_s3_bucket_website_configuration" "bucket" {
bucket = aws_s3_bucket.bucket.bucket
index_document {
suffix = "index.html"
}
error_document {
key = "index.html"
}
}
# ACL Rules for Cloudfront Bucket
resource "aws_s3_bucket_acl" "cluster" {
bucket = aws_s3_bucket.bucket.bucket
acl = "private"
}
# Configure Public Access Block for our Bucket - Block all public access by default
resource "aws_s3_bucket_public_access_block" "bucket" {
bucket = aws_s3_bucket.bucket.id
block_public_acls = true
block_public_policy = true
restrict_public_buckets = true
ignore_public_acls = true
}
# IAM - Origin Access Identity (OAI)
resource "aws_cloudfront_origin_access_identity" "oai" {
comment = "Cloudfront Origin Access Identity"
}
# Define OAI Access Policy
data "aws_iam_policy_document" "s3_policy" {
statement {
actions = ["s3:GetObject"]
resources = ["${aws_s3_bucket.bucket.arn}/*"]
principals {
type = "AWS"
identifiers = [aws_cloudfront_origin_access_identity.oai.iam_arn]
}
}
}
# Attach OAI to S3 Bucket
resource "aws_s3_bucket_policy" "s3_oai" {
bucket = aws_s3_bucket.bucket.id
policy = data.aws_iam_policy_document.s3_policy.json
}
# DNS records
resource "aws_route53_record" "dns" {
count = length(local.unique_domains)
zone_id = lookup(local.zone_name_to_id_map, lookup(local.domain_to_zone_map, local.unique_domains[count.index]))
name = local.unique_domains[count.index]
type = "A"
allow_overwrite = true
alias {
name = aws_cloudfront_distribution.distribution.domain_name
zone_id = aws_cloudfront_distribution.distribution.hosted_zone_id
evaluate_target_health = true
}
}
# The cloudfront distribution
resource "aws_cloudfront_distribution" "distribution" {
origin {
domain_name = aws_s3_bucket.bucket.bucket_regional_domain_name
origin_id = local.s3_origin_id
s3_origin_config {
origin_access_identity = aws_cloudfront_origin_access_identity.oai.cloudfront_access_identity_path
}
}
enabled = true
is_ipv6_enabled = true
default_root_object = "index.html"
aliases = local.unique_domains
default_cache_behavior {
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
cached_methods = ["GET", "HEAD"]
target_origin_id = local.s3_origin_id
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
viewer_protocol_policy = "redirect-to-https"
min_ttl = 0
default_ttl = 3600
max_ttl = 86400
}
# Index cache
ordered_cache_behavior {
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
cached_methods = ["GET", "HEAD"]
target_origin_id = local.s3_origin_id
min_ttl = 0
default_ttl = 0
max_ttl = 0
path_pattern = "/index.html"
viewer_protocol_policy = "redirect-to-https"
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
}
# Disable Cache for Paths
dynamic "ordered_cache_behavior" {
for_each = var.flag_disable_cache_path_pattern ? [1] : []
content {
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
cached_methods = ["GET", "HEAD"]
target_origin_id = local.s3_origin_id
cache_policy_id = data.aws_cloudfront_cache_policy.managed_disabled.id
path_pattern = var.disable_cache_path_pattern
viewer_protocol_policy = "redirect-to-https"
}
}
restrictions {
geo_restriction {
restriction_type = "none"
}
}
tags = var.tags
viewer_certificate {
# cloudfront_default_certificate = true
acm_certificate_arn = var.acm_arn
ssl_support_method = "sni-only"
minimum_protocol_version = "TLSv1.2_2021"
}
# Angular - Redirect 40x errors to index.html
custom_error_response {
error_code = 403
response_code = 200
response_page_path = "/index.html"
}
custom_error_response {
error_code = 404
response_code = 200
response_page_path = "/index.html"
}
}