This repository was archived by the owner on Jan 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
169 lines (150 loc) · 4.54 KB
/
Copy pathmain.tf
File metadata and controls
169 lines (150 loc) · 4.54 KB
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
locals {
aws_hosted_cloudfront_zone_id = "Z2FDTNDATAQYW2"
bucket_name = var.alias_name == "" ? var.zone_name : "${var.alias_name}.${var.zone_name}"
website_domain = "s3-website.${var.aws_region}.amazonaws.com"
website_endpoint = "${local.bucket_name}.${local.website_domain}"
distribution_origin_id = "S3-Website-${local.website_endpoint}"
index_html_path = "${path.module}/src/index.html"
}
resource "aws_s3_bucket" "bucket_index" {
count = var.redirection_url == "" ? 1 : 0
bucket = local.bucket_name
hosted_zone_id = var.aws_s3_bucket_hosted_zone_id
request_payer = "BucketOwner"
tags = {}
website_domain = local.website_domain
website_endpoint = local.website_endpoint
versioning {
enabled = false
mfa_delete = false
}
website {
index_document = "index.html"
}
}
resource "aws_s3_bucket_object" "index" {
count = var.redirection_url == "" && var.create_index ? 1 : 0
bucket = aws_s3_bucket.bucket_index[0].bucket
key = aws_s3_bucket.bucket_index[0].website[0].index_document
source = local.index_html_path
content_type = "text/html"
metadata = {
"version-id" = "null"
}
}
resource "aws_s3_bucket" "bucket_redirect" {
count = var.redirection_url == "" ? 0 : 1
bucket = local.bucket_name
hosted_zone_id = var.aws_s3_bucket_hosted_zone_id
request_payer = "BucketOwner"
tags = {}
website_domain = local.website_domain
website_endpoint = local.website_endpoint
versioning {
enabled = false
mfa_delete = false
}
website {
redirect_all_requests_to = var.redirection_url
}
}
resource "aws_s3_bucket_policy" "bucket_index_policy" {
count = var.enable_public_access ? 1 : 0
bucket = aws_s3_bucket.bucket_index[0].bucket
policy = jsonencode(
{
Statement = [
{
Action = "s3:GetObject"
Effect = "Allow"
Principal = "*"
Resource = "arn:aws:s3:::${aws_s3_bucket.bucket_index[0].bucket}/*"
Sid = "PublicReadGetObject"
},
]
Version = "2012-10-17"
}
)
}
resource "aws_cloudfront_distribution" "distribution" {
aliases = concat([
var.redirection_url == "" ? aws_s3_bucket.bucket_index[0].bucket : aws_s3_bucket.bucket_redirect[0].bucket
], var.alternative_aliases)
enabled = true
http_version = "http2"
is_ipv6_enabled = true
price_class = "PriceClass_All"
retain_on_delete = false
tags = {}
wait_for_deployment = false
default_cache_behavior {
allowed_methods = [
"HEAD",
"DELETE",
"POST",
"GET",
"OPTIONS",
"PUT",
"PATCH",
]
cached_methods = [
"GET",
"HEAD",
]
compress = false
default_ttl = var.cloudfront_default_ttl
max_ttl = var.cloudfront_max_ttl
min_ttl = 0
smooth_streaming = false
target_origin_id = local.distribution_origin_id
trusted_signers = []
viewer_protocol_policy = var.viewer_protocol_policy
forwarded_values {
headers = []
query_string = false
query_string_cache_keys = []
cookies {
forward = "none"
whitelisted_names = []
}
}
}
origin {
domain_name = local.website_endpoint
origin_id = local.distribution_origin_id
custom_origin_config {
http_port = 80
https_port = 443
origin_keepalive_timeout = 5
origin_protocol_policy = "http-only"
origin_read_timeout = 30
origin_ssl_protocols = [
"TLSv1",
"TLSv1.1",
"TLSv1.2",
]
}
}
restrictions {
geo_restriction {
locations = []
restriction_type = "none"
}
}
viewer_certificate {
acm_certificate_arn = var.aws_cert_arn
cloudfront_default_certificate = false
minimum_protocol_version = "TLSv1.1_2016"
ssl_support_method = "sni-only"
}
}
resource "aws_route53_record" "record" {
name = var.redirection_url == "" ? aws_s3_bucket.bucket_index[0].bucket : aws_s3_bucket.bucket_redirect[0].bucket
type = "A"
zone_id = var.aws_zone_id
alias {
evaluate_target_health = false
name = aws_cloudfront_distribution.distribution.domain_name
zone_id = local.aws_hosted_cloudfront_zone_id
}
}