-
Notifications
You must be signed in to change notification settings - Fork 634
Description
Describe the feature
In sdk v3 CredentialProviders are not internally cached1. Each time you call a credential provider it refreshes credentials from it's source. This causes workflows that use multiple aws services to take longer as each client fetches the same credentials from the source.
const creds = fromProcess()
await creds() // executes the process
await creds() // executes the process again
Each client caches credentials by wrapping the provider in memoize, but memoize does not cache across instances.
const client = new S3({ creds })
await client.listObjects({}) // calls credential provider
await client.listObjects({}) // uses cached credentials
new S3({ creds }).listObjects({}) // calls credential provider again!
Credential providers should use a cache shared across all clients.
Use Case
const credentials = fromProcess()
const sqs = new SQS({ credentials })
const ddb = new DynamoDB({ credentials })
await ddb.createTable({...}) // blocks fetching credentials
await sqs.sendMessage({...}) // blocks fetching the same credentials
The use case is any workflow that involves creating multiple aws-sdk clients that use the same credentials or identity. This workflow should only fetch credentials once, not once per client.
Proposed Solution
A couple options:
- change the memoize wrapper to use a global cache. I think this could be done simply by moving these variables into a WeakMap keyed by provider. so each memoized provider instance would use the same cached values.
- memoize the CredentialProviders provided by @aws-sdk/credential-providers (like fromNodeProviderChain is already)
- Add documentation and inform users that they should memoize the credential provider they use
const credentials = memoize(fromProcess())
Option 1 works with aws-sdk authored credential providers as well as user defined providers.
Option 2 would only work out of the box with aws authored providers
Option 3 would only work when users read the docs
Other Information
No response
Acknowledgements
- I may be able to implement this feature request
- This feature might incur a breaking change
SDK version used
~3.300
Environment details (OS name and version, etc.)
All