cloudfront-static-website
Static website on S3 and CloudFront, with TLS and a custom domain.
Note
This stack might take a long time to both apply and destroy due to the CloudFront distribution. Deployment typically takes around 5 minutes, while destruction can take 5-6 minutes.
Note
This template also installs the cloudfront-static-website-data template, which creates the S3 buckets for content and logs. Apply the data stack first.
Architecture
This template creates the CloudFront distribution and associated resources that work with the S3 buckets created by the data stack:
- CloudFront Distribution - CDN with custom domain and TLS
- Route53 Record - DNS alias pointing to CloudFront
- ACM Certificate - TLS certificate for custom domain (us-east-1)
- Deployment (when
DeploymentPipelineV2.Enable: true) - Terraform deploys the website bundle with the deployment Lambda function from thecicd-commontemplate
The S3 buckets (content and logs) are created separately in the data stack to allow for independent lifecycle management.
Features
- HTTPS-only with custom domain
- Client-side routing support (when
RedirectAllToIndex: true) - requests that do not match a file getRootObjectwith status200(similar to react-cors-spa) - Optional URL rewriting (when
AppendIndexToDirectories: true) - addsindex.htmlto directory requests and URLs without extensions, based on AWS CloudFront Functions sample - Configurable root and error objects via
RootObjectandErrorObject
Usage
See these guides in the Golden Path documentation:
Variables
| Name | Description | Type | Default | Required |
|---|---|---|---|---|
Name |
Website name. Used as the subdomain, exposing the site at https://<Name>.<Environment>.oslo.systems. |
string |
n/a |
yes |
RedirectAllToIndex |
Serve RootObject with status 200 for every request that does not match a file, instead of the ErrorObject error page. Needed for single-page apps with client-side routing. |
bool |
false |
no |
RootObject |
The object served when the site root (/) is requested. Also the object that RedirectAllToIndex and AppendIndexToDirectories rewrite requests to. |
string |
index.html |
no |
ErrorObject |
The object served with status 404 when a requested file does not exist. Not used when RedirectAllToIndex is enabled. |
string |
404.html |
no |
AppendIndexToDirectories |
Rewrite requests ending in / or without a file extension to append /<RootObject>, so /about serves about/index.html. Useful for static site generators that output one index.html per directory. |
bool |
false |
no |
DeploymentPipelineV2 |
Deploy the website with the next-generation deployment pipeline. Replaces IamForCicd. |
map |
{"Enable": false} |
no |
IamForCicd |
Deprecated: use DeploymentPipelineV2 instead. Lets GitHub Actions in the website's repository (GitHubRepo, under the oslokommune org) deploy the site. The workflow must run in the GitHub environment <Environment>-<Name>. |
map |
{"Enable": false, "GitHubRepo": null} |
no |