Deploying the Blog with SST v4

From git merge to a static site on S3 and CloudFront: the whole deployment, in about fifty lines of config.

The words Astro + SST in neon above a pipeline: astro build, sst deploy, S3, CloudFront and you

When I moved this blog off Ghost, the hosting part was the one I wasn't worried about. I'd already deployed a few side projects to AWS with SST, including my consultancy site and the website for Aionos, and a blog that's nothing but static files is about as simple as it gets.

Here's the whole journey from merging a pull request to the page you're reading:

Merge to main goes to GitHub Actions, then sst deploy builds the site and uploads it to an S3 bucket, which CloudFront serves through Route 53 to the visitor
Merge → GitHub Actions → sst deploy → S3 → CloudFront → you

The whole config

SST describes your infrastructure in TypeScript, in a file called sst.config.ts. For this blog, it's one component: a StaticSite, which is an S3 bucket with CloudFront in front of it.

export default $config({
  app(input) {
    return {
      name: 'mrstebo-blog',
      removal: input?.stage === 'prod' ? 'retain' : 'remove',
      protect: ['prod'].includes(input?.stage),
      home: 'aws',
      providers: { aws: { region: 'eu-west-2' } },
    };
  },
  async run() {
    const { loadRedirects, redirectInjection, securityHeadersInjection } = await import('./infra/redirects');

    const site = new sst.aws.StaticSite('Blog', {
      build: { command: 'npm run build', output: 'dist' },
      errorPage: '404.html',
      domain:
        $app.stage === 'prod'
          ? { name: 'www.mrstebo.co.uk', redirects: ['mrstebo.co.uk'], dns: sst.aws.dns({ override: true }) }
          : undefined,
      assets: { fileOptions: [ /* cache headers, below */ ] },
      edge: {
        viewerRequest: { injection: redirectInjection(loadRedirects()) },
        viewerResponse: { injection: securityHeadersInjection },
      },
    });

    return { url: site.url };
  },
});

Let's go through it.

Stages

SST deploys to stages: separate copies of everything, side by side in the same AWS account. prod is the real site. Anything else (dev, steven, try-the-new-header...) is a throwaway copy on its own CloudFront URL.

The two lines at the top protect the real one:

  • removal: 'retain' means if I ever run sst remove --stage prod, the S3 bucket is kept rather than deleted.
  • protect: true stops SST deleting anything in prod at all, even by accident.

Every other stage is remove, so tidying up after an experiment is one command:

npx sst deploy --stage dev   # your own copy, on a private CloudFront URL
npx sst remove --stage dev   # and gone again

The domain

Only prod gets a domain. www.mrstebo.co.uk is the real address (as it was on Ghost), and the bare mrstebo.co.uk redirects to it. SST creates the HTTPS certificate in ACM, validates it and adds the DNS records in Route 53 for me.

dns: sst.aws.dns({ override: true }) deserves a mention. Coming from Ghost, Route 53 already has records pointing at Ghost's servers. override lets SST take those records over instead of failing because they already exist, so switching from Ghost to the new site is a single deploy.

Cache headers

Every file is uploaded to S3 with its own Cache-Control header. Later entries win:

fileOptions: [
  { files: '**', cacheControl: 'max-age=3600,public' },
  { files: '_astro/**', cacheControl: 'max-age=31536000,public,immutable' },
  { files: ['**/*.xml', '**/*.txt'], cacheControl: 'max-age=300,public' },
  { files: 'pagefind/**', cacheControl: 'max-age=300,public' },
  { files: '**/*.html', cacheControl: 'max-age=0,no-cache,no-store,must-revalidate' },
],
  • _astro/ is where Astro puts CSS, JavaScript and every optimised image, and each file has a hash in its name (brain-with-data-flowing.uHeW0-TL.webp). If it changes, its name changes, so it can be cached for a year.
  • HTML is never cached, so a new post shows up the moment the deploy finishes.
  • Feeds, sitemaps and the search index get five minutes, because their names don't change.

Edge functions

CloudFront can run tiny bits of JavaScript (a CloudFront Function) on every request, right at the edge. SST already uses one to map /about-me/ to /about-me/index.html, and it lets you inject your own code at the start of it.

I use that for two things:

  1. Redirects for old Ghost URLs. Ghost had a few addresses the new site doesn't build: the /rss/ feed, a feed for every tag, Ghost's own sitemaps, /amp/ versions of posts and an author page. Each one now gets a 301 to its new home, and any one-off redirects live in content/redirects.json.
  2. Security headers on every response: HSTS, nosniff, a referrer policy and frame options.
export function redirectInjection(exact: Record<string, string>): string {
  return `
  {
    const uri = event.request.uri;
    const exact = ${JSON.stringify(exact)};
    let to = exact[uri] || exact[uri + '/'];
    if (!to) {
      if (uri === '/rss' || uri === '/rss/' || /^\\/(tag|author)\\/[^/]+\\/rss\\/?$/.test(uri)) to = '/rss.xml';
      // ...sitemaps and /amp/ pages too
    }
    if (to) {
      return { statusCode: 301, statusDescription: 'Moved Permanently', headers: { location: { value: to } } };
    }
  }`;
}

Because it's just a string of code, I could test it locally by wrapping it in a function and throwing every URL from Ghost's sitemaps at it. All 346 of them either exist on the new site or redirect.

Deploying from GitHub Actions

I don't deploy from my laptop. Every pull request runs a check and a full build (so a broken post or a missing image fails the PR), and merging to main deploys:

jobs:
  deploy:
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: read
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '24'
          cache: npm
      - run: npm ci
      - run: npm run check

      - name: Build
        if: github.ref != 'refs/heads/main'
        run: npm run build

      - name: Configure AWS credentials
        if: github.ref == 'refs/heads/main'
        uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
          aws-region: eu-west-2

      - name: Deploy to AWS
        if: github.ref == 'refs/heads/main'
        run: npx sst deploy --stage prod

The important line is id-token: write. Instead of storing an AWS access key in GitHub, the workflow uses OIDC: GitHub proves to AWS which repository and branch it's running for, and AWS hands back short-lived credentials for a role that only trusts this repo's main branch. There's no long-lived secret to leak, and nothing to rotate.

sst deploy runs npm run build itself (Astro, then Pagefind for the search index), uploads dist/ to S3, updates CloudFront, and invalidates the cache so the change is live straight away.

Was it worth it?

For a static blog, absolutely. The whole deployment is one config file, one workflow and one small module of edge code, all in the same repo as the posts. There's no server to patch, no database to back up, and nothing to log in to.

And if I want to try something risky? npx sst deploy --stage dev, have a play, npx sst remove --stage dev. That's my favourite part 😄