Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

The AWS SDK in Node.js cannot find any credentials

ErrorMissing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1

Short answer

The AWS SDK walks a fixed credential chain: code, environment variables, the shared files in ~/.aws, then role sources such as ECS or EC2. This error means every step came back empty. Find which source you meant to use, then set its variables or file correctly for the process that runs Node.js.

  • Applies to Node.js 22, AWS SDK for JavaScript v3, AWS SDK for JavaScript v2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check from the same shell and user that runs Node.js which AWS variables are set and whether the shared credentials file exists.

    env | grep '^AWS_' | sed 's/=.*/=<set>/'
    ls -l ~/.aws/credentials ~/.aws/config
  2. 2Match what you see, then jump to its fix

    1. 1The keys work in your terminal but not under PM2, systemd, Docker or a LambdaThe process does not see the environment variables you setFix
    2. 2No file at ~/.aws/credentials for the user running Node.js, or your keys sit under a named profileThe shared credentials file is missing, in the wrong place or under another profileFix
    3. 3Works on your laptop, fails on a server, container or functionThe code runs somewhere that should use a role, and no role source is availableFix

What does the error look like?

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

Cause 1 of 3

The process does not see the environment variables you set

1Check

Environment variables override values from the shared files, and they exist only for the shell, container or service that has them. List the variable names the process actually has.

env | grep '^AWS_' | sed 's/=.*/=<set>/'

2Fix

Set the variables where the process starts: in the service unit, the container env, or the Lambda configuration.

export AWS_ACCESS_KEY_ID=<your-key-id>
export AWS_SECRET_ACCESS_KEY=<your-secret>
export AWS_REGION=us-west-2

3Verify

Start Node.js from that same shell and call the service.

node -e "console.log(Boolean(process.env.AWS_ACCESS_KEY_ID))"

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 2 of 3

The shared credentials file is missing, in the wrong place or under another profile

1Check

The SDK reads ~/.aws/credentials (on Windows C:\Users\USER_NAME\.aws\credentials). It uses the [default] profile unless the AWS_PROFILE environment variable names another.

cat ~/.aws/credentials | sed 's/=.*/= <hidden>/'
echo "profile: ${AWS_PROFILE:-default}"

2Fix

Put the keys under [default], or set AWS_PROFILE to the profile that has them.

# ~/.aws/credentials
[default]
aws_access_key_id = <YOUR_ACCESS_KEY_ID>
aws_secret_access_key = <YOUR_SECRET_ACCESS_KEY>

# or choose a profile
export AWS_PROFILE=work-account

3Verify

Start Node.js again from that shell.

node script.js

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 3 of 3

The code runs somewhere that should use a role, and no role source is available

1Check

The v3 default chain also tries IAM Identity Center, web identity, ECS container credentials and the EC2 instance profile, in that documented order. If none is attached to the server, the chain ends empty.

echo "$AWS_PROFILE $AWS_ROLE_ARN"

2Fix

Attach an IAM role to the instance, task or function, and do not pass credentials in code. Leave the client without a credentials option so the default chain runs.

import { S3Client } from "@aws-sdk/client-s3";
const client = new S3Client({ region: "us-west-2" });

3Verify

Call one cheap API from the server.

node -e "import('@aws-sdk/client-s3').then(m=>new m.S3Client({region:'us-west-2'}).send(new m.ListBucketsCommand({}))).then(r=>console.log(r.Buckets.length))"

Confirmed by docs.aws.amazon.com

Did this fix it?

How do I stop it coming back?

  • Prefer roles and short-term credentials over long-lived keys in files.
  • Do not mix sources: a key in code, one in env and one in a file makes it unclear which wins.
  • Log the profile name and region at start-up (never the keys).

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

API Gateway gives up on a slow Lambda or Node.js backend

$context.integrationErrorMessage in the access log shows the integration error

API Gateway stops waiting for the backend at its integration timeout: 29 seconds by default on REST APIs, a fixed 30 seconds on HTTP APIs.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.