Skip to content

Commit 2e24291

Browse files
authored
Add website_enabled option to use website as origin (#73)
Also update the default value of index_document. Which is required in S3 website block.
1 parent 7abff70 commit 2e24291

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ Available targets:
174174
| forward_query_string | Forward query strings to the origin that is associated with this cache behavior | bool | `false` | no |
175175
| geo_restriction_locations | List of country codes for which CloudFront either to distribute content (whitelist) or not distribute your content (blacklist) | list(string) | `<list>` | no |
176176
| geo_restriction_type | Method that use to restrict distribution of your content by country: `none`, `whitelist`, or `blacklist` | string | `none` | no |
177-
| index_document | Amazon S3 returns this index document when requests are made to the root domain or any of the subfolders | string | `` | no |
177+
| index_document | Amazon S3 returns this index document when requests are made to the root domain or any of the subfolders | string | `index.html` | no |
178178
| ipv6_enabled | Set to true to enable an AAAA DNS record to be set as well as the A record | bool | `true` | no |
179179
| lambda_function_association | A config block that triggers a lambda function with specific actions | object | `<list>` | no |
180180
| log_expiration_days | Number of days after which to expunge the objects | number | `90` | no |
@@ -204,6 +204,7 @@ Available targets:
204204
| viewer_protocol_policy | allow-all, redirect-to-https | string | `redirect-to-https` | no |
205205
| wait_for_deployment | When set to 'true' the resource will wait for the distribution status to change from InProgress to Deployed | bool | `true` | no |
206206
| web_acl_id | ID of the AWS WAF web ACL that is associated with the distribution | string | `` | no |
207+
| website_enabled | Set to true to use an S3 static website as origin | bool | `false` | no |
207208

208209
## Outputs
209210

docs/terraform.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
| forward_query_string | Forward query strings to the origin that is associated with this cache behavior | bool | `false` | no |
3131
| geo_restriction_locations | List of country codes for which CloudFront either to distribute content (whitelist) or not distribute your content (blacklist) | list(string) | `<list>` | no |
3232
| geo_restriction_type | Method that use to restrict distribution of your content by country: `none`, `whitelist`, or `blacklist` | string | `none` | no |
33-
| index_document | Amazon S3 returns this index document when requests are made to the root domain or any of the subfolders | string | `` | no |
33+
| index_document | Amazon S3 returns this index document when requests are made to the root domain or any of the subfolders | string | `index.html` | no |
3434
| ipv6_enabled | Set to true to enable an AAAA DNS record to be set as well as the A record | bool | `true` | no |
3535
| lambda_function_association | A config block that triggers a lambda function with specific actions | object | `<list>` | no |
3636
| log_expiration_days | Number of days after which to expunge the objects | number | `90` | no |
@@ -60,6 +60,7 @@
6060
| viewer_protocol_policy | allow-all, redirect-to-https | string | `redirect-to-https` | no |
6161
| wait_for_deployment | When set to 'true' the resource will wait for the distribution status to change from InProgress to Deployed | bool | `true` | no |
6262
| web_acl_id | ID of the AWS WAF web ACL that is associated with the distribution | string | `` | no |
63+
| website_enabled | Set to true to use an S3 static website as origin | bool | `false` | no |
6364

6465
## Outputs
6566

main.tf

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
locals {
2-
website_enabled = var.redirect_all_requests_to != "" || var.index_document != "" || var.error_document != "" || var.routing_rules != ""
32
website_config = {
43
redirect_all = [
54
{
@@ -58,8 +57,24 @@ data "aws_iam_policy_document" "origin" {
5857
}
5958
}
6059

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+
6176
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
6378

6479
vars = {
6580
origin_path = coalesce(var.origin_path, "/")
@@ -97,7 +112,7 @@ resource "aws_s3_bucket" "origin" {
97112
}
98113

99114
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"] : []
101116
content {
102117
error_document = lookup(website.value, "error_document", null)
103118
index_document = lookup(website.value, "index_document", null)
@@ -154,8 +169,8 @@ locals {
154169
)
155170
)
156171

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",
159174
local.bucket,
160175
data.aws_s3_bucket.selected.region,
161176
) : format(var.bucket_domain_format, local.bucket)
@@ -185,8 +200,21 @@ resource "aws_cloudfront_distribution" "default" {
185200
origin_id = module.distribution_label.id
186201
origin_path = var.origin_path
187202

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+
}
190218
}
191219
}
192220

variables.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ variable "encryption_enabled" {
344344

345345
variable "index_document" {
346346
type = string
347-
default = ""
347+
default = "index.html"
348348
description = "Amazon S3 returns this index document when requests are made to the root domain or any of the subfolders"
349349
}
350350

@@ -371,3 +371,9 @@ variable "ipv6_enabled" {
371371
default = true
372372
description = "Set to true to enable an AAAA DNS record to be set as well as the A record"
373373
}
374+
375+
variable "website_enabled" {
376+
type = bool
377+
default = false
378+
description = "Set to true to use an S3 static website as origin"
379+
}

0 commit comments

Comments
 (0)