Credentials structure
An AWS credential object has four fields:| Field | Type | Required | Description |
|---|---|---|---|
accessKeyId | string | Yes | The AWS access key ID |
secretAccessKey | string | Yes | The AWS secret access key |
sessionToken | string | No | Session token for temporary credentials |
expiration | Date | No | When the credentials expire |
credentials option.
Credential resolution chain
When you initialize a Node.js client without explicit credentials, the SDK walks a default resolution chain and uses the first source that successfully returns credentials:Explicit credentials or provider
Credentials passed directly in code to the client constructor take highest priority.
Web identity token file
Credentials from an OIDC token file, read via
AWS_WEB_IDENTITY_TOKEN_FILE and AWS_ROLE_ARN.Browsers have no default credential provider chain. You must supply credentials explicitly when running in a browser environment.
Credential caching
The SDK caches credentials per client instance. When the resolved credential object includes anexpiration date, the SDK automatically calls the provider function again when fewer than 5 minutes remain before expiry. If no expiration is set, the provider is called only once per client lifetime.
Because each client maintains its own cache, two clients configured with the same provider function will each fetch credentials independently. To share a single cached credential across multiple clients, create the provider once, call it yourself to warm the cache, or wrap it in your own memoization layer.
Passing credentials to a client
Static credentials object
Use a literal object only for local testing. Never commit real credentials to source control.Credential provider function
Any async function matching the provider signature is accepted. The SDK will call it when credentials are needed or have expired.Built-in provider functions
The@aws-sdk/credential-providers package exports ready-made provider functions for every common credential source.
Auth methods at a glance
Environment variables
Set
AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY in the shell. Ideal for CI/CD and containers.Shared INI files
Read credentials from
~/.aws/credentials and ~/.aws/config using fromIni().AWS SSO / Identity Center
Federated login via
fromSSO(). Requires an active SSO session obtained with aws configure sso.Assume role (STS)
Generate short-lived role credentials using
fromTemporaryCredentials().EC2 instance metadata
Automatically pick up the IAM role attached to an EC2 instance via
fromInstanceMetadata().ECS container metadata
Use the IAM role attached to an ECS task via
fromContainerMetadata().Web identity / OIDC
Exchange an OIDC token for temporary credentials using
fromWebToken() or fromTokenFile().Cognito Identity
Browser and mobile apps authenticate via Cognito using
fromCognitoIdentity() or fromCognitoIdentityPool().Default Node.js chain
Let the SDK try all sources automatically with
fromNodeProviderChain().Next steps
- Credential providers — detailed reference for every built-in provider function
- Environment variables — full list of environment variables the SDK reads