Set up CloudFront static website
This guide shows you how to create a static website hosted on S3 with CloudFront distribution, SSL certificate, and custom domain.
Reference implementation
See how CloudFront static website is configured in pirates-iac
.
Before you begin
- Have an AWS account with networking and DNS infrastructure configured
- Have the
gh
CLI installed and authenticated
Step 1: Add the package
Navigate to your infrastructure repository directory:
Add the CloudFront static website package:
- Replace
web-monkey
with a stack name that fits your project.
Step 2: Configure the package
Create configuration file for your static website:
StackName: "web-monkey"
cloudfront-static-website-data.StackName: "web-monkey-data"
Name: "monkey"
RootObject: index.html
ErrorObject: 404.html
RedirectAllToIndex: false # (1)!
AppendIndexToDirectories: false # (2)!
IamForCicd:
Enable: true
GitHubRepo: golden-path-docs # (3)!
cloudfront-static-website-data.AutoForwardLogs:
Enable: false # (4)!
versions.AwsProviderVersion: ">= 6.0.0"
- By default, CloudFront returns a
403 Forbidden
error when someone requests a file that doesn't exist in your S3 bucket. Enable this setting to serveindex.html
instead of the error page. This allows your Single Page Application (SPA) to handle client-side routing - the SPA receives the request and can display the correct content based on the URL path - When someone visits a directory URL like
/getting-started/
, CloudFront needs to know which file to serve. Enable this to automatically appendindex.html
to directory requests, so/getting-started/
serves/getting-started/index.html
. - Replace
golden-path-docs
with your GitHub repository. - Want logs in Datadog? Enable this later.
Step 3: Install the package
Install the package to generate Terraform files:
Step 4: Apply the configuration
Apply the data stack first. This creates the S3 buckets for content and logs:
Apply the main CloudFront stack:
- Takes about 5 minutes ⏳
Time for a break?
It takes about 5 minutes to provision the CloudFront distribution.
The website will be available at https://web-monkey.pirates-dev.oslo.systems
:
Step 5: Upload static files manually (optional)
Upload some files to the S3 bucket with at least one index.html
file:
Step 6: Set up secrets for CI/CD (optional)
Run the script to set up CI/CD secrets for GitHub Actions:
- The script will prompt you to confirm ✅
The script will set these secrets in your GitHub repository:
AWS_ROLE_ARN
S3_BUCKET_NAME
CLOUDFRONT_DISTRIBUTION_ID
Next step
Create the CloudFront deploy workflow that will allow automated deployment of your static site from GitHub Actions.