Skip to content
EgyKode
03 · AWS by handLab 16 / 59
Guided labaws

Static Site on S3 + CloudFront

Serve a site from a private bucket through a CDN with HTTPS, correct cache headers, and a cleanup you actually run.

Time
55 min
Level
Beginner
Objectives
5 objectives
Cost
Low cost

Where this fits in the platform

This lab adds

  • A bucket served through a CDN

Before you start

You will need

  • AWS CLI v2, configured
  • An AWS account

You will be able to

  • Serve a private S3 bucket through CloudFront with Origin Access Control
  • Set cache headers that differ for immutable assets and HTML
  • Confirm compression and caching from response headers, not assumptions
  • Destroy everything you created

CostLow cost

— CloudFront's always-free tier covers 1 TB egress and 10M requests per month, and a few MB in S3 costs well under $0.01. Nothing here provisions an hourly resource: no NAT Gateway, no load balancer.

How to clean up

The scenario#

You need to put a static site on the internet, on HTTPS, cheaply, and without leaving a bucket open to the world.

This is the architecture EgyKode itself runs on — the page you are reading is served exactly this way, so the Terraform in infrastructure/terraform/production/ is the finished version of what you are about to build by hand.

Hands-on environment

Run it on AWS

This lab builds real cloud infrastructure, so it needs your own AWS account. Follow the cost and cleanup notes above — the resources are yours, and so is the bill.

Anything you tick here is your own record. EgyKode cannot see inside that terminal, so the success criteria stay self-assessed even when the environment checks your work for you.

A private bucket

Step 1 of 5

Why not just make the bucket public?#

S3 can serve a website directly. It is also the single most common cause of real-world data exposure, it cannot do HTTPS on your own domain, and it has no edge cache. The pattern below keeps the bucket private and lets exactly one CloudFront distribution read it.

Clean up#

Run this even if the lab is unfinished. Everything above is inside the free tier, but an account full of half-built experiments is how a surprise bill starts.

DestructiveThis removes real resources. Check which environment you are in first.

Terminal
aws cloudfront get-distribution-config --id $DIST_ID > dist.json  # note the ETag
Disable the distribution (set Enabled=false) and wait for Deployed
aws cloudfront delete-distribution --id $DIST_ID --if-match $ETAG
aws s3 rm s3://$BUCKET --recursive
aws s3api delete-bucket --bucket $BUCKET
aws cloudfront delete-origin-access-control --id $OAC_ID --if-match $OAC_ETAG
Verify: aws s3 ls | grep $BUCKET  # should print nothing

Success criteria

0 of 5

The concept behind it

Ready to try it without help?Do the challenge

Next up

Lab 16 of 59 on the project path

Production DNS & TLS with Route 53 and ACMTake a site from an IP address to a real domain over HTTPS, with a certificate that renews itself.Why next: A bucket served through a CDN50 minIntermediate

Previous: AWS VPC Networking by Hand