avatarMatthew Brown

Summary

The web content provides a comprehensive guide on deploying a Gatsby site to an Amazon S3 bucket and serving it over HTTPS using Amazon CloudFront.

Abstract

The article outlines the process of deploying a Gatsby-built static website to an S3 bucket, configuring it for public access, and setting up HTTPS with CloudFront and an SSL certificate from AWS Certificate Manager. It also covers domain management using Route 53, including setting up a hosted zone and alias records. The guide emphasizes Gatsby's performance benefits and the convenience of using AWS services for hosting and domain management. It includes step-by-step instructions, screenshots, and code snippets for each part of the deployment process, from creating a new Gatsby site to configuring the necessary AWS services and running deployment scripts.

Opinions

  • The author praises Gatsby for its performance and the extensive library of plugins, themes, and starters available to developers.
  • AWS is recommended as a robust platform for hosting Gatsby sites, with S3 providing static website hosting and CloudFront enabling HTTPS and domain association.
  • The use of AWS services like Route 53, Certificate Manager, and S3 is depicted as streamlined and beneficial for setting up a domain, managing SSL certificates, and hosting static sites.
  • The author suggests that the described deployment pipeline facilitates quick updates and iterations of the site, enhancing the development workflow.
  • A personal endorsement is made for Gatsby, as the author mentions their own personal site built with the framework, implying satisfaction with its capabilities and results.

How to Deploy a Gatsby Site to an S3 Bucket

A quick guide to deploying a Gatsby site to an S3 website and serve over HTTPS using CloudFront.

Made by the author in Canva

In case you aren’t familiar with Gatsby, it is a great tool for building fast and performant websites. It is a static site generator that allows you to build sites using React. Gatsby also leverages GraphQL to allow you to easily combine data from multiple sources like a WordPress blog or a Shopify store.

The number one feature of Gatsby is definitely performance. Gatsby sites are served up to users in milliseconds and navigating around is designed to be a smooth and responsive experience. Another nice feature of using Gatsby is there is a large existing library of plugins, themes, and starters. You can check out Gatsby’s library of existing starters before you start a new site because there will likely exist a good starting point that aligns with your use case.

There are many options for hosting a Gatsby site, but in this article, I am going to demonstrate how to host from an S3 website and use CloudFront to hook up a domain and SSL certificate. To follow along fully with the example you will need to have an AWS account available to use.

Create a new Gatsby site

We will be walking through how to deploy a Gatsby site to S3, but first, we will need to generate a new one for us to have a project to work with. We can use this starter for a Ghost blog to get a new site up and going that we can then deploy to S3.

Run the following command in a terminal to generate the site.

$ npx gatsby new gatsby-starter-ghost https://github.com/TryGhost/gatsby-starter-ghost

This will take a bit to set everything up, so good opportunity to take a coffee break. Once this finishes up run the following commands to start the site running locally.

$ cd gatsby-starter-ghost
$ npx gatsby develop
Screenshot by the author

You will see some output similar to that above. Once it starts up you navigate to http://localhost:8000 where you can have a look at what our starter site looks like out of the box.

Screenshot by the author

AWS Setup

Now that we have an example site ready to deploy, we need to take care of some setup in the AWS console. This step is optional, but to hook up our site to a domain you will first need to register or have a domain already registered in Route 53.

Screenshot by the author

I am going to use an existing domain I am using and add a subdomain to it. Next, we need an SSL certificate that we can use for CloudFront later on. I’m going to create a wildcard certificate that I can use with any subdomain of my registered domain.

Navigate to Certificate Manager in the AWS console and request a new certificate if you don’t already have one ready to use.

Screenshot by the author

Add the domain name(s) for the certificate in step 1.

Screenshot by the author

Choose DNS validation for step 2.

Screenshot by the author

The last step is to confirm the domain you’ve entered and the next screen will give you the option to add the DNS record in Route 53 for you. This step is necessary to validate your certificate so that it can be issued.

Next, we need to navigate back to Route 53 to create a new hosted zone with the name of the domain or subdomain that we will be using.

Screenshot by the author
Screenshot by the author

This will generate a new zone which we will be coming back to once we set up our CloudFront distribution to add later.

We need to create a new S3 bucket next. So navigate to the S3 service in the AWS console and then click create a new bucket.

Screenshot by the author

Add a name to the bucket in the first section.

Screenshot by the author

In the second section below uncheck “Block all public access”. We will need this bucket to be publicly accessible. For the rest of the options, you can just leave them as the default.

Next, go to the Permissions tab of our new bucket and click Edit next to the Bucket policy section.

Screenshots by the author

We need to add the following JSON to the Bucket policy to make it truly publicly accessible. Be sure to change “Bucket-Name” to the name of your bucket.

Screenshot by the author

Switch over to the Properties tab and scroll down to the bottom to Static website hosting and click Edit.

Screenshot by the author

We need to enable static website hosting for the bucket.

Screenshots by the author

That should be all we need to set up within S3. Be sure to copy the Bucket website endpoint as we will need that in CloudFront next.

Navigate to CloudFront and create a new distribution.

Screenshot by the author

You will want to paste the website address from S3 in Origin Domain Name at the top of the form. I choose to Redirect HTTP to HTTPS and leave the rest as the defaults down until the Distribution Settings.

Screenshot by the author

We need to enter the domain we are connecting in Alternate Domain Names (CNAMEs). Also, we switch SSL Certificate to Custom SSL Certificate and choose the certificate we created earlier in Certificate Manager. That should be all we need to change, just hit create at the bottom.

Now we are ready to head back to Route 53 to add our CloudFront distribution to the hosted zone we created earlier. Create a new zone and flip the switch in the upper right to make this an alias record.

Screenshot by the author

Then choose Route traffic to Alias to CloudFront distribution.

Screenshot by the author

After this selection, your CloudFront domain should populate in the text box.

Screenshot by the author

We have completed quite a few steps of setup, but that should we be all we need to get our site going. In the next section, we will be deploying our example site to the S3 bucket we just created and we can validate all of our setups are complete.

Deploy to S3

Now that we are all set up we need to install the S3 plugin for our Gatsby site. Back in our site’s project run the following command to install the plugin.

$ npm i gatsby-plugin-s3

Once installed we need to add this to our gatsby-config.js

Then the last line we need to add is a deployment script in package.json

"scripts": {     
  ...     
  "deploy": "gatsby-plugin-s3 deploy" 
}

One last configuration step. Open up src/utils/siteConfig.js and update the site URL to the production domain name.

Run the following command to create a new build.

$ npx gatsby build

Note that before you run the deploy command you will need to make sure you have your AWS credentials configured with the AWS CLI or provide them with the command as below.

$ AWS_ACCESS_KEY_ID=xxxx AWS_SECRET_ACCESS_KEY=xxxx npm run deploy
or
$ npm run deploy

It will ask you to confirm so enter Y when prompted.

Note that on subsequent deployments you will need to create an invalidation for the CloudFront distribution so it serves the latest code.

Screenshots by the author

Now if we navigate to our domain we should be greeted with our Gatsby site. Notice how quick our site renders and feel free to navigate around.

Screenshot by the author

Conclusion

This completes our example. We now have a deployment pipeline set up to quickly serve up new versions of our site as we build and iterate our project. Gatsby is a powerful tool and highly recommended for static sites. For another example of a site built with Gatsby, you can check out my personal site. Thanks for reading!

More content at plainenglish.io

Coffee2021
JavaScript
Programming
AWS
Web Development
Recommended from ReadMedium