🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Deployment••By Komal Nakrani

Deploy Astro to S3 and CloudFront Step by Step

Deploy Astro to S3 and CloudFront: a private bucket with origin access control, a CloudFront Function for index.html URLs, cache headers, and invalidations on deploy.

#Astro#AWS#S3#CloudFront#Deployment

To deploy Astro to AWS, build the site, upload dist/ to a private S3 bucket with aws s3 sync, and serve it through a CloudFront distribution that reads the bucket via origin access control. Add a small CloudFront Function that appends index.html to directory URLs, because S3 behind CloudFront does not do that for you, then invalidate the cache after each deploy.

Key Takeaways

  • Keep the bucket private; CloudFront reads it through origin access control (OAC).
  • Astro’s /about/ routes are about/index.html files; a viewer-request function maps one to the other.
  • Upload /_astro/ with a one-year immutable cache and HTML with no-cache.
  • Invalidate /* after each deploy, or only HTML paths if you deploy often.

1. Build

# Terminal
npm run build

Set site in astro.config.mjs to the domain CloudFront will serve, so canonicals and the sitemap are correct. Current Astro releases need Node.js 22.23.4 or newer on the machine that builds.

2. Create the bucket and distribution

In the AWS console:

  1. Create an S3 bucket with Block all public access turned on. Do not enable static website hosting; CloudFront reads the bucket as a regular origin.
  2. Create a CloudFront distribution with the bucket as origin and Origin access control selected. Let CloudFront update the bucket policy, or copy the policy it shows.
  3. Set Default root object to index.html.
  4. Under Error pages, map 403 and 404 responses to /404.html with a 404 status. A private bucket returns 403 for missing keys.
  5. Attach an ACM certificate (issued in us-east-1) and add your domain as an alternate name.

The default root object only applies to /. A request for /about/ still asks S3 for the key about/, which does not exist. The next step fixes that.

3. Add a CloudFront Function for directory URLs

Create a CloudFront Function and associate it with the distribution’s Viewer request event:

// cloudfront-function: astro-index-rewrite
function handler(event) {
  var request = event.request;
  var uri = request.uri;

  if (uri.endsWith('/')) {
    request.uri = uri + 'index.html';
  } else if (!uri.split('/').pop().includes('.')) {
    request.uri = uri + '/index.html';
  }
  return request;
}

/about/ becomes about/index.html, and /about (no extension) is mapped the same way. Requests for files with an extension pass through untouched. CloudFront Functions use a restricted JavaScript runtime, which is why this uses var and simple string methods.

4. Upload with the right cache headers

Upload hashed assets and HTML separately so each gets its own Cache-Control:

# Terminal
BUCKET=s3://my-astro-site

aws s3 sync dist/_astro "$BUCKET/_astro" \
  --cache-control "public, max-age=31536000, immutable" --delete

aws s3 sync dist "$BUCKET" --exclude "_astro/*" \
  --cache-control "public, max-age=0, must-revalidate" --delete

aws s3 sync sets content types from file extensions. The first command uploads files that never change; the second uploads HTML and everything from public/. Our cache headers tip explains the policy.

5. Invalidate on deploy

# Terminal
aws cloudfront create-invalidation --distribution-id E123EXAMPLE --paths "/*"

A /* invalidation counts as one path. Hashed assets never need invalidating, so the main reason for this step is HTML that CloudFront cached at the edge.

Automate it

Put the build, both sync commands and the invalidation in a CI job. With GitHub Actions, use aws-actions/configure-aws-credentials with an OIDC role instead of long-lived access keys. Our GitHub Actions workflows guide shows the job layout, and deploying Astro compares AWS with managed static hosts, which handle all five steps for you. Themes from our Astro theme catalog build to static files, so they deploy this way without an adapter. The static deploy docs cover the other hosts.

FAQ

Why does my Astro site on CloudFront return 403 for /about/?

S3 has no key named about/. Add a viewer-request CloudFront Function that rewrites directory URLs to index.html, as shown above, and map 403 errors to your 404.html.

Should I use S3 static website hosting for Astro?

Not with CloudFront and a private bucket. Website hosting needs a public bucket and does not support origin access control. Use a regular S3 origin with OAC and the rewrite function instead.

How do I set cache headers for Astro on S3?

Pass --cache-control to aws s3 sync. Upload _astro/ with a one-year immutable policy and the rest with max-age=0, must-revalidate.

Do I need an adapter to deploy Astro to S3?

No. A static Astro build is plain files. Adapters are only needed for on-demand rendering, which S3 cannot run.