Create the Amazon resources - Amazon SDK for JavaScript
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

The Amazon SDK for JavaScript V3 API Reference Guide describes in detail all the API operations for the Amazon SDK for JavaScript version 3 (V3).

Create the Amazon resources

This topic is part of a tutorial that demonstrates how to invoke a Lambda function through Amazon CloudWatch scheduled events using the Amazon SDK for JavaScript. To start at the beginning of the tutorial, see Creating scheduled events to execute Amazon Lambda functions.

This tutorial requires the following resources.

  • An Amazon DynamoDB table named Employee with a key named Id and the fields shown in the previous illustration. Make sure you enter the correct data, including a valid mobile phone that you want to test this use case with. For more information, see Create a Table.

  • An IAM role with attached permissions to execute Lambda functions.

  • An Amazon S3 bucket to host Lambda function.

You can create these resources manually, but we recommend provisioning these resources using the Amazon CloudFormation as described in this tutorial.

Create the Amazon resources using Amazon CloudFormation

Amazon CloudFormation enables you to create and provision Amazon infrastructure deployments predictably and repeatedly. For more information about Amazon CloudFormation, see the Amazon CloudFormation developer guide..

To create the Amazon CloudFormation stack using the Amazon CLI:

  1. Install and configure the Amazon CLI following the instructions in the Amazon CLI User Guide.

  2. Create a file named setup.yaml in the root directory of your project folder, and copy the content here on GitHub into it.

    Note

    The Amazon CloudFormation template was generated using the Amazon CDK available here on GitHub. For more information about the Amazon CDK, see the Amazon Cloud Development Kit (Amazon CDK) Developer Guide.

  3. Run the following command from the command line, replacing STACK_NAME with a unique name for the stack.

    Important

    The stack name must be unique within an Amazon Region and Amazon account. You can specify up to 128 characters, and numbers and hyphens are allowed.

    aws cloudformation create-stack --stack-name STACK_NAME --template-body file://setup.yaml --capabilities CAPABILITY_IAM

    For more information on the create-stack command parameters, see the Amazon CLI Command Reference guide, and the Amazon CloudFormation User Guide.

    View a list of the resources in the console by opening the stack on the Amazon CloudFormation dashboard, and choosing the Resources tab. You require these for the tutorial.

  4. When the stack is created, use the Amazon SDK for JavaScript to populate the DynamoDB table, as described in Populate the DynamoDB table.

Populate the DynamoDB table

To populate the table, first create a directory named libs, and in it create a file named dynamoClient.js, and paste the content below into it.

const { DynamoDBClient } = require( "@aws-sdk/client-dynamodb" ); // Set the AWS Region. const REGION = "REGION"; // e.g. "us-east-1" // Create an Amazon DynamoDB service client object. const dynamoClient = new DynamoDBClient({region:REGION}); module.exports = { dynamoClient };

This code is available here on GitHub.

Next, create a file named populate-table.js in the root directory of your project folder, and copy the content here on GitHub into it. For one of the items, replace the value for the phone property with a valid mobile phone number in the E.164 format, and the value for the startDate with today's date.

Run the following command from the command line.

node populate-table.js
const { BatchWriteItemCommand } = require( "aws-sdk/client-dynamodb" ); const {dynamoClient} = require( "./libs/dynamoClient" ); // Set the parameters. const params = { RequestItems: { Employees: [ { PutRequest: { Item: { id: { N: "1" }, firstName: { S: "Bob" }, phone: { N: "155555555555654" }, startDate: { S: "2019-12-20" }, }, }, }, { PutRequest: { Item: { id: { N: "2" }, firstName: { S: "Xing" }, phone: { N: "155555555555653" }, startDate: { S: "2019-12-17" }, }, }, }, { PutRequest: { Item: { id: { N: "55" }, firstName: { S: "Harriette" }, phone: { N: "155555555555652" }, startDate: { S: "2019-12-19" }, }, }, }, ], }, }; export const run = async () => { try { const data = await dbclient.send(new BatchWriteItemCommand(params)); console.log("Success", data); } catch (err) { console.log("Error", err); } }; run();

This code is available here on GitHub.