Skip to content

Commit b9c1904

Browse files
authored
docs(upgrading): add waiters and credentials upgrading guide (#2410)
* docs(upgrading): add waiters and credentials upgrading guide * docs(upgrading): add cognito identity provider * docs(upgrading): add the rest of v2 credential providers and equivlents
1 parent 0c9a143 commit b9c1904

File tree

1 file changed

+297
-3
lines changed

1 file changed

+297
-3
lines changed

UPGRADING.md

Lines changed: 297 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,13 @@ might not have the same name either.
110110
- **v2**: The Agent object to perform HTTP requests with. Used for connection pooling.
111111
- **v3**: You can configure `httpAgent` or `httpsAgent` as shown in the examples above.
112112

113-
- `connectionTimeout`
113+
- `connectTimeout`
114114
- **v2**: Sets the socket to timeout after failing to establish a connection with the server after connectTimeout
115115
milliseconds.
116116
- **v3**: `connectionTimeout` is available [in `NodeHttpHandler` options](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/classes/_aws_sdk_node_http_handler.nodehttphandler-1.html).
117117
- `timeout`
118118
- **v2**: The number of milliseconds a request can take before automatically being terminated.
119-
- **v3**: Hard request timeout is not available in `NodeHttpHandler`, but available as `requestTimeout` [in
120-
`FetchHttphandler` in browsers](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/classes/_aws_sdk_fetch_http_handler.fetchhttphandler-1.html)
119+
- **v3**: `socketTimeout` is available [in `NodeHttpHandler` options](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/classes/_aws_sdk_node_http_handler.nodehttphandler-1.html).
121120
- `xhrAsync`
122121
- **v2**: Whether the SDK will send asynchronous HTTP requests.
123122
- **v3**: **Deprecated**. Requests are _always_ asynchronous.
@@ -179,6 +178,277 @@ might not have the same name either.
179178
- **v2**: Whether to use the Accelerate endpoint with the S3 service.
180179
- **v3**: No change.
181180

181+
## Credential Providers
182+
183+
In v2, the SDK provides a list of credential providers to choose from, as well as a credentials provider chain,
184+
available by default on Node.js, that tries to load the AWS credentials from all the most common providers. V3 simplifies
185+
the credential provider's interface, making it easier to use and write custom credential providers. On top of a new
186+
credentials provider chain, V3 all provides a list of credential providers aiming to provide equivalent to v2.
187+
188+
Here is all the credential providers in v2 and their equivalents in v3.
189+
190+
### Default Credential Provider
191+
192+
Default credential provider is how SDK resolve the AWS credential if you DO NOT provide one explicitly.
193+
194+
- **v2**: [CredentialProviderChain](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CredentialProviderChain.html)
195+
in Node.js resolves credential from sources as following order:
196+
197+
- [environmental variable](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-environment.html)
198+
- [shared credentials file](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-shared.html)
199+
- [ECS container credentials](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/RemoteCredentials.html)
200+
- [spawning external process](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-sourcing-external.html)
201+
- [OIDC token from specified file](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/TokenFileWebIdentityCredentials.html)
202+
- [EC2 instance metadata](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html)
203+
204+
If one of the credential providers above fails to resolve the AWS credential, the chain falls back to next provider
205+
until a valid credential is resolved, or throw error when all of them fail.
206+
207+
In Browsers and ReactNative, the chain is empty, meaning you always need supply credentials explicitly.
208+
209+
- **v3**: [defaultProvider](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_credential_provider_node.html#defaultprovider)
210+
The credential sources and fallback order _does not_ change in v3. It also supports [AWS Single Sign-On credentials](https://aws.amazon.com/single-sign-on/).
211+
212+
### Temporary Credentials
213+
214+
- **v2**: [`ChainableTemporaryCredentials`](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/ChainableTemporaryCredentials.html)
215+
Represents temporary credentials retrieved from `AWS.STS`. Without any extra parameters, credentials will be
216+
fetched from the `AWS.STS.getSessionToken()` operation. If an IAM role is provided, the `AWS.STS.assumeRole()` operation
217+
will be used to fetch credentials for the role instead.
218+
`AWS.ChainableTemporaryCredentials` differs from `AWS.TemporaryCredentials` in the way masterCredentials and refreshes
219+
are handled. `AWS.ChainableTemporaryCredentials` refreshes expired credentials using the masterCredentials passed by
220+
the user to support chaining of STS credentials. However, `AWS.TemporaryCredentials` recursively collapses the
221+
masterCredentials during instantiation, precluding the ability to refresh credentials which require intermediate, temporary credentials.
222+
223+
The original [`TemporaryCredentials`](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/TemporaryCredentials.html)
224+
has been **deprecated** in favor of `ChainableTemporaryCredentials` in v2 and ``
225+
226+
- **v3**: Partially supported. You can retrieve the temporary credential from STS with the
227+
[role assumer function based on `sts:AssumeRole`](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-sts/globals.html#getdefaultroleassumer). The difference to v2 is that `sts:getSessionToken` is not called
228+
if no `RoleArn` is supplied. Please open a [feature request](https://github.com/aws/aws-sdk-js-v3/issues/new?assignees=&labels=feature-request&template=---feature-request.md&title=)
229+
if you need it.
230+
Here's an example:
231+
232+
```javascript
233+
import { FooClient } from "@aws-sdk/client-foo";
234+
import { getDefaultRoleAssumer } from "@aws-sdk/client-sts"; // ES6 import
235+
// const { FooClient } = require("@aws-sdk/client-foo");
236+
// const { getDefaultRoleAssumer } = require("@aws-sdk/client-sts"); // CommonJS import
237+
238+
/* role assumer function that calls sts:AssumeRole API */
239+
const roleAssumer = getDefaultRoleAssumer();
240+
const sourceCredential = {
241+
/* A credential can be a credential object or an async function that returns a credential object */
242+
};
243+
/* A credential can be a credential object or an async function that returns a credential object */
244+
const derivativeCredentials = () =>
245+
roleAssumer(sourceCredentials, {
246+
RoleArn,
247+
RoleSessionName,
248+
});
249+
const client = new FooClient({
250+
credentials: derivativeCredentials,
251+
});
252+
```
253+
254+
### Cognito Identity Credentials
255+
256+
Load credentials from Cognito Identity service, normally used in browsers.
257+
258+
- **v2**: [`CognitoIdentityCredentials`](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CognitoIdentityCredentials.html)
259+
Represents credentials retrieved from STS Web Identity Federation using the Amazon Cognito Identity service.
260+
- **v3**: [`Cognito Identity Credential Provider`](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_credential_provider_cognito_identity.html#fromcognitoidentity-1)
261+
The [`@aws/credential-provider-cognito-identity` package](https://www.npmjs.com/package/@aws-sdk/credential-provider-cognito-identity)
262+
provides two credential provider functions, one of which [`fromCognitoIdentity`](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_credential_provider_cognito_identity.html#fromcognitoidentity-1)
263+
takes an identity ID and calls `cognitoIdentity:GetCredentialsForIdentity`, while the other
264+
[`fromCognitoIdentityPool`](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_credential_provider_cognito_identity.html#fromcognitoidentitypool-1)
265+
takes an identity pool ID, calls `cognitoIdentity:GetId` on the first invocation, and then calls`fromCognitoIdentity`.
266+
Subsequent invocations of the latter do not re-invoke GetId
267+
268+
The provider implements the "Simplified Flow" described in the [Cognito developer guide](https://docs.aws.amazon.com/cognito/latest/developerguide/authentication-flow.html).
269+
The "Classic Flow" which involves calling `cognito:GetOpenIdToken` and then calling `sts:AssumeRoleWithWebIdentity` is
270+
NOT supported. Please open a [feature request](https://github.com/aws/aws-sdk-js-v3/issues/new?assignees=&labels=feature-request&template=---feature-request.md&title=)
271+
to us if you need it.
272+
273+
```javascript
274+
// fromCognitoIdentityPool example
275+
import { fromCognitoIdentityPool } from "@aws-sdk/credential-provider-cognito-identity";
276+
import { CognitoIdentityClient } from "@aws-sdk/client-cognito-identity"; // ES6 import
277+
// const { fromCognitoIdentityPool } = require("@aws-sdk/credential-provider-cognito-identity");
278+
// const { CognitoIdentityClient } = require("@aws-sdk/client-cognito-identity"); // CommonJS import
279+
280+
const client = new FooClient({
281+
region: "us-east-1",
282+
credentials: fromCognitoIdentityPool({
283+
client: new CognitoIdentityClient({
284+
// specify Cognito Identity client config here
285+
region: "us-east-1",
286+
}),
287+
identityPoolId: "us-east-1:1699ebc0-7900-4099-b910-2df94f52a030",
288+
customRoleArn: "arn:aws:iam::1234567890:role/MYAPP-CognitoIdentity", // Optional
289+
logins: {
290+
// Optional
291+
"graph.facebook.com": "FBTOKEN",
292+
"www.amazon.com": "AMAZONTOKEN",
293+
"api.twitter.com": "TWITTERTOKEN",
294+
},
295+
}),
296+
});
297+
```
298+
299+
```javascript
300+
// fromCognitoIdentity example
301+
import { fromCognitoIdentity } from "@aws-sdk/credential-provider-cognito-identity";
302+
import { CognitoIdentityClient } from "@aws-sdk/client-cognito-identity"; // ES6 import
303+
// const { fromCognitoIdentity } = require("@aws-sdk/credential-provider-cognito-identity");
304+
// const { CognitoIdentityClient } = require("@aws-sdk/client-cognito-identity"); // CommonJS import
305+
306+
const client = new FooClient({
307+
region: "us-east-1",
308+
credentials: fromCognitoIdentity({
309+
client: new CognitoIdentityClient({ region: "us-east-1" }),
310+
identityId: "us-east-1:128d0a74-c82f-4553-916d-90053e4a8b0f",
311+
customRoleArn: "arn:aws:iam::1234567890:role/MYAPP-CognitoIdentity", // Optional
312+
logins: {
313+
// Optional
314+
"graph.facebook.com": "FBTOKEN",
315+
"www.amazon.com": "AMAZONTOKEN",
316+
"api.twitter.com": "TWITTERTOKEN",
317+
},
318+
}),
319+
});
320+
```
321+
322+
### EC2 Metadata(IMDS) Credential
323+
324+
Represents credentials received from the metadata service on an EC2 instance.
325+
326+
- **v2**: [`EC2MetadataCredentials`](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CognitoIdentityCredentials.html)
327+
- **v3**: [`fromInstanceMetadata`](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_credential_provider_imds.html#frominstancemetadata-1): Creates a credential provider that will source credentials from the EC2 Instance Metadata Service.
328+
329+
```javascript
330+
import { fromInstanceMetadata } from "@aws-sdk/credential-provider-imds"; // ES6 import
331+
// const { fromInstanceMetadata } = require("@aws-sdk/credential-provider-imds"); // CommonJS import
332+
333+
const client = new FooClient({
334+
credentials: fromInstanceMetadata({
335+
maxRetries: 3, // Optional
336+
timeout: 0, // Optional
337+
}),
338+
});
339+
```
340+
341+
### ECS Credentials
342+
343+
Represents credentials received from specified URL. This provider will request temporary credentials from
344+
URI specified by the `AWS_CONTAINER_CREDENTIALS_RELATIVE_URI` or the `AWS_CONTAINER_CREDENTIALS_FULL_URI` environment
345+
variable.
346+
347+
- **v2**: `ECSCredentials` or [`RemoteCredentials`](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/RemoteCredentials.html).
348+
- **v3**: [`fromContainerMetadata`](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_credential_provider_imds.html#fromcontainermetadata-1) creates a credential provider that will source credentials from the ECS Container Metadata Service.
349+
350+
```javascript
351+
import { fromContainerMetadata } from "@aws-sdk/credential-provider-imds"; // ES6 import
352+
// const { fromContainerMetadata } = require("@aws-sdk/credential-provider-imds"); // CommonJS import
353+
354+
const client = new FooClient({
355+
credentials: fromContainerMetadata({
356+
maxRetries: 3, // Optional
357+
timeout: 0, // Optional
358+
}),
359+
});
360+
```
361+
362+
### File System Credentials
363+
364+
- **v2**: [`FileSystemCredentials`](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/FileSystemCredentials.html)
365+
represents credentials from a JSON file on disk.
366+
- **v3**: **Deprecated**. You can explicitly read the JSON file and supply to the client. Please open a
367+
[feature request](https://github.com/aws/aws-sdk-js-v3/issues/new?assignees=&labels=feature-request&template=---feature-request.md&title=)
368+
to us if you need it.
369+
370+
### SAML Credential Provider
371+
372+
- **v2**: [`SAMLCredentials`](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SAMLCredentials.html) represents
373+
credentials retrieved from STS SAML support.
374+
- **v3**: **Not available**. Please open a [feature request](https://github.com/aws/aws-sdk-js-v3/issues/new?assignees=&labels=feature-request&template=---feature-request.md&title=)
375+
to us if you need it.
376+
377+
### Shared Credential File Credentials
378+
379+
Loads credentials from shared credentials file (defaulting to `~/.aws/credentials` or defined by the
380+
`AWS_SHARED_CREDENTIALS_FILE` environment variable). This file is supported across different AWS SDKs and tools. You can
381+
refer to the [shared config and credentials files document](https://docs.aws.amazon.com/sdkref/latest/guide/creds-config-files.html)
382+
for more information.
383+
384+
- **v2**: [`SharedIniFileCredentials`](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SharedIniFileCredentials.html)
385+
- **v3**: [`fromIni`](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_credential_provider_ini.html).
386+
387+
```javascript
388+
import { fromIni } from "@aws-sdk/credential-provider-ini";
389+
import { getDefaultRoleAssumer, getDefaultRoleAssumerWithWebIdentity } from "@aws-sdk/client-sts"; // ES6 import
390+
// const { fromIni } from("@aws-sdk/credential-provider-ini");
391+
// const { getDefaultRoleAssumer, getDefaultRoleAssumerWithWebIdentity } = require("@aws-sdk/client-sts"); // CommonJS import
392+
393+
const client = new FooClient({
394+
credentials: fromIni({
395+
configFilepath: "~/.aws/config", // Optional
396+
filepath: "~/.aws/credentials", // Optional
397+
mfaCodeProvider: async (mfaSerial) => {
398+
// implement a pop-up asking for MFA code
399+
return "some_code";
400+
}, // Optional
401+
profile: "default", // Optional
402+
roleAssumer: getDefaultRoleAssumer(), // Optional. Required if you specify role to assume
403+
roleAssumerWithWebIdentity: getDefaultRoleAssumerWithWebIdentity(), // Optional. Required if you specify role to assume using `sts:AssumeRoleWithWebIdentity` API
404+
}),
405+
});
406+
```
407+
408+
### Web Identity Credentials
409+
410+
Retrieves credentials using OIDC token from a file on disk. It's commonly used in EKS.
411+
412+
- **v2**: [`TokenFileWebIdentityCredentials`](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/TokenFileWebIdentityCredentials.html).
413+
- **v3**: [`fromTokenFile`](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_credential_provider_web_identity.html#fromtokenfile-1)
414+
415+
```javascript
416+
import { fromTokenFile } from "@aws-sdk/credential-provider-web-identity";
417+
import { getDefaultRoleAssumerWithWebIdentity } from "@aws-sdk/client-sts"; // ES6 import
418+
// const { fromIni } from("@aws-sdk/credential-provider-ini");
419+
// const { getDefaultRoleAssumerWithWebIdentity } = require("@aws-sdk/client-sts"); // CommonJS import
420+
421+
const client = new FooClient({
422+
credentials: fromTokenFile({
423+
roleAssumerWithWebIdentity: getDefaultRoleAssumerWithWebIdentity(),
424+
roleArn: "arn:xxxx" // Optional. Otherwise read from `AWS_ROLE_ARN` environmental variable
425+
roleSessionName: "session:a" // Optional. Otherwise read from `AWS_ROLE_SESSION_NAME` environmental variable
426+
})
427+
});
428+
```
429+
430+
### Web Identity Federation Credentials
431+
432+
Retrieves credentials from STS web identity federation support.
433+
434+
- **v2**: [`WebIdentityCredentials`](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/WebIdentityCredentials.html)
435+
- **v3**: [`fromWebToken`](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_credential_provider_web_identity.html#fromwebtoken-1)
436+
437+
```javascript
438+
import { fromWebToken } from "@aws-sdk/credential-provider-web-identity";
439+
import { getDefaultRoleAssumerWithWebIdentity } from "@aws-sdk/client-sts"; // ES6 import
440+
// const { fromWebToken } from("@aws-sdk/credential-provider-web-identity");
441+
// const { getDefaultRoleAssumerWithWebIdentity } = require("@aws-sdk/client-sts"); // CommonJS import
442+
443+
const client = new FooClient({
444+
credentials: fromWebToken({
445+
roleAssumerWithWebIdentity: getDefaultRoleAssumerWithWebIdentity(),
446+
roleArn: "arn:xxxx" // Otherwise read from `AWS_ROLE_ARN` environmental variable
447+
roleSessionName: "session:a" // Otherwise read from `AWS_ROLE_SESSION_NAME` environmental variable
448+
})
449+
});
450+
```
451+
182452
## S3 Multipart Upload
183453

184454
In v2, the S3 client contains an [`upload()`](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#upload-property)
@@ -231,3 +501,27 @@ await ddbDocClient.send(
231501
```
232502

233503
More examples and configurations are available in the [package README](https://github.com/aws/aws-sdk-js-v3/blob/main/lib/lib-dynamodb/README.md).
504+
505+
## Waiters
506+
507+
In v2, all waiters are bound to the service client class, you need to specify in waiter's input which designed state the
508+
client will be waiting for. For example, you need to [call `waitFor("bucketExists")`](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#bucketExists-waiter)
509+
to wait for a newly created bucket to be ready.
510+
511+
In v3, you don't need to import waiters if your application doesn't need one. Moreover, you can import only the waiter
512+
you need to wait for the particular desired state you want. Thus, you can reduce your bundle size and improve
513+
performance. Here's the example of waiting for bucket to be ready after creation:
514+
515+
```javascript
516+
import { S3Client, CreateBucketCommand, waitUntilBucketExists } from "@aws-sdk/client-s3"; // ES6 import
517+
// const { S3Client, CreateBucketCommand, waitUntilBucketExists } = require("@aws-sdk/client-s3"); // CommonJS import
518+
519+
const Bucket = "BUCKET_NAME";
520+
const client = new S3Client({ region: "REGION" });
521+
const command = new CreateBucketCommand({ Bucket });
522+
523+
await client.send(command);
524+
await waitUntilBucketExists({ client, maxWaitTime: 60 }, { Bucket });
525+
```
526+
527+
You can find everything of how to configure the waiters in the [blog post of waiters in v3 SDK](https://aws.amazon.com/blogs/developer/waiters-in-modular-aws-sdk-for-javascript/).

0 commit comments

Comments
 (0)