Skip to content

Commit

Permalink
Merge pull request #9 from fillup/develop
Browse files Browse the repository at this point in the history
Release 0.12 support and new custom error handling configuration
  • Loading branch information
fillup authored Aug 10, 2020
2 parents d22b0a4 + 4518a67 commit ce38f9f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ 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`
- `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`
Expand All @@ -40,6 +45,30 @@ 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 = [
{
error_code = 404
response_code = 200
response_page_path = "/index.html"
},
]
}
```

## License - MIT
MIT License

Expand Down
15 changes: 12 additions & 3 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
28 changes: 28 additions & 0 deletions vars.tf
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,34 @@ 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
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
Expand Down

0 comments on commit ce38f9f

Please sign in to comment.