The route

  1. Core Foundations: setting up an AWS account safely

    Lock down the account and set a budget before you create anything that costs money.

  2. Core Foundations: IAM, who can do what

    AWS Identity and Access Management from first principles: principals, policies, roles, temporary credentials, and how AWS decides whether a request is allowed.

  3. Core Foundations: VPC, your private network in the cloud

    Amazon VPC from first principles: CIDR ranges, subnets, route tables, internet and NAT gateways, and the difference between security groups and network ACLs.

  4. Core Foundations: EC2, virtual servers properly

    Amazon EC2 from first principles: instance types, AMIs, storage, pricing models, IAM roles for instances, and connecting with Session Manager instead of SSH keys.

  5. Core Foundations: S3, storage that underpins AWS

    Storage, security defaults and a first static site: the starting point for the project.

Project: Host a static website on S3 and CloudFront

Deploy a real website from a private S3 bucket through CloudFront, with HTTPS, clean URLs, security headers, sensible caching and a keyless deploy from GitHub Actions: the same set-up that serves cirruscape.com.

The S3 lesson gets a page online in four clicks. This project takes the same idea to the standard of a site you’d put your name to: everything defined in CloudFormation, URLs that work the way visitors expect, caching that’s fast without serving stale pages, and deploys that need no stored AWS keys. It’s how cirruscape.com runs.

It’s a brief, not a tutorial: it tells you what to build and how to check it works, and links back to the lessons for the how. Expect a weekend.

What you’ll build

 Browser ──HTTPS──► CloudFront ──────────────► S3 bucket (private)
                    │ URL rewrite function       ▲  Block Public Access on
                    │ security headers           │  only this distribution
                    │ cache policy               │  can read (OAC)
                    │ 404 page                   │
                                                 │
 GitHub Actions ──OIDC──► deploy role ──► sync files + invalidate cache
Piece What it does Lesson
S3 bucket Holds the site’s files, and is never public S3
CloudFront + Origin Access Control Serves the site over HTTPS from edge locations; the only thing allowed to read the bucket S3
Bucket policy, deploy role Who can read and who can write, and nothing more IAM
Budget Tells you if any of this starts costing money Account setup

Before you start

  • An account set up as in account setup, with a budget.
  • The AWS CLI, signed in with aws sso login (or CloudShell).
  • A site to publish. A single index.html, an about/index.html and a 404.html are enough; a static site generator such as Astro or Hugo is more fun. Keep it in a GitHub repository.

Milestone 1: a private bucket behind CloudFront, as code

Write one CloudFormation template, site.yaml, with:

  • An S3 bucket with Block Public Access fully on and no website hosting configured.
  • A CloudFront distribution whose origin is the bucket’s REST endpoint (<bucket>.s3.<region>.amazonaws.com), not the S3 website endpoint, using an Origin Access Control. Set ViewerProtocolPolicy: redirect-to-https, DefaultRootObject: index.html, and the AWS managed CachingOptimized cache policy.
  • A bucket policy that lets CloudFront read objects, but only on behalf of your distribution:
