AWS SDK says the security token included in the request is invalid
UnrecognizedClientException: The security token included in the request is invalidShort 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.
Fix it fast
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-identity2Match what you see, then jump to its fix
- 1
It worked earlier today, or after a role was assumedTemporary credentials have expiredFix - 2
The 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
Fails 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
- 1
What does the error look like?
UnrecognizedClientException: The security token included in the request is invalidThe 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-identity2Fix
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-identity3Verify
The identity call succeeds, then read the secret.
aws secretsmanager get-secret-value --secret-id my-secret --region us-east-1Confirmed by docs.aws.amazon.com
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-profile3Verify
The identity printed by the CLI is the one you expect, then start the app from the same shell.
aws sts get-caller-identityConfirmed by docs.aws.amazon.com
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-12Fix
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 help3Verify
Call Secrets Manager in that Region with the new credentials.
aws secretsmanager list-secrets --region ap-east-1Confirmed by docs.aws.amazon.com
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
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 fixBack end
AWS SDK for JavaScript v3 cannot find credentials, even for DynamoDB Local
A DynamoDB client pointed at http://localhost:8000 fails with the same message
The SDK walked its default credential provider chain and found nothing.
3 causes, each with a fixBack end
Amazon Cognito says the user pool client does not exist
ResourceNotFoundException (HTTP status code 400) returned by InitiateAuth
Cognito looks the app client ID up inside the Region you call.
3 causes, each with a fixBack end
Node.js S3 client cannot resolve my-bucket.localhost on LocalStack
Error: getaddrinfo ENOTFOUND bucketname.localhost
The SDK puts the bucket name in the hostname (virtual-hosted style), so it looks up my-bucket.localhost, which does not resolve.
3 causes, each with a fixBack end
S3 says Access Denied when my Node.js code reads or writes an object
Access Denied
S3 answers AccessDenied when no policy allows the exact action on the exact resource, when something denies it, or when the SDK signs with a different identity than you think.
5 causes, each with a fixBack 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