MORTIMERDEV LEARN / PATH
Ship a static site on AWS
Putting a static site on AWS takes more than uploading files: a private bucket, a CloudFront distribution that caches the right things for the right time, security headers, and a way to know when it goes wrong. This path builds each piece with the Labs tools, in the order you'd set them up.
- Intermediate
- 6 steps
- About 90 minutes
- Free
You’ll learn: Keeping the S3 bucket private behind Origin Access Control; Choosing CloudFront cache and origin request policies; Cache-Control for HTML and fingerprinted files; Security headers, including a Content-Security-Policy; An alarm and a dashboard for the distribution.
For: Web developers who want to host a site or single-page app on S3 and CloudFront and understand every setting, not just copy a template.
Builds on: AWS foundations for web developers. Not required, but helpful.
The steps.
6 steps · not startedStore it: a private S3 bucket
About 15 minutes · S3 CORS and Bucket Policy ExplainerThe bucket holds your files, but visitors should never reach it directly. Keep it private, let only your CloudFront distribution read it, and add CORS rules only if other sites need to fetch files from it.
You’ll learn
- Why Block Public Access stays on for a public website
- The Origin Access Control bucket policy, scoped to one distribution
- When you need CORS rules, and how S3 picks the one it uses
Read first
- Why your S3 CORS request is blocked, and how to fix it · 8 minute read
Try this in the tool
- Read the Origin Access Control bucket policy: which part limits it to one distribution?
- Load “Web fonts for any site” and test a request from another origin
- Load “Blocked: origin with a slash” and find why it never matches
Serve it: CloudFront
About 20 minutes · CloudFront Policy BuilderCloudFront serves your files from locations near your visitors and decides what to cache. Its cache policy decides which requests share a copy and for how long, so the wrong one either caches too little or serves old pages after a deploy.
You’ll learn
- Cache policies vs origin request policies, and what goes in the cache key
- How CloudFront's TTLs combine with the Cache-Control your files send
- Separate behaviors for HTML and for fingerprinted assets
Read first
Try this in the tool
- Start from the “Static site” example and read why the HTML gets its own cache policy
- Switch to “Single-page app” and read the finding about missing files
- Compare the TypeScript and Python CDK for the same setup
Cache it right: Cache-Control
About 15 minutes · HTTP Caching ExplainerCloudFront and browsers follow the Cache-Control header on each file. Get it right and pages update straight after a deploy while scripts and images stay cached for a year.
You’ll learn
- max-age for browsers, s-maxage for CloudFront
- no-cache vs no-store, and which one HTML needs
- immutable for files whose names change when their contents do
Read first
- s-maxage vs max-age: how long browsers and CDNs cache a response · 3 minute read
- no-cache vs no-store: why no-cache still caches · 3 minute read
Try this in the tool
- Paste Cache-Control: public, max-age=0, s-maxage=300 with CloudFront as the CDN and read what each cache does
- Try public, max-age=31536000, immutable for a fingerprinted script
Lock it down: security headers
About 20 minutes · CSP ExplainerA CloudFront response headers policy can add security headers to every response. The most important is a Content-Security-Policy, which stops injected scripts from running, and it's also the easiest to get wrong.
You’ll learn
- Building a policy that allows only the scripts and styles you use
- Why 'unsafe-inline' undoes most of the protection, and the alternatives
- Testing with Content-Security-Policy-Report-Only before enforcing
Read first
- What 'unsafe-inline' does in a Content-Security-Policy, and what to use instead · 4 minute read
- "Refused to load" CSP errors: how to read them and fix the policy · 4 minute read
Try this in the tool
- Build a policy for your site, then paste it into the CloudFront builder's custom security headers
- Test it against a blocked request and read which directive applied
Know when it breaks: an alarm
About 10 minutes · CloudWatch Alarm BuilderA static site rarely breaks, so when it does nobody is looking. An alarm on CloudFront's error rate tells you before your visitors do.
You’ll learn
- CloudFront's 5xxErrorRate metric, and why the alarm must be in us-east-1
- Treating missing data as not breaching for a quiet site
Try this in the tool
- Pick the CloudFront 5xx error rate preset and run the sample data
- Read the region note, then find the same rule in the CDK
Watch it: a dashboard
About 10 minutes · CloudWatch Dashboard BuilderA small dashboard puts the alarm, and real-user data if you add RUM, on one screen, so you can check the site after every deploy.
You’ll learn
- An alarm status widget for the alarms you care about
- Adding a RUM app monitor for page speed and JavaScript errors
Try this in the tool
- Add the CloudFront alarm's name from the last step and read the CDK
- Add a RUM app monitor and compare the preview
Ship it
With the pieces in place, a deploy is the same few steps each time.
Every deploy
- Upload HTML with a short Cache-Control and fingerprinted files with a year and immutable, for example with aws s3 sync --cache-control.
- Run cdk diff first if the infrastructure changed, and check nothing is replaced.
- Invalidate the HTML paths (or /*) so CloudFront fetches the new pages.
- Open the site and the dashboard, and check the response headers with curl -I.
Before you launch
- For your own domain, request the certificate in us-east-1: CloudFront only uses certificates from that region.
- Run the CSP in Report-Only for a while and fix what it reports before enforcing it.
- Check that the bucket isn't public and that its website endpoint isn't how visitors reach the site.
- Make sure the alarm notifies someone who will see it.
Next: Observability on AWS: set up, watch, act · 4 steps, about 65 minutes.
