1 | # @aws-sdk/credential-provider-node
|
2 |
|
3 | [](https://www.npmjs.com/package/@aws-sdk/credential-provider-node)
|
4 | [](https://www.npmjs.com/package/@aws-sdk/credential-provider-node)
|
5 |
|
6 | ## AWS Credential Provider for Node.JS
|
7 |
|
8 | This module provides a factory function, `fromEnv`, that will attempt to source
|
9 | AWS credentials from a Node.JS environment. It will attempt to find credentials
|
10 | from the following sources (listed in order of precedence):
|
11 |
|
12 | - Environment variables exposed via `process.env`
|
13 | - SSO credentials from token cache
|
14 | - Web identity token credentials
|
15 | - Shared credentials and config ini files
|
16 | - The EC2/ECS Instance Metadata Service
|
17 |
|
18 | The default credential provider will invoke one provider at a time and only
|
19 | continue to the next if no credentials have been located. For example, if the
|
20 | process finds values defined via the `AWS_ACCESS_KEY_ID` and
|
21 | `AWS_SECRET_ACCESS_KEY` environment variables, the files at `~/.aws/credentials`
|
22 | and `~/.aws/config` will not be read, nor will any messages be sent to the
|
23 | Instance Metadata Service.
|
24 |
|
25 | If invalid configuration is encountered (such as a profile in
|
26 | `~/.aws/credentials` specifying as its `source_profile` the name of a profile
|
27 | that does not exist), then the chained provider will be rejected with an error
|
28 | and will not invoke the next provider in the list.
|
29 |
|
30 | _IMPORTANT_: if you intend to acquire credentials using EKS
|
31 | [IAM Roles for Service Accounts](https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html)
|
32 | then you must explicitly specify a value for `roleAssumerWithWebIdentity`. There is a
|
33 | default function available in `@aws-sdk/client-sts` package. An example of using
|
34 | this:
|
35 |
|
36 | ```js
|
37 | const { getDefaultRoleAssumerWithWebIdentity } = require("@aws-sdk/client-sts");
|
38 | const { defaultProvider } = require("@aws-sdk/credential-provider-node");
|
39 | const { S3Client, GetObjectCommand } = require("@aws-sdk/client-s3");
|
40 |
|
41 | const provider = defaultProvider({
|
42 | roleAssumerWithWebIdentity: getDefaultRoleAssumerWithWebIdentity(),
|
43 | });
|
44 |
|
45 | const client = new S3Client({ credentialDefaultProvider: provider });
|
46 | ```
|
47 |
|
48 | _IMPORTANT_: We provide a wrapper of this provider in `@aws-sdk/credential-providers`
|
49 | package to save you from importing `getDefaultRoleAssumerWithWebIdentity()` or
|
50 | `getDefaultRoleAssume()` from STS package. Similarly, you can do:
|
51 |
|
52 | ```js
|
53 | const { fromNodeProviderChain } = require("@aws-sdk/credential-providers");
|
54 |
|
55 | const credentials = fromNodeProviderChain();
|
56 |
|
57 | const client = new S3Client({ credentials });
|
58 | ```
|
59 |
|
60 | ## Supported configuration
|
61 |
|
62 | You may customize how credentials are resolved by providing an options hash to
|
63 | the `defaultProvider` factory function. The following options are
|
64 | supported:
|
65 |
|
66 | - `profile` - The configuration profile to use. If not specified, the provider
|
67 | will use the value in the `AWS_PROFILE` environment variable or a default of
|
68 | `default`.
|
69 | - `filepath` - The path to the shared credentials file. If not specified, the
|
70 | provider will use the value in the `AWS_SHARED_CREDENTIALS_FILE` environment
|
71 | variable or a default of `~/.aws/credentials`.
|
72 | - `configFilepath` - The path to the shared config file. If not specified, the
|
73 | provider will use the value in the `AWS_CONFIG_FILE` environment variable or a
|
74 | default of `~/.aws/config`.
|
75 | - `mfaCodeProvider` - A function that returns a a promise fulfilled with an
|
76 | MFA token code for the provided MFA Serial code. If a profile requires an MFA
|
77 | code and `mfaCodeProvider` is not a valid function, the credential provider
|
78 | promise will be rejected.
|
79 | - `roleAssumer` - A function that assumes a role and returns a promise
|
80 | fulfilled with credentials for the assumed role. If not specified, the SDK
|
81 | will create an STS client and call its `assumeRole` method.
|
82 | - `roleArn` - ARN to assume. If not specified, the provider will use the value
|
83 | in the `AWS_ROLE_ARN` environment variable.
|
84 | - `webIdentityTokenFile` - File location of where the `OIDC` token is stored.
|
85 | If not specified, the provider will use the value in the `AWS_WEB_IDENTITY_TOKEN_FILE`
|
86 | environment variable.
|
87 | - `roleAssumerWithWebIdentity` - A function that assumes a role with web identity and
|
88 | returns a promise fulfilled with credentials for the assumed role.
|
89 | - `timeout` - The connection timeout (in milliseconds) to apply to any remote
|
90 | requests. If not specified, a default value of `1000` (one second) is used.
|
91 | - `maxRetries` - The maximum number of times any HTTP connections should be
|
92 | retried. If not specified, a default value of `0` will be used.
|
93 |
|
94 | ## Related packages:
|
95 |
|
96 | - [AWS Credential Provider for Node.JS - Environment Variables](../credential-provider-env)
|
97 | - [AWS Credential Provider for Node.JS - SSO](../credential-provider-sso)
|
98 | - [AWS Credential Provider for Node.JS - Web Identity](../credential-provider-web-identity)
|
99 | - [AWS Credential Provider for Node.JS - Shared Configuration Files](../credential-provider-ini)
|
100 | - [AWS Credential Provider for Node.JS - Instance and Container Metadata](../credential-provider-imds)
|
101 | - [AWS Shared Configuration File Loader](../shared-ini-file-loader)
|