-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
154 lines (124 loc) · 3.54 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
locals {
custom_subdomain = var.custom_subdomain == "" ? var.domain_name : "${var.custom_subdomain}.${var.domain_name}"
aliases = var.aliases != [] ? toset([for alias in toset(var.aliases): "${alias}"]) : []
}
#
# S3 Website
#
resource "aws_s3_bucket" "website" {
bucket = var.bucket_name
tags = var.tags
}
resource "aws_s3_bucket_public_access_block" "website" {
bucket = aws_s3_bucket.website.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
resource "aws_s3_bucket_website_configuration" "website" {
bucket = aws_s3_bucket.website.bucket
index_document {
suffix = var.index_document
}
error_document {
key = var.error_document
}
}
#
# CloudFront Distribution
#
resource "aws_cloudfront_origin_access_identity" "default" {
comment = "Origin Access Identity for ${var.website_name} S3 website"
}
resource "aws_cloudfront_distribution" "default" {
origin {
domain_name = aws_s3_bucket.website.bucket_regional_domain_name
origin_id = aws_s3_bucket.website.bucket
s3_origin_config {
origin_access_identity = aws_cloudfront_origin_access_identity.default.cloudfront_access_identity_path
}
}
aliases = var.aliases == [] ? [ local.custom_subdomain ] : concat(tolist(local.aliases), [ local.custom_subdomain ])
default_cache_behavior {
allowed_methods = var.allowed_methods
cached_methods = var.cached_methods
target_origin_id = aws_s3_bucket.website.bucket
forwarded_values {
query_string = true
cookies {
forward = "all"
}
}
viewer_protocol_policy = "redirect-to-https"
min_ttl = "0"
default_ttl = "3600"
max_ttl = "86400"
}
restrictions {
geo_restriction {
restriction_type = "none"
}
}
price_class = "PriceClass_All"
enabled = true
default_root_object = "index.html"
viewer_certificate {
acm_certificate_arn = var.certificate_arn
ssl_support_method = "sni-only"
minimum_protocol_version = var.minimum_protocol_version
}
custom_error_response {
error_caching_min_ttl = 10
error_code = 403
response_code = 200
response_page_path = "/index.html"
}
custom_error_response {
error_caching_min_ttl = 10
error_code = 404
response_code = 200
response_page_path = "/index.html"
}
}
#
# Bucket policy for Cloudfront OIA
#
data "aws_iam_policy_document" "s3_policy" {
statement {
actions = ["s3:GetObject"]
resources = ["${aws_s3_bucket.website.arn}/*"]
principals {
type = "AWS"
identifiers = [aws_cloudfront_origin_access_identity.default.iam_arn]
}
}
}
resource "aws_s3_bucket_policy" "website" {
bucket = aws_s3_bucket.website.id
policy = data.aws_iam_policy_document.s3_policy.json
}
#
# Route 53 Records
#
resource "aws_route53_record" "website" {
zone_id = var.zone_id
name = local.custom_subdomain
type = "A"
alias {
name = aws_cloudfront_distribution.default.domain_name
zone_id = aws_cloudfront_distribution.default.hosted_zone_id
evaluate_target_health = false
}
}
resource "aws_route53_record" "aliases" {
for_each = var.create_alias_records ? local.aliases : []
zone_id = var.zone_id
name = each.value
type = "A"
alias {
name = aws_cloudfront_distribution.default.domain_name
zone_id = aws_cloudfront_distribution.default.hosted_zone_id
evaluate_target_health = false
}
}