Skip to content

Commit 26788b9

Browse files
authored
docs(credential-providers): add longer explanation about credential function and chaining (#6382)
* docs(credential-providers): add longer explanation about credential function and chaining * docs: update link formatting
1 parent 1a479dc commit 26788b9

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

packages/credential-providers/README.md

+20-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ A collection of all credential providers, with default clients.
2525
1. [SSO login with AWS CLI](#sso-login-with-the-aws-cli)
2626
1. [Sample Files](#sample-files-2)
2727
1. [From Node.js default credentials provider chain](#fromNodeProviderChain)
28-
1. [Creating a custom credentials chain](#chain)
28+
1. [Creating a custom credentials chain](#createCredentialChain)
2929

3030
## `fromCognitoIdentity()`
3131

@@ -785,10 +785,25 @@ const credentialProvider = fromNodeProviderChain({
785785
});
786786
```
787787

788-
## `chain()`
788+
## `createCredentialChain()`
789789

790790
You can use this helper to create a credential chain of your own.
791791

792+
A credential chain is created from a list of functions of the signature () => Promise<[AwsCredentialIdentity](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-smithy-types/Interface/AwsCredentialIdentity/)>,
793+
composed together such that the overall chain has the **same** signature.
794+
795+
That is why you can provide the chained credential provider to the same field (`credentials`) as any single provider function.
796+
797+
All the providers from this package are compatible, and can be used to create such a chain.
798+
799+
As with _any_ function provided to the `credentials` SDK client constructor configuration field, if the credential object returned does not contain
800+
an `expiration` (type `Date`), the client will only ever call the provider function once. You do not need to memoize this function.
801+
802+
To enable automatic refresh, the credential provider function should set an `expiration` (`Date`) field. When this expiration approaches within 5 minutes, the
803+
provider function will be called again by the client in the course of making SDK requests.
804+
805+
To assist with this, the `createCredentialChain` has a chainable helper `.expireAfter(milliseconds: number)`. An example is included below.
806+
792807
```ts
793808
import { fromEnv, fromIni, createCredentialChain } from "@aws-sdk/credential-providers";
794809
import { S3 } from "@aws-sdk/client-s3";
@@ -810,7 +825,9 @@ new S3({
810825
// A set expiration will cause the credentials function to be called again
811826
// when the time left is less than 5 minutes.
812827
new S3({
813-
// expire after 15 minutes (in milliseconds).
828+
// This setting indicates expiry after 15 minutes (in milliseconds) with `15 * 60_000`.
829+
// Due to the 5 minute expiry window, the function will be called approximately every
830+
// 10 minutes under continuous usage of this client.
814831
credentials: createCredentialChain(fromEnv(), fromIni()).expireAfter(15 * 60_000),
815832
});
816833

0 commit comments

Comments
 (0)