1

I have a typescript/node-based application where the following line of code is throwing an error:

const res = await s3.getObject(obj).promise();

The error I'm getting in terminal output is:

❌ Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1

 CredentialsError: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1

However, I do actually have a credentials file in my .aws directory with values for aws_access_key_id and aws_secret_access_key. I have also exported the values for these with the variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. I have also tried this with and without running export AWS_SDK_LOAD_CONFIG=1 but to no avail (same error message). Would anyone be able to provide any possible causes/suggestions for further troubleshooting?

Sabo Boz
  • 1,683
  • 4
  • 13
  • 29

2 Answers2

1

Install npm i dotenv Add a .env file with your AWS_ACCESS_KEY_ID etc credentials in. Then in your index.js or equivalent file add require("dotenv").config();

Then update the config of your AWS instance:

  region: "eu-west-2",
  maxRetries: 3,
  httpOptions: { timeout: 30000, connectTimeout: 5000 },
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
});
highrankin
  • 87
  • 9
0

Try not setting AWS_SDK_LOAD_CONFIG to anything (unset it). Unset all other AWS variables. In Mac/linux, you can do export | grep AWS_ to find others you might have set.

Next, do you have AWS connectivity from the command line? Install the AWS CLI v2 if you don't have it yet, and run aws sts get-caller-identity from a terminal window. Don't bother trying to run node until you get this working. You can also try aws configure list.

Read through all the sections of Configuring the AWS CLI, paying particular attention to how to use the credentials and config files at $HOME/.aws/credentials and $HOME/.aws/config. Are you using the default profile or a named profile?

I prefer to use named profiles, but I use more than one so that may not be needed for you. I have always found success using the AWS_PROFILE environment variable:

export AWS_PROFILE=your_profile_name   # macOS/linux
setx AWS_PROFILE your_profile_name     # Windows
$Env:AWS_PROFILE="your_profile_name"   # PowerShell

This works for me both with an Okta/gimme-aws-creds scenario, as well as an Amazon SSO scenario. With the Okta scenario, just the AWS secret keys go into $HOME/.aws/credentials, and further configuration such as default region or output format go in $HOME/.aws/config (this separation is so that tools can completely rewrite the credentials file without touching the config). With the Amazon SSO scenario, all the settings go in the config.

ErikE
  • 48,881
  • 23
  • 151
  • 196
  • I do not have AWS_PROFILE set to anything, but if I run something like ```aws s3 ls``` I can see the s3 buckets fine – Sabo Boz Dec 21 '21 at 03:21
  • Then it should work in node. Have you validated that you are setting environment variables appropriately before running node? Have you reviewed the credentials vs. config files that I mentioned? Have you ensured all other `AWS_` environment variables have been unset? Are you running in a Docker container? – ErikE Dec 21 '21 at 08:03
  • I think I figured out the idea - before running my node code I'm actually using a tool to switch credentials (and thus setting new values for ```AWS_ACCESS_KEY_ID``` and ```AWS_SECRET_ACCESS_KEY```), and these are the values I need (not the ones in the config file). Is there any way to make it so if these variables are set, ignore whatever's in the config file? – Sabo Boz Dec 23 '21 at 11:01
  • Those variables should override what's in the config file. This is discussed in the docs I linked above. However, the behavior could be changed by some of the other environment variables such as `AWS_SDK_LOAD_CONFIG` which I mentioned as well. Are you sure that in your node environment, those two environment variables are set correctly, and no others are set that could alter the way the config is being interpreted? – ErikE Dec 27 '21 at 20:15