From 0e7d6c46f2c3077e405e2d82a9ff2ce0f42f2713 Mon Sep 17 00:00:00 2001 From: Phillip Date: Mon, 27 Jul 2020 10:59:33 -0400 Subject: [PATCH 1/4] allow overriding index and error documents --- README.md | 2 ++ main.tf | 4 ++-- vars.tf | 12 ++++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1370eb5..755996e 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,8 @@ supports S3 redirects. This module helps keep setup consistent for multiple Hugo - `cf_min_ttl` - Minimum CloudFront caching TTL. Default: `0` - `cf_max_ttl` - Maximum CloudFront caching TTL. Default: `31536000` - `cf_price_class` - The CloudFront pricing class to use. Default: `PriceClass_All` + - `error_document` - The file that should be served for errors. Default: `404.html` + - `index_document` - The default file to be served. Default: `index.html` - `origin_path` - Path to document root in S3 bucket without slashes. Default: `public` - `routing_rules` - A json array containing routing rules describing redirect behavior and when redirects are applied. Default routes `/` to `index.html` - `viewer_protocol_policy` - One of allow-all, https-only, or redirect-to-https. Default: `redirect-to-https` diff --git a/main.tf b/main.tf index 6402aa4..cd8cb34 100644 --- a/main.tf +++ b/main.tf @@ -17,8 +17,8 @@ resource "aws_s3_bucket" "hugo" { force_destroy = true website { - index_document = "index.html" - error_document = "${var.origin_path}/404.html" + index_document = var.index_document + error_document = "${var.origin_path}/${var.error_document}" // Routing rule is needed to support hugo friendly urls routing_rules = var.routing_rules diff --git a/vars.tf b/vars.tf index 6078501..312cdb3 100644 --- a/vars.tf +++ b/vars.tf @@ -73,6 +73,18 @@ variable "cors_max_age_seconds" { default = 3000 } +variable "error_document" { + description = "Error page document in S3 bucket" + type = string + default = "404.html" +} + +variable "index_document" { + description = "Index page document in S3 bucket" + type = string + default = "index.html" +} + variable "origin_path" { description = "Path in S3 bucket for hosted files, with leading slash" type = string From d78a47f3ee4985902748c9fe943972e8163d4cf4 Mon Sep 17 00:00:00 2001 From: Phillip Date: Mon, 27 Jul 2020 14:25:45 -0400 Subject: [PATCH 2/4] add support for custom error responses for cloudfront --- main.tf | 11 ++++++++++- vars.tf | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/main.tf b/main.tf index cd8cb34..acef1c7 100644 --- a/main.tf +++ b/main.tf @@ -61,9 +61,18 @@ resource "aws_cloudfront_distribution" "hugo" { origin_path = var.origin_path } + dynamic "custom_error_response" { + for_each = var.custom_error_response + content { + error_code = custom_error_response.value.error_code + response_code = custom_error_response.value.response_code + response_page_path = custom_error_response.value.response_page_path + } + } + enabled = true is_ipv6_enabled = true - default_root_object = "index.html" + default_root_object = var.default_root_object aliases = var.aliases diff --git a/vars.tf b/vars.tf index 312cdb3..633293f 100644 --- a/vars.tf +++ b/vars.tf @@ -73,6 +73,22 @@ variable "cors_max_age_seconds" { default = 3000 } +variable "custom_error_response" { + description = "Optionally a list of custom error response configurations for CloudFront distribution" + type = set(object({ + error_code = number + response_code = number + response_page_path = string + })) + default = null +} + +variable "default_root_object" { + description = "CloudFront distribution default_root_object" + type = string + default = "index.html" +} + variable "error_document" { description = "Error page document in S3 bucket" type = string From b3ccf07b0632fd8b4632832ce0aa6d8c18b8f905 Mon Sep 17 00:00:00 2001 From: Phillip Date: Mon, 27 Jul 2020 14:36:49 -0400 Subject: [PATCH 3/4] update docs for custom_error_message --- README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/README.md b/README.md index 755996e..94111e9 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,9 @@ supports S3 redirects. This module helps keep setup consistent for multiple Hugo - `cf_min_ttl` - Minimum CloudFront caching TTL. Default: `0` - `cf_max_ttl` - Maximum CloudFront caching TTL. Default: `31536000` - `cf_price_class` - The CloudFront pricing class to use. Default: `PriceClass_All` + - `custom_error_response` - A list of objects to provide custom error responses from CloudFront. + See [Using Custom Error Responses from CloudFront](#using-custom-error-responses-from-cloudfront) for details. + - `default_root_object` - Default root object for CloudFlare to request when not otherwise specified. Default: `index.html` - `error_document` - The file that should be served for errors. Default: `404.html` - `index_document` - The default file to be served. Default: `index.html` - `origin_path` - Path to document root in S3 bucket without slashes. Default: `public` @@ -42,6 +45,28 @@ module "hugosite" { } ``` +## Using Custom Error Responses from CloudFront +Cloudfront allows you to override error responses if desired. This is useful when hosting Single Page Apps on S3 +and want to leverage the default error document to route all requests to your index, but prevent S3 from returning +a 404 error to the browser. Here is an example of replacing 404 errors with 200 OK responses. + +```hcl +module "hugosite" { + source = "github.com/fillup/terraform-hugo-s3-cloudfront" + aliases = ["www.domain.com", "domain.com"] + bucket_name = "www.domain.com" + cert_domain = "*.domain.com" + deployment_user_arn = "arn:aws:iam::111122223333:person" + default_root_object = null + error_document = "index.html" + custom_error_response = set(object({ + error_code = 404 + response_code = 200 + response_page_path = "/index.html" + })) +} +``` + ## License - MIT MIT License From 78f0ec0669974dd8d2c1ec83a0a80f32707bc1af Mon Sep 17 00:00:00 2001 From: Phillip Date: Mon, 10 Aug 2020 19:26:36 -0400 Subject: [PATCH 4/4] fix docs --- README.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 94111e9..f2a4bad 100644 --- a/README.md +++ b/README.md @@ -59,11 +59,13 @@ module "hugosite" { deployment_user_arn = "arn:aws:iam::111122223333:person" default_root_object = null error_document = "index.html" - custom_error_response = set(object({ - error_code = 404 - response_code = 200 - response_page_path = "/index.html" - })) + custom_error_response = [ + { + error_code = 404 + response_code = 200 + response_page_path = "/index.html" + }, + ] } ```