Deploying Gatsby to AWS
Tags: serverless, gatsby, react, awsamplify
Published: 2019-01-18 18:05:34

Gatsby is quickly becoming the tool of choice for developers looking to get up and running with new React applications.

> For a walkthrough of how to do this in just 56 seconds, check out the video [here](https://www.youtube.com/watch?v=cl1v4BkZj4w).

The combination of speed, smart default configuration, & ease of development that Gatsby delivers out of the box is a step forward from what we had before, similar to the jump that Create React App took us from manually configuring our webpack configurations in the early days.

In this post we'll learn how to deploy your next Gatsby site to AWS using the [Amplify Console](https://aws.amazon.com/amplify/console/).

## What is Amplify

Amplify started off as just a client framework but has progressed into much more. It now consists of a CLI, console, as well as the client framework.

The CLI allows you to create & configure new cloud services directly from your command line. The experience is similar to [Rails](https://rubyonrails.org) in the sense that you can scaffold these services & are given an opinionated set of configuration that you can then update to fit your needs.

Once you've created your cloud services, you can use the Amplify client to connect to & interact with these services.

Finally, when you're ready to launch your application the Amplify console provides a suite of tools around hosting & continuous deployment to get you on your way.

Let's take a look at how to combine these two technologies.

## Getting Started

To get started, we'll create a new Gatsby project using either the [Gatsby CLI](https://www.gatsbyjs.org/tutorial/part-zero/#install-git) or npx & then change into the new directory:

```sh
npx gatsby new GatsbyAmplify

cd GatsbyAmplify
```

Once the application is created, we'll create a new GitHub repo & push the project to the repo:

```sh
git init

git remote add origin git@github.com:<username>/<projectname>.git

git add .

git commit -m 'initial commit'

git push origin master
```

## Amplify Console

Now that the GitHub project has been created we can log into the [Amplify Console](https://console.aws.amazon.com/amplify/home).

From here, under Deploy we can click __GET STARTED__: 

![Amplify Console](https://thepracticaldev.s3.amazonaws.com/i/drl8ug8xk26u9ehswy8c.png)

Next, we'll choose GitHub as our repository & click __Next__.

![](https://thepracticaldev.s3.amazonaws.com/i/f1a2htgoy7o4shx09c8l.png)

Then connect the mater branch of the new repo we just created & click __Next__:

![](https://thepracticaldev.s3.amazonaws.com/i/7r24czb7oclvq3c9tmor.png)

In this view, we can review the default build settings & click __Next__ to continue:

![](https://thepracticaldev.s3.amazonaws.com/i/8zted3b8c1bubqiajbgg.png)

Finally, we can review the deployment & click __Save & Deploy__ when we're ready to deploy our app:

![](https://thepracticaldev.s3.amazonaws.com/i/s7yrdxphs4v8eesm2kh6.png)

Once the deployment is successful, you should see this:

![](https://thepracticaldev.s3.amazonaws.com/i/gfcp2lz8sald97w4tpxy.png)

To view details of the deployment, click on the name of the branch (in our case, __master__).

In this view, you can see details about the deployment including a link to view the app & screenshots of the app on different devices. 

![](https://thepracticaldev.s3.amazonaws.com/i/igjh7xyndmypatekkq2p.png)

## Kicking off a new build

Now that our app is deployed, let's take it one step further. The Amplify Console will automatically watch your master branch & kick off a new build whenever new code is merged. Let's test this out.

Open `src/pages/index.js` & replace the following line:

```html
<h1>Hi people</h1>
```

with this:

```html
<h1>Hello from Amplify</h1>
```

Save the file & push the changes to your master branch:

```sh
git add .

git commit -m 'updated heading'

git push origin master

```

Now, when we go back into the Amplify console we'll see that a new build has been started:

![](https://thepracticaldev.s3.amazonaws.com/i/kq0sx5azsx3psg8p3c1v.png)

When the build is completed & we launch the app, we should now see our new heading:

![](https://thepracticaldev.s3.amazonaws.com/i/jwnri28j0t5iasbsq8qc.png)


## Next Steps

Now that you've gotten the hang of working with the Amplify Console, what are some next steps?

If you're interested in adding authentication to your Gatsby app, check out [this](https://github.com/dabit3/gatsby-auth-starter-aws-amplify) Gatsby Auth Starter I published.

If you're interested in adding new features to your Gatsby app (like authentication or a GraphQL API), check out the [Amplify CLI & Amplify Client libraries](https://aws-amplify.github.io/).

You may also be interested in working with multiple environments or teams. If so, check out [the documentation](https://aws-amplify.github.io/docs/cli/multienv?sdk=js) on how to get up & running with teams or have a look at [this post](https://read.acloud.guru/multiple-serverless-environments-with-aws-amplify-344759e1be08) that I wrote.

> My Name is [Nader Dabit](https://twitter.com/dabit3). I am a Developer Advocate at Amazon Web Services working with projects like [AWS AppSync](https://aws.amazon.com/appsync/) and [AWS Amplify](https://aws-amplify.github.io/). I specialize in cross-platform & cloud-enabled application development.