Skip to content

Commit 1acd9c9

Browse files
committed
docs: add an example to use fromTokenFile
1 parent 1744455 commit 1acd9c9

File tree

1 file changed

+58
-3
lines changed
  • packages/credential-provider-assume-role

1 file changed

+58
-3
lines changed

packages/credential-provider-assume-role/README.md

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
This module includes functions which get credentials by calling STS assumeRole\* APIs.
99

10-
### fromTokenFile
10+
## fromTokenFile
1111

1212
The function `fromTokenFile` returns `CredentialProvider` that reads credentials as follows:
1313

14-
- Reads file location of where the OIDC token is stored from either environment or config file paramters.
14+
- Reads file location of where the OIDC token is stored from either environment or config file parameters.
1515
- Reads IAM role wanting to be assumed from either environment or config file paramters.
1616
- Reads optional role session name to be used to distinguish sessions from either environment or config file paramters.
1717
If session name is not defined, it comes up with a role session name.
@@ -25,7 +25,7 @@ The function `fromTokenFile` returns `CredentialProvider` that reads credentials
2525
| AWS_IAM_ROLE_ARN | role_arn | true | The IAM role wanting to be assumed |
2626
| AWS_IAM_ROLE_SESSION_NAME | role_session_name | false | The IAM session name used to distinguish sessions |
2727

28-
#### Supported configuration
28+
### Supported configuration
2929

3030
The following options are supported:
3131

@@ -42,3 +42,58 @@ The following options are supported:
4242
fulfilled with credentials for the assumed role.
4343
- `roleAssumerWithWebIdentity` - A function that assumes a role with web identity
4444
and returns a promise fulfilled with credentials for the assumed role.
45+
46+
### Examples
47+
48+
A basic example of using fromTokenFile:
49+
50+
```js
51+
import { STSClient, AssumeRoleWithWebIdentityCommand } from "@aws-sdk/client-sts";
52+
import { fromTokenFile } from "@aws-sdk/credential-provider-assume-role";
53+
54+
const stsClient = new STSClient({});
55+
56+
const roleAssumerWithWebIdentity = async (params) => {
57+
const { Credentials } = await stsClient.send(
58+
new AssumeRoleWithWebIdentityCommand(params)
59+
);
60+
if (!Credentials || !Credentials.AccessKeyId || !Credentials.SecretAccessKey) {
61+
throw new Error(`Invalid response from STS.assumeRole call with role ${params.RoleArn}`);
62+
}
63+
return {
64+
accessKeyId: Credentials.AccessKeyId,
65+
secretAccessKey: Credentials.SecretAccessKey,
66+
sessionToken: Credentials.SessionToken,
67+
expiration: Credentials.Expiration,
68+
};
69+
};
70+
71+
const client = new FooClient({
72+
credentials: fromTokenFile({
73+
roleAssumerWithWebIdentity
74+
});
75+
});
76+
```
77+
78+
#### Values in environment variables
79+
80+
The values can be defined in environment variables as follows:
81+
82+
```console
83+
$ node
84+
> Object.fromEntries(Object.entries(process.env).filter(([key, value]) => key.startsWith("AWS_")));
85+
{
86+
AWS_WEB_IDENTITY_TOKEN_FILE: '/temp/token',
87+
AWS_ROLE_ARN: 'arn:aws:iam::123456789012:role/example-role-arn'
88+
}
89+
```
90+
91+
#### Values in configuration files
92+
93+
The values can be defined in configuration files as follows:
94+
95+
```
96+
[sample-profile]
97+
web_identity_token_file = /temp/token
98+
role_session_name = arn:aws:iam::123456789012:role/example-role-arn
99+
```

0 commit comments

Comments
 (0)