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.
To set up this workshop package, complete the following tasks:
nvm use
or nvm use 12
in a terminal window.node -v
in a terminal window and confirm that it shows the latest version of v10
, such as v12.17.0
).yarn
.aws
in a terminal window.~/.aws/credentials
(%UserProfile%\.aws\credentials
on Windows) should look like the following:
[default]
aws_access_key_id = <ACCESS_KEY>
aws_secret_access_key = <SECRET_ACCESS_KEY>
~/.aws/config
(%UserProfile%\.aws\config
on Windows) should look like the following:
[default]
region = us-west-2
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.
yarn build:backend
yarn cdk deploy
yarn prepare:frontend
yarn start:frontend
Follow backend README.md.
Follow frontend README.md.
The Cloudformation stack can be deleted by running: yarn cdk destroy
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:
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:
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);
});
When the code fails, the stack trace would look like:
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)
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:
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;
}
})();
The stack trace would be much smaller:
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)
Contributions are more than welcome. Please read the code of conduct and the contributing guidelines.
This sample code is made available under the MIT license. See the LICENSE file.