Home

Awesome

aws-sdk-js-v3-workshop

If you're looking for scripts to upgrade your code from v2 to v3, see aws-sdk-js-codemod.

In this workshop, we're going to:

The note taking application is the modified version from the original Open Source MIT licensed project shared in the tutorials on serverless-stack.

If you are starting a new project with AWS SDK for JavaScript v3, then please refer aws-sdk-js-notes-app which uses more services in addition to S3 and DynamoDB.

Table of Contents

Pre-requisites

To set up this workshop package, complete the following tasks:

Exercise1

This exercise code uses AWS SDK for JavaScript v2 as follows:

The README files have instructions on how to move both to v3. The backend and frontend can be worked on independently as long as the APIs don't change.

Create backend API

Deploy infrastructure

Prepare frontend API

Debug frontend

Migrate backend from v2 to v3

Follow backend README.md.

Migrate frontend from v2 to v3

Follow frontend README.md.

Clean up

The Cloudformation stack can be deleted by running: yarn cdk destroy

Exercise2

This exercise has the code which uses AWS SDK for JavaScript v3, which you would have got after finishing Exercise1:

Edit existing APIs or create new ones to use AWS Services you're familiar with in the backend. For example:

Exercise3

Inspect the differences of stack trace if call DynamoDB.putItem with invalid resources in V2 and V3 SDK.

Using v2, call a service with invalid parameters as shown below:

<details> <summary>Code</summary>
const DynamoDB = require("aws-sdk/clients/dynamodb");
const client = new DynamoDB({ region: "us-west-2" });
const request = client.putItem({
  TableName: "FakeName",
  Item: {
    Foo: { S: "Foo" },
  },
});
request.send((err, data) => {
  console.log(err);
});
</details>

When the code fails, the stack trace would look like:

<details> <summary>Stack trace</summary>
ResourceNotFoundException: Requested resource not found
    at Request.extractError (XXX/node_modules/aws-sdk/lib/protocol/json.js:51:27)
    at Request.callListeners (XXX/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
    at Request.emit (XXX/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
    at Request.emit (XXX/node_modules/aws-sdk/lib/request.js:683:14)
    at Request.transition (XXX/node_modules/aws-sdk/lib/request.js:22:10)
    at AcceptorStateMachine.runTo (XXX/node_modules/aws-sdk/lib/state_machine.js:14:12)
    at XXX/node_modules/aws-sdk/lib/state_machine.js:26:10
    at Request.<anonymous> (XXX/node_modules/aws-sdk/lib/request.js:38:9)
    at Request.<anonymous> (XXX/node_modules/aws-sdk/lib/request.js:685:12)
    at AcceptorStateMachine.runTo (XXX/node_modules/aws-sdk/lib/state_machine.js:14:12)
    at XXX/node_modules/aws-sdk/lib/state_machine.js:26:10
    at Request.<anonymous> (XXX/node_modules/aws-sdk/lib/request.js:38:9)
    at Request.<anonymous> (XXX/node_modules/aws-sdk/lib/request.js:685:12)
    at AcceptorStateMachine.runTo (XXX/node_modules/aws-sdk/lib/state_machine.js:14:12)
    at XXX/node_modules/aws-sdk/lib/state_machine.js:26:10
    at Request.<anonymous> (XXX/node_modules/aws-sdk/lib/request.js:38:9)
    at Request.<anonymous> (XXX/node_modules/aws-sdk/lib/request.js:685:12)
    at Request.callListeners (XXX/node_modules/aws-sdk/lib/sequential_executor.js:116:18)
</details>

This happens, as Request.transition exists multiple times as the SDK state machine stuck at some state and makes stack trace unreadable.

Is the same operation is tried in v3:

<details> <summary>Code</summary>
const {
  DynamoDBClient,
  PutItemCommand,
} = require("@aws-sdk/client-dynamodb-node");
const client = new DynamoDBClient({ region: "us-west-2" });
const command = new PutItemCommand({
  TableName: "FakeName",
  Item: {
    Foo: { S: "Foo" },
  },
});
(async () => {
  try {
    await client.send(command);
  } catch (e) {
    console.log(e);
    throw e;
  }
})();
</details>

The stack trace would be much smaller:

<details> <summary>Stack trace</summary>
ResourceNotFoundException: Requested resource not found
    at JsonRpcParser.exports.jsonErrorUnmarshaller [as parseServiceException] (XXX/node_modules/@aws-sdk/json-error-unmarshaller/build/index.js:37:70)
    at JsonRpcParser.<anonymous> (XXX/node_modules/@aws-sdk/protocol-json-rpc/build/JsonRpcParser.js:22:40)
    at step (XXX/node_modules/tslib/tslib.js:136:27)
    at Object.next (XXX/node_modules/tslib/tslib.js:117:57)
    at fulfilled (XXX/node_modules/tslib/tslib.js:107:62)
    at process._tickCallback (internal/process/next_tick.js:68:7)
</details>

Contributing

Contributions are more than welcome. Please read the code of conduct and the contributing guidelines.

License Summary

This sample code is made available under the MIT license. See the LICENSE file.