|
1 | 1 | locals { |
2 | | - website_enabled = var.redirect_all_requests_to != "" || var.index_document != "" || var.error_document != "" || var.routing_rules != "" |
3 | 2 | website_config = { |
4 | 3 | redirect_all = [ |
5 | 4 | { |
@@ -58,8 +57,24 @@ data "aws_iam_policy_document" "origin" { |
58 | 57 | } |
59 | 58 | } |
60 | 59 |
|
| 60 | +data "aws_iam_policy_document" "origin_website" { |
| 61 | + override_json = var.additional_bucket_policy |
| 62 | + |
| 63 | + statement { |
| 64 | + sid = "S3GetObjectForCloudFront" |
| 65 | + |
| 66 | + actions = ["s3:GetObject"] |
| 67 | + resources = ["arn:aws:s3:::$${bucket_name}$${origin_path}*"] |
| 68 | + |
| 69 | + principals { |
| 70 | + type = "AWS" |
| 71 | + identifiers = ["*"] |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
61 | 76 | data "template_file" "default" { |
62 | | - template = data.aws_iam_policy_document.origin.json |
| 77 | + template = var.website_enabled ? data.aws_iam_policy_document.origin_website.json : data.aws_iam_policy_document.origin.json |
63 | 78 |
|
64 | 79 | vars = { |
65 | 80 | origin_path = coalesce(var.origin_path, "/") |
@@ -97,7 +112,7 @@ resource "aws_s3_bucket" "origin" { |
97 | 112 | } |
98 | 113 |
|
99 | 114 | dynamic "website" { |
100 | | - for_each = local.website_enabled ? local.website_config[var.redirect_all_requests_to == "" ? "default" : "redirect_all"] : [] |
| 115 | + for_each = var.website_enabled ? local.website_config[var.redirect_all_requests_to == "" ? "default" : "redirect_all"] : [] |
101 | 116 | content { |
102 | 117 | error_document = lookup(website.value, "error_document", null) |
103 | 118 | index_document = lookup(website.value, "index_document", null) |
@@ -154,8 +169,8 @@ locals { |
154 | 169 | ) |
155 | 170 | ) |
156 | 171 |
|
157 | | - bucket_domain_name = var.use_regional_s3_endpoint ? format( |
158 | | - "%s.s3-%s.amazonaws.com", |
| 172 | + bucket_domain_name = (var.use_regional_s3_endpoint || var.website_enabled) ? format( |
| 173 | + var.website_enabled ? "%s.s3-website-%s.amazonaws.com" : "%s.s3-%s.amazonaws.com", |
159 | 174 | local.bucket, |
160 | 175 | data.aws_s3_bucket.selected.region, |
161 | 176 | ) : format(var.bucket_domain_format, local.bucket) |
@@ -185,8 +200,21 @@ resource "aws_cloudfront_distribution" "default" { |
185 | 200 | origin_id = module.distribution_label.id |
186 | 201 | origin_path = var.origin_path |
187 | 202 |
|
188 | | - s3_origin_config { |
189 | | - origin_access_identity = aws_cloudfront_origin_access_identity.default.cloudfront_access_identity_path |
| 203 | + dynamic "s3_origin_config" { |
| 204 | + for_each = ! var.website_enabled ? [1] : [] |
| 205 | + content { |
| 206 | + origin_access_identity = aws_cloudfront_origin_access_identity.default.cloudfront_access_identity_path |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + dynamic "custom_origin_config" { |
| 211 | + for_each = var.website_enabled ? [1] : [] |
| 212 | + content { |
| 213 | + http_port = 80 |
| 214 | + https_port = 443 |
| 215 | + origin_protocol_policy = "http-only" |
| 216 | + origin_ssl_protocols = ["TLSv1", "TLSv1.1", "TLSv1.2"] |
| 217 | + } |
190 | 218 | } |
191 | 219 | } |
192 | 220 |
|
|
0 commit comments