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, `defaultProvider`, that will attempt to
|
9 | source AWS credentials from a Node.JS environment. It will attempt to find
|
10 | credentials 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 | // You must explicitly pass a region if you are not using us-east-1
|
44 | region: "eu-west-1"
|
45 | }),
|
46 | });
|
47 |
|
48 | const client = new S3Client({ credentialDefaultProvider: provider });
|
49 | ```
|
50 |
|
51 | _IMPORTANT_: We provide a wrapper of this provider in `@aws-sdk/credential-providers`
|
52 | package to save you from importing `getDefaultRoleAssumerWithWebIdentity()` or
|
53 | `getDefaultRoleAssume()` from STS package. Similarly, you can do:
|
54 |
|
55 | ```js
|
56 | const { fromNodeProviderChain } = require("@aws-sdk/credential-providers");
|
57 |
|
58 | const credentials = fromNodeProviderChain();
|
59 |
|
60 | const client = new S3Client({ credentials });
|
61 | ```
|
62 |
|
63 | ## Supported configuration
|
64 |
|
65 | You may customize how credentials are resolved by providing an options hash to
|
66 | the `defaultProvider` factory function. The following options are
|
67 | supported:
|
68 |
|
69 | - `profile` - The configuration profile to use. If not specified, the provider
|
70 | will use the value in the `AWS_PROFILE` environment variable or a default of
|
71 | `default`.
|
72 | - `filepath` - The path to the shared credentials file. If not specified, the
|
73 | provider will use the value in the `AWS_SHARED_CREDENTIALS_FILE` environment
|
74 | variable or a default of `~/.aws/credentials`.
|
75 | - `configFilepath` - The path to the shared config file. If not specified, the
|
76 | provider will use the value in the `AWS_CONFIG_FILE` environment variable or a
|
77 | default of `~/.aws/config`.
|
78 | - `mfaCodeProvider` - A function that returns a a promise fulfilled with an
|
79 | MFA token code for the provided MFA Serial code. If a profile requires an MFA
|
80 | code and `mfaCodeProvider` is not a valid function, the credential provider
|
81 | promise will be rejected.
|
82 | - `roleAssumer` - A function that assumes a role and returns a promise
|
83 | fulfilled with credentials for the assumed role. If not specified, no role
|
84 | will be assumed, and an error will be thrown.
|
85 | - `roleArn` - ARN to assume. If not specified, the provider will use the value
|
86 | in the `AWS_ROLE_ARN` environment variable.
|
87 | - `webIdentityTokenFile` - File location of where the `OIDC` token is stored.
|
88 | If not specified, the provider will use the value in the `AWS_WEB_IDENTITY_TOKEN_FILE`
|
89 | environment variable.
|
90 | - `roleAssumerWithWebIdentity` - A function that assumes a role with web identity and
|
91 | returns a promise fulfilled with credentials for the assumed role.
|
92 | - `timeout` - The connection timeout (in milliseconds) to apply to any remote
|
93 | requests. If not specified, a default value of `1000` (one second) is used.
|
94 | - `maxRetries` - The maximum number of times any HTTP connections should be
|
95 | retried. If not specified, a default value of `0` will be used.
|
96 |
|
97 | ## Related packages:
|
98 |
|
99 | - [AWS Credential Provider for Node.JS - Environment Variables](../credential-provider-env)
|
100 | - [AWS Credential Provider for Node.JS - SSO](../credential-provider-sso)
|
101 | - [AWS Credential Provider for Node.JS - Web Identity](../credential-provider-web-identity)
|
102 | - [AWS Credential Provider for Node.JS - Shared Configuration Files](../credential-provider-ini)
|
103 | - [AWS Credential Provider for Node.JS - Instance and Container Metadata](../credential-provider-imds)
|
104 | - [AWS Shared Configuration File Loader](../shared-ini-file-loader)
|