Skip to content

Commit ab298bf

Browse files
authored
Merge pull request #2 from jakobpin/master
Replace S3 API with Website endpoints to fix access denied redirect
2 parents 39f696b + e9f4512 commit ab298bf

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

main.tf

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,6 @@ resource "aws_s3_bucket" "website_redirect" {
138138
}
139139

140140
## CloudFront
141-
# Creates an Amazon CloudFront origin access identity (will be used in the distribution origin configuration)
142-
resource "aws_cloudfront_origin_access_identity" "origin_access_identity_website" {
143-
comment = "CloudfrontOriginAccessIdentity - ${var.website-domain-main}"
144-
}
145-
146141
# Creates the CloudFront distribution to serve the static website
147142
resource "aws_cloudfront_distribution" "website_cdn_root" {
148143
enabled = true
@@ -151,10 +146,13 @@ resource "aws_cloudfront_distribution" "website_cdn_root" {
151146

152147
origin {
153148
origin_id = "origin-bucket-${aws_s3_bucket.website_root.id}"
154-
domain_name = aws_s3_bucket.website_root.bucket_regional_domain_name
149+
domain_name = aws_s3_bucket.website_root.website_endpoint
155150

156-
s3_origin_config {
157-
origin_access_identity = aws_cloudfront_origin_access_identity.origin_access_identity_website.cloudfront_access_identity_path
151+
custom_origin_config {
152+
origin_protocol_policy = "http-only"
153+
http_port = 80
154+
https_port = 443
155+
origin_ssl_protocols = ["TLSv1.2", "TLSv1.1", "TLSv1"]
158156
}
159157
}
160158

@@ -234,24 +232,21 @@ resource "aws_route53_record" "website_cdn_root_record" {
234232
}
235233

236234

237-
# Creates policy to limit access to the S3 bucket to CloudFront Origin
235+
# Creates policy to allow public access to the S3 bucket
238236
resource "aws_s3_bucket_policy" "update_website_root_bucket_policy" {
239237
bucket = aws_s3_bucket.website_root.id
240238

241239
policy = <<POLICY
242240
{
243-
"Version": "2008-10-17",
244-
"Id": "PolicyForCloudFrontPrivateContent",
241+
"Version": "2012-10-17",
242+
"Id": "PolicyForWebsiteEndpointsPublicContent",
245243
"Statement": [
246244
{
247-
"Sid": "AllowCloudFrontOriginAccess",
245+
"Sid": "PublicRead",
248246
"Effect": "Allow",
249-
"Principal": {
250-
"AWS": "${aws_cloudfront_origin_access_identity.origin_access_identity_website.iam_arn}"
251-
},
247+
"Principal": "*",
252248
"Action": [
253-
"s3:GetObject",
254-
"s3:ListBucket"
249+
"s3:GetObject"
255250
],
256251
"Resource": [
257252
"${aws_s3_bucket.website_root.arn}/*",
@@ -276,9 +271,7 @@ resource "aws_cloudfront_distribution" "website_cdn_redirect" {
276271
custom_origin_config {
277272
http_port = 80
278273
https_port = 443
279-
origin_keepalive_timeout = 5
280274
origin_protocol_policy = "http-only"
281-
origin_read_timeout = 30
282275
origin_ssl_protocols = ["TLSv1", "TLSv1.1", "TLSv1.2"]
283276
}
284277
}

0 commit comments

Comments
 (0)