Skip to content

hyprnz/terraform-aws-s3-static-website-module

Repository files navigation

Terraform-aws-s3-static-website Module

This module can be used to create a static website implementation using S3 and Cloudfront resources. The module can optionally add a route53 entry to alias the Cloudfront with a custom domain.

The module requires a TLS certificate to be provisioned in the same AWS account in which the resources are being installed in. Cloudfront only supports binding to certificates in the us-east-1 region. A certificate arn is a required parameter for this module.

The module has been designed to support a Continuous Development/Deployment pattern. A config bucket object resource allows for the abstraction of configuration elements from the bundle being served, allowing the bundle to become an immutable artifact. Configuration becomes a runtime concern, as opposed to a build time concern.

This module also has an additional and separate artifactory module - more information on what this module provides can be found in the Artifactory README

React client side routing support

In order to support React's client side routing implementation, you'll need to provide the following value for the cloudfront_custom_errors variable as follows

cloudfront_custom_errors = [
  {
    error_caching_min_ttl = 300
    error_code            = 404
    response_code         = 200
    response_page_path    = "/index.html"
}]

The response_page_path (/index.html in this example) should match your static html file that invokes the React bundle. Further explanations for the settings can be found at https://www.terraform.io/docs/providers/aws/r/cloudfront_distribution.html#custom-error-response-arguments.

Implementation patterns

This module is used to provide the infrastructure for both serving static content in a way that creates pipeline building blocks that allows users to easily construct a pipeline to provide continuous delivery patterns. An example pipeline is shown below:

Build/Test/Package -> Copy to Artifactory (S3 Bucket) -> Copy to Module implementation (e.g. UAT) -> Promote to Module implementation (e.g. PROD)

ADR's

This module includes an Architectural Decision Register (ADR's) as represented by the first adr. All ADR's are listed here.

Terraform version support

This repo supports version 0.11 and 0.12 of Terraform. The implementation pattern is outlined in the following ADR 4. Dual support for Terraform version 0.11 and 0.12

Contributions

All contributions are accepted, details on how to contribute can be found in contrib.md.


Requirements

Name Version
terraform >= 0.12
aws ~> 5.0

Providers

Name Version
aws ~> 5.0
aws.global ~> 5.0

Modules

No modules.

Inputs

Name Description Type Default Required
namespace A namespace that is appended to the site_name variable. This minimises S3 bucket naming collisions. string n/a yes
site_name The name of the static website or js bundle to be hosted by the S3 bucket. This will be the bucket name prefix. string n/a yes
certificate_arn The arn of the certificate to be used for TLS connections. The certificate must be in the same account as the cloudfront resource. string null no
cloudfront_allowed_methods Controls which HTTP methods CloudFront processes and forwards to your Amazon S3 bucket or your custom origin. list(string)
[
"GET",
"HEAD",
"OPTIONS"
]
no
cloudfront_cached_methods Controls whether CloudFront caches the response to requests using the specified HTTP methods. list(string)
[
"GET",
"HEAD"
]
no
cloudfront_custom_errors A map of custom error settings for the CloudFront Distribution
list(object({
error_caching_min_ttl = number
error_code = number
response_code = number
response_page_path = string
}))
[] no
cloudfront_tags Additional tags to be added to all cloudfront resources. map(any) {} no
comment A comment for the Cloudfront distribution resource string "" no
create_certificate Determine if a certificate is created for the custom domain name, only work when domain_name is provided. Default is true. bool true no
create_custom_route53_record Determines if a route53 alias record should be created that matches the domain_name variable. Default is false. bool false no
default_ttl The default amount of time (in seconds) that an object is in a CloudFront cache before CloudFront forwards another request in the absence of an Cache-Control max-age or Expires header. Defaults to 1 hour. number 3600 no
domain_name Add a custom domain name to the CloudFront distribution. Must match the certificate name to provide a valid TLS connection. string null no
enable_cdn_compression Select whether you want CloudFront to automatically compress content for web requests that include Accept-Encoding: gzip in the request header. CloudFront compresses files of certain types for both Amazon S3 and custom origins. Default is true. bool true no
error_document_default The default error html file. Defaults to error.html. string "error.html" no
index_document_default The default html file. Defaults to index.html. string "index.html" no
max_ttl The maximum amount of time (in seconds) that an object is in a CloudFront cache before CloudFront forwards another request to your origin to determine whether the object has been updated. Only effective in the presence of Cache-Control max-age, Cache-Control s-maxage, and Expires headers. Defaults to 7 days. number 604800 no
min_ttl The minimum amount of time that you want objects to stay in CloudFront caches before CloudFront queries your origin to see whether the object has been updated. Defaults to 0 seconds. number 0 no
module_tags Additional tags that are added to all resources in this module. map(any) {} no
price_class The price class for this distribution. One of PriceClass_All, PriceClass_200, PriceClass_100. string "PriceClass_All" no
s3_tags Additional tags to be added to all s3 resources. map(any) {} no
site_config_values A map of js bundle configuration values required for a specific environment. map(any) {} no
wait_for_deployment If enabled, the resource will wait for the distribution status to change from InProgress to Deployed. Setting this to false will skip the process. Default: true. bool true no
zone_id The zone id of the hosted zone to create the alias record in. Used only when create_custom_route53_record is set to true. string "" no

Outputs

Name Description
cloudfront_hosted_zone The hosted zone id of the Cloudfront Distribution
cloudfront_url The URL for the Cloudfront Distribution - used to set the alias for the custom domain.
custom_domain_name The custom domain name of the CloudFront distribution.
s3_bucket_domain_name The domain name of the s3 web site bucket.
s3_bucket_regional_domain_name The regional domain name of the s3 web site bucket.


License

License

See LICENSE for full details.

Copyright 2020 Hypr NZ

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Copyright © 2020 Hypr NZ