Deploy a Low-Cost Static Website on AWS (S3 + CloudFront) with Terraform
Why S3 + CloudFront is a low-cost hosting stack
Terraform deploys a private Amazon S3 (Simple Storage Service) origin and an Amazon CloudFront CDN (content delivery network) so a static site or Single-Page Application (SPA) stays cheap, HTTPS-ready, and globally cached.
On AWS Free Tier and low traffic, this stack often costs pennies. You avoid EC2 or VPS patching, capacity planning, and always-on servers. Static website hosting here is serverless: S3 stores objects; CloudFront caches and serves them at the edge.
The full code example is on GitHub.

Prerequisites
You need these before you apply the Terraform configuration:
- An AWS account
- A domain name
- The Terraform CLI
- AWS credentials or an AWS CLI profile
Set up Terraform and the remote state backend
Start with this provider.tf file. The provider region is us-east-1, which you need later for CloudFront certificates.
Loading...
Store Terraform state in encrypted persistent storage. An S3 bucket fits that role. Create the state bucket first, then point the backend at it. The initial S3 module looks like this:
Loading...
After the S3 bucket exists, add this backend block to provider.tf:
Loading...
Migrate local state to the remote backend with terraform init -migrate-state.
Enable HTTPS with ACM
Issue an ACM certificate (AWS Certificate Manager) for your custom domain. CloudFront terminates TLS for viewers. Request the certificate in us-east-1; CloudFront requires ACM certificates in that region.
Define the certificate in modules/acm/main.tf:
Loading...
DNS validation records prove you own the domain. ACM waits until those records resolve.
Provision a private S3 bucket and CloudFront with Origin Access Control (OAC)
Create a private S3 bucket for site objects and a CloudFront distribution in front of it. Origin Access Control (OAC) lets CloudFront read objects from that private bucket. Block public bucket access so visitors never hit S3 directly.
Do not enable the S3 website endpoint or bucket index and error documents. CloudFront sets default_root_object and rewrites 403/404 responses to /index.html so client-side routers work (Astro, Next.js static export, Hugo, Gatsby, and similar SPAs). viewer_protocol_policy = "redirect-to-https" sends HTTP traffic to HTTPS.
Part of modules/s3/main.tf:
Loading...
Customize CloudFront in modules/cloudfront/main.tf for your SPA. This example disables IPv6 because some networks advertise IPv6 support but route it incorrectly, which breaks connectivity.
Loading...
Custom domain and DNS
After terraform apply, read cloudfront_distribution_domain_name from a Terraform output or with terraform state list. Add a CNAME (or alias) at Route 53 or any nameserver so your domain resolves to the CloudFront distribution. ACM DNS validation for the certificate uses the same nameserver.
Deploy the static site and refresh the CDN cache
Build your static site or SPA, then sync the output to the S3 bucket:
Loading...
CloudFront caches aggressively. After you change HTML or assets, invalidate the distribution so viewers get the new version:
Loading...
Debugging and next steps
If the site fails to load, verify DNS with dig or nslookup. If records look correct, enable CloudFront access logs to an S3 bucket and inspect the HTTP requests.
After the site runs, consider these follow-ups:
- Add AWS WAF to filter abusive traffic.
- Ask your AWS account manager for a Well-Architected or security review if you run a company workload.
- Add an API with AWS Serverless, ECS, or EC2 when you need dynamic backends.