PolicyDocument:
  Statement:
    - Effect: Allow
      Principal:
        Service: cloudfront.amazonaws.com
      Action: s3:GetObject
      Resource: !Sub ${SiteBucket.Arn}/*
      Condition:
        StringEquals:
          AWS:SourceArn: !Sub arn:aws:cloudfront::${AWS::AccountId}:distribution/${Distribution}

That Condition is the important part: without it, any CloudFront distribution, including someone else’s, could read your bucket. It’s a resource-based policy, as in IAM.

Deploy it, then upload your site with aws s3 sync ./site s3://<bucket>/.

Done when:

  • https://<id>.cloudfront.net/ shows your home page.
  • http:// redirects to https://.
  • Requesting the object straight from S3 (https://<bucket>.s3.<region>.amazonaws.com/index.html) returns 403 Forbidden.

Milestone 2: URLs that work the way people expect

Try https://<id>.cloudfront.net/about/. It fails, because CloudFront only applies the default root object to /, not to subfolders. Fix it with a CloudFront Function on viewer requests that serves index.html for directory URLs, and redirects /about to /about/ so every page has one address:

function handler(event) {
  var request = event.request;
  var uri = request.uri;

  if (uri.endsWith('/')) {
    request.uri = uri + 'index.html';
    return request;
  }

  var lastSegment = uri.substring(uri.lastIndexOf('/') + 1);
  if (lastSegment.indexOf('.') === -1) {
    return {
      statusCode: 301,
      statusDescription: 'Moved Permanently',
      headers: { location: { value: uri + '/' } },
    };
  }

  return request;
}

Then add, both in the template:

  • A real 404 page. Because CloudFront isn’t allowed to list the bucket, S3 answers a missing object with 403, not 404. Map both 403 and 404 to /404.html with a 404 response code in the distribution’s custom error responses.
  • A response headers policy with security headers: Strict-Transport-Security, X-Content-Type-Options: nosniff, X-Frame-Options: DENY, a Referrer-Policy, and a Content-Security-Policy that allows only what your site uses.

Done when:

  • /about/ and /about both end up on your About page, and the address bar shows /about/.
  • curl -sI https://<id>.cloudfront.net/nope/ returns HTTP/2 404 and your 404 page’s content.
  • curl -sI https://<id>.cloudfront.net/ shows your security headers.

Milestone 3: deploy content, and cache it properly

Write a deploy.sh that uploads the site and makes changes visible quickly. The trick is two caching rules:

  • Files whose names change when their content changes (app.3f9a1c.js, the _astro/ folder in an Astro site) can be cached for a year: Cache-Control: public, max-age=31536000, immutable.
  • Everything else, especially HTML, gets a short cache, such as public, max-age=300, so updates show up within minutes.

Upload each group with aws s3 sync --cache-control ..., use --delete so removed pages disappear, and finish with aws cloudfront create-invalidation --paths "/*" so CloudFront fetches fresh copies. Read the bucket name and distribution ID from your stack’s outputs rather than hard-coding them.

Done when: you change a page, run ./deploy.sh, and see the change within a minute, while curl -sI on a hashed asset shows the one-year cache header.

Milestone 4: deploy from GitHub with no stored keys

Add a GitHub Actions workflow that runs your deploy script on every push to main, using the OIDC deploy role from the IAM lesson. Define the OIDC provider and role in the template too. Give the role exactly what the deploy needs:

  • s3:ListBucket on the bucket, and s3:PutObject and s3:DeleteObject on its objects.
  • cloudfront:CreateInvalidation and cloudfront:GetInvalidation on the distribution.
  • cloudformation:DescribeStacks on the stack, to read its outputs.

Done when:

  • A push to main updates the live site, and the repository has no AWS secrets.
  • From the workflow, aws s3 ls on any other bucket fails with AccessDenied.

Optional: your own domain

A domain costs money (registration, plus US$0.50 a month for a Route 53 hosted zone), so this part is optional. If you have one:

  • Request an ACM certificate with DNS validation. It must be in us-east-1, whatever your home Region, because that’s where CloudFront looks for certificates.
  • Add the domain to the distribution’s Aliases, and the certificate to its ViewerCertificate.
  • Create Route 53 alias records (A, and AAAA if you enable IPv6) pointing at the distribution.

Cost and clean-up

For a personal site this costs very little. S3 storage for a small site is cents a month, and CloudFront’s always-free allowance covers far more traffic than a new site gets. The domain, if you add one, is usually the biggest cost. Your budget from account setup will tell you if that changes.

To tear it down, empty the bucket first (aws s3 rm s3://<bucket> --recursive), because CloudFormation won’t delete a bucket with objects in it. Then delete the stack. Removing the CloudFront distribution takes a few minutes.

Going further

  • Do it the old way, then compare. Launch a small EC2 instance in a public subnet of the default VPC, install a web server and copy your site onto it. Compare what you had to look after (patching, a security group, a public IPv4 address that bills by the hour, no HTTPS without more work) with what CloudFront and S3 gave you. Then terminate it, and check its volume and public IP are gone. The VPC and EC2 lessons have what you need.
  • Turn on CloudFront standard logging to a second bucket, and see who visits.
  • Ready for more? The Serverless Builder path adds an API, a database and events behind a site like this one.
← All paths