---
title: "How to set up deploy previews with AWS and GitHub Actions."
description: "Learn to set up deploy previews with AWS and GitHub Actions. Automate preview builds for pull requests to streamline feedback and continuous deployment."
url: "https://significa.co/blog/deploy-previews-aws"
datePublished: "2020-02-01T22:05:00.000Z"
dateModified: "2026-10-06T09:14:14.938Z"
---

1 Feb 2020 · Updated 6 Oct 2026 · 5 min read

# Deploy Previews with AWS and Github Actions.

[Development](https://significa.co/blog?t=Development)  [Learning](https://significa.co/blog?t=Learning)

Build Netlify-like Deploy Previews with AWS and Github Actions.

![Deploy previews with AWS illustration cover.](https://a.storyblok.com/f/198185/3000x1500/459b44e37f/cover_deploy.png/m/1440x0/)

We love [AWS](https://aws.amazon.com/), and it's, without a doubt, our go-to choice for building our infrastructures. It's flexible, powerful and very capable but – if we're honest – sometimes it's a bit daunting. The ease of use of services like [Netlify](https://www.netlify.com/) or [Heroku](https://heroku.com/) can go a long way to improve the overall [developer experience](https://medium.com/@albertcavalcante/what-is-dx-developer-experience-401a0e44a9d9).

\
One of the things we missed the most from Netlify was its [Deploy Previews](https://www.netlify.com/blog/2016/07/20/introducing-deploy-previews-in-netlify/). For marketing websites, there is no better way to quickly iterate: as soon as there is an open _Pull Request_ against our master branch, we can gather feedback, do proper Quality Assurance sessions and simply keep on improving the current branch with all-team collaboration instead of the usual "code review first \> merge to a staging branch \> gather feedback \> new branch \> rinse and repeat".

That's why we needed to bring it to AWS. This quick tutorial is an example of how to achieve this behaviour with Github Actions and AWS for a [Gatsby](https://www.gatsbyjs.org/) website (although it could easily be adapted to something else).

## Main goal

When a Pull Request is opened, or someone commits to it, we should make a new build, deploy it to `somedomain.com/preview/<pr-number>` and post back the URL as a comment.

![Github action](https://a.storyblok.com/f/198185/1296x362/5bffab3a1c/github_actions.png/m/0x0/)

## AWS

This tutorial is not about setting up AWS to host websites, so we'll just go through it as quickly as possible. We recommend automating these steps with a [CloudFormation](https://aws.amazon.com/cloudformation/) template, but for the sake of simplicity, we'll just go over it in the console.

You're going to need at least an **S3 Bucket** for public web hosting and an **IAM user** with the necessary permissions. We recommend at least setting up Route 53 for a custom domain and - if you want HTTPS - Cloudfront with a certificate from Certificate Manager.

Feel free to skip this part if you already have what you need.

### S3 Bucket

- Go to S3 and click on _\+ Create Bucket_. If you're not planning on adding HTTPS, **make sure the DNS-compliant name of the bucket is the domain you wish to use** (e.g., [somedomain.com](https://somedomain.com/)). This way, we can create a record directly to this bucket on Route 53.
- Go over to step 3 of the modal and **unselect** _Block all public access_.
- After the bucket is created, go over to _Properties_, then to _Static website hosting_ and select _Use this bucket to host a website_. Your index and error documents probably are index.html and 404.html, respectively.
- Go over to _Permissions_, then to _Bucket Policy,_ and paste this policy (changing the bucket name).

```javascript
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::YOUR_BUCKET_NAME/*"
        }
    ]
}
```

### Deploy Preview Expiration

Optionally, you can also make S3 automatically delete your Deploy Previews after a while:

- Go to _Management_ \> _Lifecycle_ and click _\+ Add lifecycle rule_;
- Give it a name (e.g., _Deploy Preview Expire_);
- In the _Prefix/tags filter_, add `preview/` as a prefix;
- Skip transitions and in the Expiration tab, tick all the boxes and add the desired values.

### Route 53

If you want to route directly to the bucket without HTTPS, you just need to create a new A Record Set for your bucket:

- Go over to Route53 and open your hosted zone;
- Add a new _A Record Set_;
- Select _Alias_;
- Select your bucket from the S3 list.

### Cloudfront and HTTPS (Optional)

If you want HTTPS, you must first request a Certificate from Certificate Manager.

Then, go over to Cloudfront, create a new Web Distribution and:

- Paste your bucket public URL in _Origin Domain Name_;
- Select _Redirect HTTP to HTTPS_;
- Given we're just using CloudFront for the HTTPS, select _Customize_ in _Object Caching_ and make them all 0;
- Select _Yes_ for _Compress Objects Automatically_;
- Add your desired CNAMEs and select your newly created Certificate.

Finally, go over to Route 53 and change the value of the recently created A Record to point to CloudFront instead of S3.

## Relative links and assets

Most times, websites are built to be deployed to the root of the domain but here we have a problem: given we are not deploying to `/`, our relative links and links to resources (e.g., images) won't work.

The first thing we thought of was using the [HTML5 base tag](https://www.w3schools.com/tags/tag_base.asp). Luckily we didn't have to (feel free to try it anyway and let us know your results!)

Instead of having to conditionally add some markup, Gatsby provides a very helpful [Path Prefix feature](https://www.gatsbyjs.org/docs/path-prefix/) that does exactly what we were looking for. Our path prefix needs to change with every PR, so we just leveraged environment variables to define it.

```javascript
// gatsby.config.js
module.exports = {
  pathPrefix: process.env.PATH_PREFIX,
  ...
}
```

For Gatsby to look at this path, we just need to build the website with `gatsby build --prefix-paths`. Perfect!

## Github Actions

We'll use [jakejarvis/s3-sync-action](https://github.com/jakejarvis/s3-sync-action) to deploy and [pbrandone/create-status-action](https://github.com/pbrandone/create-status-action) to add the deployed URL as a commit status.

The configuration for the workflow is very straightforward. We just have remember to build with `PATH_PREFIX=preview/${{ github.event.number }} npm run build -- --prefix-paths`

Just make sure you have the necessary environment variables in your repo's _Settings_ \> _Secrets_ and you are good to go!

```javascript
name: Deploy Preview

on: pull_request

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1

      - name: Cache npm
        uses: actions/cache@v1
        with:
          path: ~/.npm
          key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-

      - name: Set deployment status
        uses: pbrandone/create-status-action@master
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          state: pending
          description: Preparing deploy preview
          context: Deploy Preview URL

      - name: Install
        run: npm ci

      - name: Build
        run: PATH_PREFIX=preview/${{ github.event.number }} npm run build -- --prefix-paths

      - name: Deploy
        if: success()
        uses: jakejarvis/s3-sync-action@master
        with:
          args: --delete
        env:
          AWS_S3_BUCKET: YOUR_BUCKET_NAME # Could also come from github secrets
          AWS_ACCESS_KEY_ID: ${{ secrets.DEPLOY_PREVIEW_KEY }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.DEPLOY_PREVIEW_SECRET }}
          AWS_REGION: "eu-west-1"
          SOURCE_DIR: "public"
          DEST_DIR: preview/${{ github.event.number }}

      - name: Set success deployment status
        if: success()
        uses: pbrandone/create-status-action@master
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          state: success
          description: Deploy preview ready!
          url: https://yourdomain.com/preview/${{ github.event.number }}
          context: Deploy Preview URL

      - name: Set failed deployment status
        if: failure()
        uses: pbrandone/create-status-action@master
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          state: failure
          description: Failed to deploy preview
          context: Deploy Preview URL
```

Feel free to check our website's open-source repo and see it in the wild:

[_https://github.com/significa/significa.co_](https://github.com/significa/significa.co)

### Update

In this [tweet](https://twitter.com/swyx/status/1238488523319246849?s=20), [@swyx](https://twitter.com/swyx) and [@sebastienlorber](https://twitter.com/sebastienlorber) were kind enough to correctly point out that commenting on the deployed URL per commit will easily clutter the Pull Request's discussion.

The snippet above was updated to use GitHub Status instead of commenting.

![Github action](https://a.storyblok.com/f/198185/1590x632/d3e8cf4c03/image_deploy_previews.png/m/0x0/)

If you still want to go with the comments approach with [unsplash/comment-on-pr](https://github.com/unsplash/comment-on-pr) to post the URL comment back to the PR, this is what we had:

```javascript
- name: Deploy message
  if: success()
  uses: unsplash/comment-on-pr@master
  env:
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  with:
    msg: |
      Deploy preview ready!
      https://yourdomain.com/preview/${{ github.event.number }}
      built from ${{ github.sha }}
```

Pedro Brandão

Founder and Front-end Developer

[Author page](https://significa.co/about/pedro-brandao)

Pedro el patron Brandão is the Founder of Significa. Pedro’s playlist is made entirely of songs no one has ever listened to.
