Skip to content

Commit 2ec1ef6

Browse files
dmattiaaknysh
authored andcommitted
Add support for website config on origin (#61)
This code is heavily inspired by https://github.com/cloudposse/terraform-aws-s3-website This is in response to #14
1 parent 267c4a4 commit 2ec1ef6

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,15 @@ Available targets:
163163
| delimiter | Delimiter to be used between `namespace`, `stage`, `name` and `attributes` | string | `-` | no |
164164
| enabled | Select Enabled if you want CloudFront to begin processing requests as soon as the distribution is created, or select Disabled if you do not want CloudFront to begin processing requests after the distribution is created. | bool | `true` | no |
165165
| encryption_enabled | When set to 'true' the resource will have aes256 encryption enabled by default | bool | `false` | no |
166+
| error_document | An absolute path to the document to return in case of a 4XX error | string | `` | no |
166167
| extra_logs_attributes | Additional attributes to put onto the log bucket label | list(string) | `<list>` | no |
167168
| extra_origin_attributes | Additional attributes to put onto the origin label | list(string) | `<list>` | no |
168169
| forward_cookies | Time in seconds that browser can cache the response for S3 bucket | string | `none` | no |
169170
| forward_header_values | A list of whitelisted header values to forward to the origin | list(string) | `<list>` | no |
170171
| forward_query_string | Forward query strings to the origin that is associated with this cache behavior | bool | `false` | no |
171172
| 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 |
172173
| geo_restriction_type | Method that use to restrict distribution of your content by country: `none`, `whitelist`, or `blacklist` | string | `none` | no |
174+
| index_document | Amazon S3 returns this index document when requests are made to the root domain or any of the subfolders | string | `` | no |
173175
| is_ipv6_enabled | State of CloudFront IPv6 | bool | `true` | no |
174176
| lambda_function_association | A config block that triggers a lambda function with specific actions | object | `<list>` | no |
175177
| log_expiration_days | Number of days after which to expunge the objects | string | `90` | no |
@@ -189,6 +191,8 @@ Available targets:
189191
| parent_zone_id | ID of the hosted zone to contain this record (or specify `parent_zone_name`) | string | `` | no |
190192
| parent_zone_name | Name of the hosted zone to contain this record (or specify `parent_zone_id`) | string | `` | no |
191193
| price_class | Price class for this distribution: `PriceClass_All`, `PriceClass_200`, `PriceClass_100` | string | `PriceClass_100` | no |
194+
| redirect_all_requests_to | A hostname to redirect all website requests for this distribution to. If this is set, it overrides other website settings | string | `` | no |
195+
| routing_rules | A json array containing routing rules describing redirect behavior and when redirects are applied | string | `` | no |
192196
| stage | Stage (e.g. `prod`, `dev`, `staging`) | string | `` | no |
193197
| static_s3_bucket | aws-cli is a bucket owned by amazon that will perminantly exist. It allows for the data source to be called during the destruction process without failing. It doesn't get used for anything else, this is a safe workaround for handling the fact that if a data source like the one `aws_s3_bucket.selected` gets an error, you can't continue the terraform process which also includes the 'destroy' command, where is doesn't even need this data source! Don't change this bucket name, it's a variable so that we can provide this description. And this works around a problem that is an edge case. | string | `aws-cli` | no |
194198
| tags | Additional tags (e.g. map(`BusinessUnit`,`XYZ`) | map(string) | `<map>` | no |

docs/terraform.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222
| delimiter | Delimiter to be used between `namespace`, `stage`, `name` and `attributes` | string | `-` | no |
2323
| enabled | Select Enabled if you want CloudFront to begin processing requests as soon as the distribution is created, or select Disabled if you do not want CloudFront to begin processing requests after the distribution is created. | bool | `true` | no |
2424
| encryption_enabled | When set to 'true' the resource will have aes256 encryption enabled by default | bool | `false` | no |
25+
| error_document | An absolute path to the document to return in case of a 4XX error | string | `` | no |
2526
| extra_logs_attributes | Additional attributes to put onto the log bucket label | list(string) | `<list>` | no |
2627
| extra_origin_attributes | Additional attributes to put onto the origin label | list(string) | `<list>` | no |
2728
| forward_cookies | Time in seconds that browser can cache the response for S3 bucket | string | `none` | no |
2829
| forward_header_values | A list of whitelisted header values to forward to the origin | list(string) | `<list>` | no |
2930
| forward_query_string | Forward query strings to the origin that is associated with this cache behavior | bool | `false` | no |
3031
| 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 |
3132
| 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 |
3234
| is_ipv6_enabled | State of CloudFront IPv6 | bool | `true` | no |
3335
| lambda_function_association | A config block that triggers a lambda function with specific actions | object | `<list>` | no |
3436
| log_expiration_days | Number of days after which to expunge the objects | string | `90` | no |
@@ -48,6 +50,8 @@
4850
| parent_zone_id | ID of the hosted zone to contain this record (or specify `parent_zone_name`) | string | `` | no |
4951
| parent_zone_name | Name of the hosted zone to contain this record (or specify `parent_zone_id`) | string | `` | no |
5052
| price_class | Price class for this distribution: `PriceClass_All`, `PriceClass_200`, `PriceClass_100` | string | `PriceClass_100` | no |
53+
| redirect_all_requests_to | A hostname to redirect all website requests for this distribution to. If this is set, it overrides other website settings | string | `` | no |
54+
| routing_rules | A json array containing routing rules describing redirect behavior and when redirects are applied | string | `` | no |
5155
| stage | Stage (e.g. `prod`, `dev`, `staging`) | string | `` | no |
5256
| static_s3_bucket | aws-cli is a bucket owned by amazon that will perminantly exist. It allows for the data source to be called during the destruction process without failing. It doesn't get used for anything else, this is a safe workaround for handling the fact that if a data source like the one `aws_s3_bucket.selected` gets an error, you can't continue the terraform process which also includes the 'destroy' command, where is doesn't even need this data source! Don't change this bucket name, it's a variable so that we can provide this description. And this works around a problem that is an edge case. | string | `aws-cli` | no |
5357
| tags | Additional tags (e.g. map(`BusinessUnit`,`XYZ`) | map(string) | `<map>` | no |

main.tf

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
locals {
2+
website_enabled = var.redirect_all_requests_to != "" || var.index_document != "" || var.error_document != "" || var.routing_rules != ""
3+
website_config = {
4+
redirect_all = [
5+
{
6+
redirect_all_requests_to = var.redirect_all_requests_to
7+
}
8+
]
9+
default = [
10+
{
11+
index_document = var.index_document
12+
error_document = var.error_document
13+
routing_rules = var.routing_rules
14+
}
15+
]
16+
}
17+
}
18+
119
module "origin_label" {
220
source = "git::https://github.com/cloudposse/terraform-terraform-label.git?ref=tags/0.4.0"
321
namespace = var.namespace
@@ -78,6 +96,16 @@ resource "aws_s3_bucket" "origin" {
7896
}
7997
}
8098

99+
dynamic "website" {
100+
for_each = local.website_enabled ? local.website_config[var.redirect_all_requests_to == "" ? "default" : "redirect_all"] : []
101+
content {
102+
error_document = lookup(website.value, "error_document", null)
103+
index_document = lookup(website.value, "index_document", null)
104+
redirect_all_requests_to = lookup(website.value, "redirect_all_requests_to", null)
105+
routing_rules = lookup(website.value, "routing_rules", null)
106+
}
107+
}
108+
81109
cors_rule {
82110
allowed_headers = var.cors_allowed_headers
83111
allowed_methods = var.cors_allowed_methods

variables.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,4 +337,28 @@ variable "encryption_enabled" {
337337
type = bool
338338
default = false
339339
description = "When set to 'true' the resource will have aes256 encryption enabled by default"
340+
}
341+
342+
variable "index_document" {
343+
type = string
344+
default = ""
345+
description = "Amazon S3 returns this index document when requests are made to the root domain or any of the subfolders"
346+
}
347+
348+
variable "redirect_all_requests_to" {
349+
type = string
350+
default = ""
351+
description = "A hostname to redirect all website requests for this distribution to. If this is set, it overrides other website settings"
352+
}
353+
354+
variable "error_document" {
355+
type = string
356+
default = ""
357+
description = "An absolute path to the document to return in case of a 4XX error"
358+
}
359+
360+
variable "routing_rules" {
361+
type = string
362+
default = ""
363+
description = "A json array containing routing rules describing redirect behavior and when redirects are applied"
340364
}

0 commit comments

Comments
 (0)