Maaz Zindani

Loading the index…

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

AWS SDK says the security token included in the request is invalid

ErrorUnrecognizedClientException: The security token included in the request is invalid

Short answer

The call reached AWS but the credentials it carried were refused. In Node.js the usual reasons are temporary credentials that expired, old access keys in environment variables that override your profile, or a global STS session token used in an opt-in Region. Print the identity the SDK resolves, then fix that one.

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

Fix it fast

  1. 1Find out which cause you have

    Ask AWS who the credentials in this shell belong to. If this call fails too, the problem is the credentials, not your Node code.

    env | grep -E "^AWS_(ACCESS_KEY_ID|SESSION_TOKEN|PROFILE|REGION|DEFAULT_REGION)="
    aws sts get-caller-identity
  2. 2Match what you see, then jump to its fix

    1. 1It worked earlier today, or after a role was assumedTemporary credentials have expiredFix
    2. 2The profile in ~/.aws works in the CLI with --profile, but the app fails; env shows AWS_ACCESS_KEY_IDOld keys in environment variables override the profile you meant to useFix
    3. 3Fails only in a manually enabled Region such as Asia Pacific (Hong Kong), with credentials from sts.amazonaws.comA global STS session token is used in an opt-in RegionFix

What does the error look like?

  • UnrecognizedClientException: The security token included in the request is invalid
  • The security token included in the request is invalid

Cause 1 of 3

Temporary credentials have expired

1Check

Credentials from an assumed role or an SSO login are temporary. Secrets Manager's troubleshooting page tells you to verify that temporary security credentials have not expired.

aws sts get-caller-identity

2Fix

Get fresh credentials (sign in again or assume the role again) and restart the process that cached the old ones. Where the app runs on AWS, use the role attached to the compute service instead of copied keys.

aws sso login
aws sts get-caller-identity

3Verify

The identity call succeeds, then read the secret.

aws secretsmanager get-secret-value --secret-id my-secret --region us-east-1

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 2 of 3

Old keys in environment variables override the profile you meant to use

1Check

A setting given as an environment variable overrides the value from the shared config and credentials files, and the SDK's credential chain tries environment keys first.

env | grep ^AWS_

2Fix

Remove the stale variables, or put the correct values in them. A session token has to be exported together with the key pair when the keys are temporary.

unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN
export AWS_PROFILE=my-profile

3Verify

The identity printed by the CLI is the one you expect, then start the app from the same shell.

aws sts get-caller-identity

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 3 of 3

A global STS session token is used in an opt-in Region

1Check

Session tokens from the global STS endpoint are valid only in Regions enabled by default, so they do not work in manually enabled Regions.

aws sts get-caller-identity --region ap-east-1

2Fix

Request the session from a Regional STS endpoint, or set the global endpoint's token version to be valid in all Regions.

# IAM administrator: set the global endpoint's session token version (v1 = default Regions only, v2 = all Regions)
aws iam set-security-token-service-preferences help

3Verify

Call Secrets Manager in that Region with the new credentials.

aws secretsmanager list-secrets --region ap-east-1

Confirmed by docs.aws.amazon.com

Did this fix it?

How do I stop it coming back?

  • Use roles that rotate their own credentials on AWS compute, and avoid pasting keys into .env files.
  • Keep one source of credentials per environment so that an old environment variable cannot hide the correct profile.
  • Call a Regional STS endpoint when you work in opt-in Regions.

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

The AWS SDK in Node.js cannot find any credentials

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

The AWS SDK walks a fixed credential chain: code, environment variables, the shared files in ~/.aws, then role sources such as ECS or EC2.

3 causes, each with a fix

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.