Enables you to deploy your gatsby site to a S3 bucket. Requires very little configuration, while optimizing your site as much as possible.
Install the plugin:
npm i gatsby-plugin-s3
Add it to your gatsby-config.js
& configure the bucket name (required)
plugins: [
{
resolve: `gatsby-plugin-s3`,
options: {
bucketName: 'my-website-bucket',
},
},
];
There are more fields that can be configured, see below.
Add a deployment script to your package.json
"scripts": {
...
"deploy": "gatsby-plugin-s3 deploy"
}
Optionally you can skip the confirmation prompt automatically by adding --yes
like so:
"deploy": "gatsby-plugin-s3 deploy --yes"
When gatsby-plugin-s3
detects a CI environment, it will automatically skip this prompt by default.
After configuring credentials (see below), you can now execute npm run build && npm run deploy
to have your site be build and immediately deployed to S3.
A couple of different methods of specifying credentials exist, the easiest one being using the AWS CLI:
# NOTE: ensure python is installed
pip install awscli
aws configure
If you don't want to have your credentials saved globally (i.e. you're dealing with multiple tokens on the same environment), they can be set as environment variables, for example:
AWS_ACCESS_KEY_ID=xxxx AWS_SECRET_ACCESS_KEY=xxxx npm run deploy
Additionally, these can be set in a local .env
file too, but this requires a bit more setup work. See the recipe here.
As mentioned above, the bucketName
is required, but there's much more to configure (add to the options
object) as you please:
Property | Type | Default | Description |
---|---|---|---|
bucketName |
string |
n/a | Your bucket name (required) |
bucketPrefix |
string \| undefined |
undefined |
An optional prefix/directory to use on the bucket. This requires the bucket to already be created. Do not include leading or trailing slashes. Can be useful with CloudFront originPath option. |
region |
string \| undefined |
Whatever the AWS SDK decides is the default otherwise. | Your region. |
protocol |
'http' \| 'https' \| undefined |
undefined |
The protocol of your site. If you are using a CDN or reverse-proxy (such as CloudFront) in front of S3. then you must fill out this and the hostname field to ensure redirects work correctly. If you are just using your S3 website directly, this is unnecessary. |
hostname |
string \| undefined |
undefined |
The hostname of your site. See above. |
params |
Params \| undefined |
Depending on your mergeCachingParams setting, either an empty object or the recommended headers. |
Custom params to apply to your files |
acl |
string \| null \| undefined |
"public-read" |
Define bucket ACL. If you don't want to use an ACL, set this to null |
mergeCachingParams |
boolean \| undefined |
true |
Enable Gatsby recommended caching settings |
generateRoutingRules |
boolean \| undefined |
true |
The plugin will generate routing rules to be applied to the website config for all redirects it can find. |
generateRedirectObjectsForPermanentRedirects |
boolean \| undefined |
false (until 1.0) |
The plugin will not generate routing rules for permanent (301) redirects, but will instead upload empty objects with the x-amz-website-redirect-location property. This can be used to get around the hard limit of 50 routing rules on AWS S3. |
generateIndexPageForRedirect |
boolean \| undefined |
true |
The plugin will create a fake index page if a redirect from the root path is made - as a workaround, because routing rules can't be applied in that situation. |
generateMatchPathRewrites |
boolean \| undefined |
true |
Generate rewrites for client only paths |
removeNonexistentObjects |
boolean \| undefined |
true |
Remove S3 objects if they no longer exist locally |
retainObjectsPatterns |
string array \| undefined |
[] |
An array of file globs that should not be removed from the S3 bucket if the file does not exist locally, this does nothing unless removeNonexistentObjects is true. See here for glob format |
customAwsEndpointHostname |
string \| undefined |
amazonaws.com |
Custom AWS S3 endpoint. See our docs for all available options |
enableS3StaticWebsiteHosting |
boolean \| undefined |
true |
Disables modifications to the S3 Static Website Hosting configuration. Without S3 Static Website Hosting some features (index.html rewriting, trailing slash redirects, and serverside redirects), will not function. Not recommended, but could be useful for preventing Cloud formation Stack Drift or suppressing Terraform noise if you don't need, the static website hosting functionality. |
parallelLimit |
number \| undefined |
20 |
Max number of files to upload in parallel. |
maxRetries |
number \| undefined |
undefined |
The maximum amount of retries to perform for a service request. |
connectTimeout |
number \| undefined |
undefined |
The maximum time in milliseconds that the connection phase of the request should be allowed to take. This only limits the connection phase and has no impact once the socket has established a connection. |
timeout |
number \| undefined |
120000 |
Sets the socket to timeout after the specified amount of milliseconds of inactivity on the socket. |
fixedRetryDelay |
number \| undefined |
undefined |
By default an exponential backoff is used for retryable failures. Use this option to use a fixed retry delay instead of exponential for particularly flaky connections |
verbose |
boolean \| undefined |
false |
Whether or not the plugin should output verbose logs from S3 uploads |
Several recipes are available:
Learn how to retrieve AWS credentials from a .env file. Additionally setup a different bucket name depending on your environment.
Learn how to override the content type gatsby-plugin-s3 sets on your files.
CloudFront is a global CDN and can be used to make your blazing fast Gatsby site load even faster, particularly for first-time visitors. Additionally, CloudFront provides the easiest way to give your S3 bucket a custom domain name and HTTPS support.
Serverless can be used in combination with gatsby-plugin-s3, swapping the plugin's deployment step for sls deploy
instead.
Serverless will give you the added advantage of being able to add multiple AWS services such as Lambda, CloudFront, and more all in the same repo, deployment step and CloudFormation stack while still being able to profit from all the optimisations gatsby-plugin-s3 does.
with-serverless
exampleRouting traffic from gatsby-plugin-s3 during the deployment through a http proxy can be done with a env var.
Using Yandex, DigitalOcean, or any other S3-compliant storage service together with gatsby-plugin-s3
You can deploy your site to a prefix, leaving all other data in the bucket intact.
gatsby-plugin-s3
respects the pathPrefix
gatsby option with no additional setup needed for this plugin, so you can follow the guide in the gatsby docs.
AWS S3 has a limit of 50 Routing Rules that can be applied to a bucket. Unfortunately this limits
the number of 302 (temporary) redirects you can create. For 301 (permanent) redirects, a way to get around the limit is
setting the x-amz-website-redirect-location
header on an empty object.
To enable this behavior, set the generateRedirectObjectsForPermanentRedirects
configuration option to true
.
Please see CONTRIBUTING.md for instructions on how to set up your development environment.