8

I configured .env file to have AWS credentials, it doesn't work.

in the docs, it is written the config will automatically be loaded from .env file. but it doesn't.

I tried to add the following

    aws.config.update({
    region: process.env.AWS_region,
    accessKeyId: process.env.AWS_ACCESS_KEY_ID,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
});

and that worked.

any idea why AWS SDK doesn't load the options automatically?

"aws-sdk": "^2.288.0",
"dotenv": "^6.0.0",
2
  • 1
    this is the link to the official docs it should work Commented Aug 8, 2018 at 9:50
  • 1
    I tried it but it doesn't work@AloAlo Commented Aug 8, 2018 at 9:50

3 Answers 3

6

Old question, but answering as I had this issue with a test.

This is due to the AWS SDK capturing the credentials when the sdk is first required or imported.

When you run dotenv.config(), it has already completed this and does not re-read the environment variables.

Updating the AWS config yourself sets the values and is a reasonable solution.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a bunch man! I started to think I was going insane. Thats quite stupid from the sdk to act like this IMO. Now give me back that hour of my life Bezos!
2

I had the same issue and then figured that I had to export the env variables in the shell profile (~/.zshrc in my case zsh - just add export AWS_ACCESS_KEY_ID=<key> and the same for other AWS vars ). Restarted the terminal console and then my node aws sdk was able to pick it up. If you are using node aws sdk, then I'd suggest print process.env.AWS_ACCESS_KEY_ID in your code to verify that your node code is indeed able to read the env variable in the first place. Hope that helps.

Comments

0

couldn't find an answer to help me, but I ran into an issue where AWS was not loading from dotenv where AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY were not loading from Dotenv. (using AWS SDK 3.0 for node/JS)

Dotenv does not by default overwrite any variables that are already set when it is loaded: https://www.npmjs.com/package/dotenv#-faq. See question: What happens to environment variables that were already set?

So.. to get dotenv to overwrite any values that may be existing, just add the override:true value when you import dotenv - i.e. require('dotenv').config({override:true}).

Then use the fromEnv method in @aws-sdk/credential-providers to load from your .env file: https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-credential-providers/#fromenv

Anyway, I'm not a professional developer, just someone figuring things out as I go. Hope this helps other people though.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.