Skip to content

Commit d1a5961

Browse files
seperated root and www
1 parent e105ef3 commit d1a5961

13 files changed

+217
-97
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,14 @@ This Terraform Module will create all required AWS resources to host a Static We
55

66
- [Documentation](https://cloudpedia.ai/terraform-module/aws-static-website/)
77
- [Terraform module](https://registry.terraform.io/modules/cloudpediaai/static-website/aws/latest)
8+
- [GitHub Repo](https://github.com/CloudPediaAI/terraform-aws-static-website)
9+
10+
11+
## Related Terraform Modules from CloudPedia.AI
12+
13+
### AWS Website Redirect
14+
This Terraform Module will create all required AWS resources to establish a permanent redirect from your Source Website (eg. example.net) to a Target Website (example.com).
15+
16+
- [Documentation](https://cloudpedia.ai/terraform-module/aws-website-redirect/)
17+
- [Terraform Module](https://registry.terraform.io/modules/cloudpediaai/website-redirect/aws/latest)
18+
- [GitHub Repo](https://github.com/CloudPediaAI/terraform-aws-website-redirect)

acm.tf renamed to acm-root.tf

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
resource "aws_acm_certificate" "ssl" {
1+
resource "aws_acm_certificate" "root" {
22
provider = aws.us-east-1
33
domain_name = local.domain_name
44
validation_method = "DNS"
5-
subject_alternative_names = [local.www_domain_name]
5+
# subject_alternative_names = [local.www_domain_name]
66

77
tags = var.tags
88

@@ -11,9 +11,9 @@ resource "aws_acm_certificate" "ssl" {
1111
}
1212
}
1313

14-
resource "aws_route53_record" "validation" {
14+
resource "aws_route53_record" "root_validation" {
1515
for_each = {
16-
for dvo in aws_acm_certificate.ssl.domain_validation_options : dvo.domain_name => {
16+
for dvo in aws_acm_certificate.root.domain_validation_options : dvo.domain_name => {
1717
name = dvo.resource_record_name
1818
record = dvo.resource_record_value
1919
type = dvo.resource_record_type
@@ -28,8 +28,8 @@ resource "aws_route53_record" "validation" {
2828
zone_id = (var.hosted_zone_id != null) ? data.aws_route53_zone.by_id[0].zone_id : data.aws_route53_zone.by_name[0].zone_id
2929
}
3030

31-
resource "aws_acm_certificate_validation" "ssl" {
31+
resource "aws_acm_certificate_validation" "root" {
3232
provider = aws.us-east-1
33-
certificate_arn = aws_acm_certificate.ssl.arn
34-
validation_record_fqdns = [for record in aws_route53_record.validation : record.fqdn]
33+
certificate_arn = aws_acm_certificate.root.arn
34+
validation_record_fqdns = [for record in aws_route53_record.root_validation : record.fqdn]
3535
}

acm-www.tf

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
resource "aws_acm_certificate" "www" {
2+
count = (var.need_www_redirect) ? 1 : 0
3+
4+
provider = aws.us-east-1
5+
domain_name = local.www_domain_name
6+
validation_method = "DNS"
7+
8+
tags = var.tags
9+
10+
lifecycle {
11+
create_before_destroy = true
12+
}
13+
}
14+
15+
resource "aws_route53_record" "www_validation" {
16+
for_each = (var.need_www_redirect) ? tomap({
17+
for dvo in aws_acm_certificate.www[0].domain_validation_options : dvo.domain_name => {
18+
name = dvo.resource_record_name
19+
record = dvo.resource_record_value
20+
type = dvo.resource_record_type
21+
}
22+
}) : {}
23+
24+
allow_overwrite = true
25+
name = each.value.name
26+
records = [each.value.record]
27+
ttl = 60
28+
type = each.value.type
29+
zone_id = (var.hosted_zone_id != null) ? data.aws_route53_zone.by_id[0].zone_id : data.aws_route53_zone.by_name[0].zone_id
30+
}
31+
32+
resource "aws_acm_certificate_validation" "www" {
33+
count = (var.need_www_redirect) ? 1 : 0
34+
35+
provider = aws.us-east-1
36+
certificate_arn = aws_acm_certificate.www[0].arn
37+
validation_record_fqdns = [for record in aws_route53_record.www_validation : record.fqdn]
38+
}

cloudfront.tf renamed to cloudfront-root.tf

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ resource "aws_cloudfront_distribution" "public" {
2121
count = (local.origin_access == "public") ? 1 : 0
2222

2323
origin {
24-
domain_name = aws_s3_bucket_website_configuration.web_portal_webConfig.website_endpoint
24+
domain_name = aws_s3_bucket_website_configuration.root.website_endpoint
2525
origin_id = "S3-${local.bucket_name}"
2626
custom_origin_config {
2727
http_port = 80
@@ -82,7 +82,7 @@ resource "aws_cloudfront_distribution" "public" {
8282
}
8383

8484
viewer_certificate {
85-
acm_certificate_arn = aws_acm_certificate.ssl.arn
85+
acm_certificate_arn = aws_acm_certificate.root.arn
8686
ssl_support_method = "sni-only"
8787
minimum_protocol_version = "TLSv1.2_2021"
8888
}
@@ -94,7 +94,7 @@ resource "aws_cloudfront_distribution" "oac" {
9494
count = (local.origin_access == "oac") ? 1 : 0
9595

9696
origin {
97-
domain_name = aws_s3_bucket.web_portal.bucket_regional_domain_name
97+
domain_name = aws_s3_bucket.root.bucket_regional_domain_name
9898
origin_id = "S3-${local.bucket_name}"
9999
origin_access_control_id = aws_cloudfront_origin_access_control.oac[0].id
100100
}
@@ -146,7 +146,7 @@ resource "aws_cloudfront_distribution" "oac" {
146146
}
147147

148148
viewer_certificate {
149-
acm_certificate_arn = aws_acm_certificate.ssl.arn
149+
acm_certificate_arn = aws_acm_certificate.root.arn
150150
ssl_support_method = "sni-only"
151151
minimum_protocol_version = "TLSv1.2_2021"
152152
}
@@ -159,7 +159,7 @@ resource "aws_cloudfront_distribution" "oai" {
159159
count = (local.origin_access == "oai") ? 1 : 0
160160

161161
origin {
162-
domain_name = aws_s3_bucket.web_portal.bucket_regional_domain_name
162+
domain_name = aws_s3_bucket.root.bucket_regional_domain_name
163163
origin_id = "S3-${local.bucket_name}"
164164
s3_origin_config {
165165
origin_access_identity = aws_cloudfront_origin_access_identity.oai[0].cloudfront_access_identity_path
@@ -213,7 +213,7 @@ resource "aws_cloudfront_distribution" "oai" {
213213
}
214214

215215
viewer_certificate {
216-
acm_certificate_arn = aws_acm_certificate.ssl.arn
216+
acm_certificate_arn = aws_acm_certificate.root.arn
217217
ssl_support_method = "sni-only"
218218
minimum_protocol_version = "TLSv1.2_2021"
219219
}

cloudfront-www.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ resource "aws_cloudfront_distribution" "public_www" {
33
count = (var.need_www_redirect) ? 1 : 0
44

55
origin {
6-
domain_name = aws_s3_bucket_website_configuration.web_portal_redirect_config[0].website_endpoint
6+
domain_name = aws_s3_bucket_website_configuration.www[0].website_endpoint
77
origin_id = "S3-${local.www_bucket_name}"
88
custom_origin_config {
99
http_port = 80
@@ -63,7 +63,7 @@ resource "aws_cloudfront_distribution" "public_www" {
6363
}
6464

6565
viewer_certificate {
66-
acm_certificate_arn = aws_acm_certificate.ssl.arn
66+
acm_certificate_arn = aws_acm_certificate.www[0].arn
6767
ssl_support_method = "sni-only"
6868
minimum_protocol_version = "TLSv1.2_2021"
6969
}

output.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
output "bucket" {
2-
value = aws_s3_bucket.web_portal
2+
value = aws_s3_bucket.root
33
description = "S3 Bucket created for the Static Website"
44
}
55

route53.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ data "aws_route53_zone" "by_name" {
1111
}
1212

1313
# creating A records in Route 53 to route traffic to the website
14-
resource "aws_route53_record" "root-a" {
14+
resource "aws_route53_record" "a_record_root" {
1515
zone_id = (var.hosted_zone_id != null) ? data.aws_route53_zone.by_id[0].zone_id : data.aws_route53_zone.by_name[0].zone_id
1616
name = local.domain_name
1717
type = "A"
@@ -23,7 +23,7 @@ resource "aws_route53_record" "root-a" {
2323
}
2424
}
2525

26-
resource "aws_route53_record" "www-a" {
26+
resource "aws_route53_record" "a_record_www" {
2727
count = (var.need_www_redirect) ? 1 : 0
2828

2929
zone_id = (var.hosted_zone_id != null) ? data.aws_route53_zone.by_id[0].zone_id : data.aws_route53_zone.by_name[0].zone_id

s3-obj-source.tf

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
2+
locals {
3+
mime_types = {
4+
aac = "audio/aac"
5+
abw = "application/x-abiword"
6+
apng = "image/apng"
7+
arc = "application/x-freearc"
8+
avif = "image/avif"
9+
avi = "video/x-msvideo"
10+
azw = "application/vnd.amazon.ebook"
11+
bin = "application/octet-stream"
12+
bmp = "image/bmp"
13+
bz = "application/x-bzip"
14+
bz2 = "application/x-bzip2"
15+
cda = "application/x-cdf"
16+
csh = "application/x-csh"
17+
css = "text/css"
18+
csv = "text/csv"
19+
doc = "application/msword"
20+
docx = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
21+
eot = "application/vnd.ms-fontobject"
22+
epub = "application/epub+zip"
23+
gz = "application/gzip"
24+
gif = "image/gif"
25+
htm = "text/html"
26+
html = "text/html"
27+
ico = "image/vnd.microsoft.icon"
28+
ics = "text/calendar"
29+
jar = "application/java-archive"
30+
jpeg = "image/jpeg"
31+
jpg = "image/jpeg"
32+
js = "application/javascript"
33+
json = "application/json"
34+
jsonld = "application/ld+json"
35+
map = "application/json"
36+
mid = "audio/midi, audio/x-midi"
37+
midi = "audio/midi, audio/x-midi"
38+
mjs = "text/javascript"
39+
mp3 = "audio/mpeg"
40+
mp4 = "video/mp4"
41+
mpeg = "video/mpeg"
42+
mpkg = "application/vnd.apple.installer+xml"
43+
odp = "application/vnd.oasis.opendocument.presentation"
44+
ods = "application/vnd.oasis.opendocument.spreadsheet"
45+
odt = "application/vnd.oasis.opendocument.text"
46+
oga = "audio/ogg"
47+
ogv = "video/ogg"
48+
ogx = "application/ogg"
49+
opus = "audio/opus"
50+
otf = "font/otf"
51+
png = "image/png"
52+
pdf = "application/pdf"
53+
php = "application/x-httpd-php"
54+
ppt = "application/vnd.ms-powerpoint"
55+
pptx = "application/vnd.openxmlformats-officedocument.presentationml.presentation"
56+
rar = "application/vnd.rar"
57+
rtf = "application/rtf"
58+
sh = "application/x-sh"
59+
svg = "image/svg+xml"
60+
tar = "application/x-tar"
61+
tif = "image/tiff"
62+
tiff = "image/tiff"
63+
ts = "video/mp2t"
64+
ttf = "font/ttf"
65+
txt = "text/plain"
66+
vsd = "application/vnd.visio"
67+
wav = "audio/wav"
68+
weba = "audio/webm"
69+
webm = "video/webm"
70+
webp = "image/webp"
71+
woff = "font/woff"
72+
woff2 = "font/woff2"
73+
xhtml = "application/xhtml+xml"
74+
xls = "application/vnd.ms-excel"
75+
xlsx = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
76+
xml = "application/xml"
77+
xul = "application/vnd.mozilla.xul+xml"
78+
zip = "application/zip"
79+
}
80+
}
81+
82+
resource "aws_s3_object" "website_source" {
83+
for_each = (var.website_source_folder != null) ? fileset(var.website_source_folder, "**") : []
84+
bucket = local.bucket_name
85+
key = each.value
86+
source = "${var.website_source_folder}/${each.value}"
87+
content_type = lookup(local.mime_types, split(".", each.value)[length(split(".", each.value)) - 1], "text/html")
88+
etag = filemd5("${var.website_source_folder}/${each.value}")
89+
}

s3-objects.tf renamed to s3-obj-template.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
resource "aws_s3_object" "index_html" {
22
count = (var.website_source_folder == null) ? 1 : 0
33

4-
depends_on = [aws_s3_bucket.web_portal]
4+
depends_on = [aws_s3_bucket.root]
55
bucket = local.bucket_name
66
key = "index.html"
77
content = "<!DOCTYPE html><html lang='en'><head><meta charset='UTF-8'><meta name='viewport' content='width=device-width,initial-scale=1.0'><title>Coming Soon</title><style>body{font-family:sans-serif;margin:0;padding:0;background-color:#84c98a} .container{max-width:800px;margin:0 auto;padding:20px} .header{text-align:center;color:#f1eaea;font-size:2em;margin-bottom:20px} .content{color:#025b6b;line-height:1.5} .footer{text-align:center;color:#3b3a3a;margin-top:20px}</style></head><body><div class='container'><div class='header'><h1>Something amazing is coming soon!</h1></div><div class='content'><p>Get ready for something brand new and exciting. We are working hard to bring you a breaking through experience. Stay tuned for more updates!</p></div><div class='footer'><p>Template created by CloudPedia.AI</p></div></div></body></html>"
@@ -11,7 +11,7 @@ resource "aws_s3_object" "index_html" {
1111
resource "aws_s3_object" "error_html" {
1212
count = (var.website_source_folder == null) ? 1 : 0
1313

14-
depends_on = [aws_s3_bucket.web_portal]
14+
depends_on = [aws_s3_bucket.root]
1515
bucket = local.bucket_name
1616
key = "error.html"
1717
content = "<!DOCTYPEhtml><html lang='en'><head><meta charset='UTF-8'><meta name='viewport' content='width=device-width,initial-scale=1.0'><title>Oops! Page Not Found</title><style>body{font-family:sans-serif;margin:0;padding:0;background-color:#ff7c7c} .container{max-width:800px;margin:0 auto;padding:20px;text-align:center} .header{text-align:center;color:#f1eaea;font-size:2em;margin-bottom:20px} .error-message{margin-bottom:20px} .error-message p{font-size:1.2em;color:#666} .navigation{margin-top:20px} .navigation ul{list-style:none;padding:0} .navigation li{margin-bottom:10px} .navigation a{text-decoration:none;color:#333} .navigation a:hover{text-decoration:underline} .footer{text-align:center;color:#3b3a3a;margin-top:20px}</style></head><body><div class='container'><div class='header'><h1>404: Page Not Found</h1></div><div class='error-message'><p>The page you are looking for does not seem exist anymore.</p></div><div class='navigation'><h2>Let's get you back on track!</h2><ul><li><a href='/'>Goto the homepage</a></li></ul></div><div class='footer'><p>Template cre ated by CloudPedia.AI</p></div></div></body></html>"

s3-policy.tf renamed to s3-policy-root.tf

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
data "aws_iam_policy_document" "root_access_public" {
32
count = (local.origin_access == "public") ? 1 : 0
43

@@ -16,8 +15,8 @@ data "aws_iam_policy_document" "root_access_public" {
1615
]
1716

1817
resources = [
19-
aws_s3_bucket.web_portal.arn,
20-
"${aws_s3_bucket.web_portal.arn}/*"
18+
aws_s3_bucket.root.arn,
19+
"${aws_s3_bucket.root.arn}/*"
2120
]
2221

2322
}
@@ -37,8 +36,8 @@ data "aws_iam_policy_document" "root_access_oac" {
3736
}
3837

3938
resources = [
40-
aws_s3_bucket.web_portal.arn,
41-
"${aws_s3_bucket.web_portal.arn}/*"
39+
aws_s3_bucket.root.arn,
40+
"${aws_s3_bucket.root.arn}/*"
4241
]
4342

4443
condition {
@@ -63,7 +62,7 @@ data "aws_iam_policy_document" "root_access_oac" {
6362
]
6463

6564
resources = [
66-
"${aws_s3_bucket.web_portal.arn}/*"
65+
"${aws_s3_bucket.root.arn}/*"
6766
]
6867

6968
condition {
@@ -88,8 +87,8 @@ data "aws_iam_policy_document" "root_access_oai" {
8887
}
8988

9089
resources = [
91-
aws_s3_bucket.web_portal.arn,
92-
"${aws_s3_bucket.web_portal.arn}/*"
90+
aws_s3_bucket.root.arn,
91+
"${aws_s3_bucket.root.arn}/*"
9392
]
9493

9594
condition {
@@ -110,37 +109,7 @@ data "aws_iam_policy_document" "root_access_oai" {
110109
}
111110

112111
resources = [
113-
"${aws_s3_bucket.web_portal.arn}/*"
112+
"${aws_s3_bucket.root.arn}/*"
114113
]
115114
}
116115
}
117-
118-
119-
data "aws_iam_policy_document" "www_access_public" {
120-
count = (var.need_www_redirect) ? 1 : 0
121-
122-
statement {
123-
sid = "allowReqFromCloudFrontOnly"
124-
effect = "Allow"
125-
126-
principals {
127-
type = "Service"
128-
identifiers = ["cloudfront.amazonaws.com"]
129-
}
130-
131-
132-
actions = [
133-
"s3:GetObject"
134-
]
135-
136-
resources = [
137-
"${aws_s3_bucket.web_portal_redirect[0].arn}/*"
138-
]
139-
140-
condition {
141-
test = "StringEquals"
142-
variable = "AWS:SourceArn"
143-
values = [format("arn:aws:cloudfront::%s:distribution/%s", data.aws_caller_identity.current.account_id, aws_cloudfront_distribution.public_www[0].id)]
144-
}
145-
}
146-
}

0 commit comments

Comments
 (0)