Retrieving env variables from a file

Save your AWS tokens to disk with a .env file

AWS credentials (and other environment specific information) can be saved to a .env file. You can (partially!) follow the gatsby docs on the subject for this, but we'll summarize it here:

Add the following to your gatsby-config.js:


Then, add your AWS key & secret to a .env file: (don't check it into version control!)


(Optional:) If you wish to have the bucketName configured from your .env, this can now be done fairly easily adding the following to your .env:

// gatsby-config.js
plugins: [
      resolve: `gatsby-plugin-s3`,
      options: {
          bucketName: process.env.S3_BUCKET_NAME

That's it so far for what the gatsby docs recommend.

However! We're not entirely there yet, as the deploy phase needs access to this information as well. You can make dotenv run before the deploy script does by telling npx to require dotenv before executing the deploy script by swapping out the deploy script for following:


"scripts": {
- "deploy": "gatsby-plugin-s3 deploy",
+ "deploy": "npx -n \"-r dotenv/config\" gatsby-plugin-s3 deploy",

If you have any more custom settings you want to pass to dotenv (for example, you want a different file than .env to be loaded), these configurations need to be added as env vars to this step. See the dotenv docs on this subject.


Hey! 👋 Was this page helpful?

We're always looking to make our docs better, please let us know if you have any suggestions or advice about what's working and what's not!

© Jari Zwarts 2020