6

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",
Allloush
  • 1,140
  • 8
  • 27
  • 1
    this is the link to the official [docs](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-environment.html) it should work – Alo Alo Aug 08 '18 at 09:50
  • 1
    I tried it but it doesn't work@AloAlo – Allloush Aug 08 '18 at 09:50

2 Answers2

5

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.

Mark Smithson
  • 51
  • 2
  • 2
  • 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! – Felipe Oct 25 '22 at 21:21
1

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.

juve stig
  • 21
  • 3