AWS credential provider that sources credentials from a Node.JS environment.
npm install @aws-sdk/credential-provider-node

This module provides a factory function, defaultProvider, that will attempt to
source AWS credentials from a Node.JS environment. It will attempt to find
credentials from the following sources (listed in order of precedence):
- Environment variables exposed via process.env
- SSO credentials from token cache
- Web identity token credentials
- Shared credentials and config ini files
- The EC2/ECS Instance Metadata Service
The default credential provider will invoke one provider at a time and only
continue to the next if no credentials have been located. For example, if the
process finds values defined via the AWS_ACCESS_KEY_ID andAWS_SECRET_ACCESS_KEY environment variables, the files at ~/.aws/credentials
and ~/.aws/config will not be read, nor will any messages be sent to the
Instance Metadata Service.
If invalid configuration is encountered (such as a profile in~/.aws/credentials specifying as its source_profile the name of a profile
that does not exist), then the chained provider will be rejected with an error
and will not invoke the next provider in the list.
_IMPORTANT_: if you intend to acquire credentials using EKS
IAM Roles for Service Accounts,
then you must explicitly specify a value for roleAssumerWithWebIdentity. There is a
default function available in @aws-sdk/client-sts package. An example of using
this:
``js
const { getDefaultRoleAssumerWithWebIdentity } = require("@aws-sdk/client-sts");
const { defaultProvider } = require("@aws-sdk/credential-provider-node");
const { S3Client, GetObjectCommand } = require("@aws-sdk/client-s3");
const provider = defaultProvider({
roleAssumerWithWebIdentity: getDefaultRoleAssumerWithWebIdentity({
// You must explicitly pass a region if you are not using us-east-1
region: "eu-west-1",
}),
});
const client = new S3Client({ credentialDefaultProvider: provider });
`
_IMPORTANT_: We provide a wrapper of this provider in @aws-sdk/credential-providersgetDefaultRoleAssumerWithWebIdentity()
package to save you from importing orgetDefaultRoleAssume() from STS package. Similarly, you can do:
`js
const { fromNodeProviderChain } = require("@aws-sdk/credential-providers");
const credentials = fromNodeProviderChain();
const client = new S3Client({ credentials });
`
You may customize how credentials are resolved by providing an options hash to
the defaultProvider factory function. The following options are
supported:
- profile - The configuration profile to use. If not specified, the providerAWS_PROFILE
will use the value in the environment variable or a default ofdefault
.filepath
- - The path to the shared credentials file. If not specified, theAWS_SHARED_CREDENTIALS_FILE
provider will use the value in the environment~/.aws/credentials
variable or a default of .configFilepath
- - The path to the shared config file. If not specified, theAWS_CONFIG_FILE
provider will use the value in the environment variable or a~/.aws/config
default of .mfaCodeProvider
- - A function that returns a a promise fulfilled with anmfaCodeProvider
MFA token code for the provided MFA Serial code. If a profile requires an MFA
code and is not a valid function, the credential providerroleAssumer
promise will be rejected.
- - A function that assumes a role and returns a promiseroleArn
fulfilled with credentials for the assumed role. If not specified, no role
will be assumed, and an error will be thrown.
- - ARN to assume. If not specified, the provider will use the valueAWS_ROLE_ARN
in the environment variable.webIdentityTokenFile
- - File location of where the OIDC token is stored.AWS_WEB_IDENTITY_TOKEN_FILE
If not specified, the provider will use the value in the roleAssumerWithWebIdentity
environment variable.
- - A function that assumes a role with web identity andtimeout
returns a promise fulfilled with credentials for the assumed role.
- - The connection timeout (in milliseconds) to apply to any remote1000
requests. If not specified, a default value of (one second) is used.maxRetries
- - The maximum number of times any HTTP connections should be0` will be used.
retried. If not specified, a default value of
- AWS Credential Provider for Node.JS - Environment Variables
- AWS Credential Provider for Node.JS - SSO
- AWS Credential Provider for Node.JS - Web Identity
- AWS Credential Provider for Node.JS - Shared Configuration Files
- AWS Credential Provider for Node.JS - Instance and Container Metadata
- AWS Shared Configuration File Loader