For Gatsby developers who desire deeper interaction with their deployments, Amazon S3 is a lower-level alternative to AWS Amplify. An object storage provider offered by AWS, Amazon S3 also provides an integration with Amazon CloudFront, a global CDN. Once you have an account with access to the AWS Console, you can install the AWS CLI and configure it to authenticate into your account with your AWS key and secret:
$
pip
install
awscli
$
aws
configure
The most direct way to deploy your Gatsby site to S3 is to use the Gatsby S3 plugin, which provides configuration options for your S3 bucket:
$
npm
install
gatsby-plugin-s3
Configure your newly installed plugin in gatsby-config.js with information about your S3 bucket:
// gatsby-config.js
plugins
:
[
{
resolve
:
`gatsby-plugin-s3`
,
options
:
{
bucketName
:
"my-s3-bucket-name"
,
},
},
]
Then, add a deploy script to your Gatsby site’s package.json file by providing a deploy command using the Gatsby S3 plugin:
//
package.json
"scripts"
:
{
"deploy"
:
"gatsby-plugin-s3 deploy"
}
Once you’ve configured the plugin and this NPM script, you can execute the following chained commands to deploy the site to your S3 bucket:
$
npm
run
build
&&
npm
run
deploy
NOTE
For more information about hosting Gatsby on S3, consult the Gatsby documentation’s guide to using Gatsby with S3, which includes information about using environment variables and setting up CloudFront. Also consult the S3 documentation.
Leave a Reply