This tutorial shows you how to use the AWS SDK for JavaScript (V3) and the AWS Management Console to create and run a web application that inserts records into Amazon DynamoDB.
Services used
- Amazon Cognito
- AWS Lambda
- Amazon DynamoDB
- AWS Identity and Access Management (IAM)
Cost to complete: The AWS services included in this document are included in the AWS Free Tier.
Note: Be sure to terminate all of the resources after you have completed this tutorial to ensure that you are not charged.
To build this cross-service example, you need the following:
- An AWS account. For more information, see the AWS SDKs and Tools Reference Guide.
- A NodeJS environment.
- We recommend that you grant this code least privilege, or at most the minimum permissions required to perform the task. For more information, see Grant Least Privilege in the AWS Identity and Access Management User Guide.
- This code has not been tested in all AWS Regions. Some AWS services are available only in specific Regions.
- Running this code might result in charges to your AWS account. We recommend you destroy the resources when you are finished.
Note: In the following sections, the links to the console take a best guess at your intended Region. Make sure to create the resources in the same Region.
- Open Amazon Cognito in the AWS Management Console.
- Select
Identity poolsin the sidebar. - Choose
Create identity pool. - Select the
Guest accesscheck box. - Choose
Next. - Select
Create a new IAM role. - Enter a role name in the
IAM rolefield. - Choose
Next. - Enter a name for the identity pool.
- Choose
Next. - Review the information and choose
Create identity pool.
- Open IAM in the AWS Management Console.
- Select
Rolesin the sidebar. - Search for the guest role created in the preceding section.
- Choose the role link.
- Expand the default permissions policy on the
Permissionstab. - Choose the
Editbutton. - Replace the existing policy JSON with the following JSON. This allows a user with the guest role to invoke the Lambda.
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "lambda:InvokeFunction",
"Resource": "*",
"Effect": "Allow"
}
]
}- Choose
Next. - Choose
Save Changes.
- Open DynamoDB in the AWS Management Console.
- Choose
Create table. - Enter
lambda-for-browserin theTable namefield. The name of the table must match withTableNamein./frontend/src/index.js. - For the
Partition key, enter the key nameIdand the type ofNumber. - Keep the
Sort keyblank. There are no queries in this example, so it's irrelevant. - Keep
Default settingsselected. - Choose
Create table.
The backend will have access to the SDK through its runtime. Bundling is not necessary for the backend code, but it's done here to keep the code smaller and more organized.
Note: The AWS SDK packages are excluded from the bundle. This is configured in ./lambda/webpack.config.js.
- Replace
IDENTITY_POOL_IDinlambda/src/ddbClient.jswith the ID of the identity pool created in Create an Identity pool and guest role. cd lambdanpm inpm run build- Create a compressed
.zipfile fromlambda/dist/index.mjs.
- Open Lambda in the AWS Management Console.
- Choose
Create function. - Enter the name
examplePutItemin theFunction namefield. This name must matchFunctionNameinfrontend/src/index.js. - Expand the
Change default execution rolesection. - Select
Create a new role from AWS policy templates. - Enter a name in the
Role namefield. - Select the
Simple microservice permissionsoption to grant the Lambda function access to DynamoDB. - Choose
Create function. - In the
Codetab of the function page, chooseUpload fromand select.zip file. - Choose
Uploadand select the.zipfile created in step 4 of Bundle the Lambda function. - Choose
Save. - (Optional) Test the function in the console by choosing
Testand providing the followingEvent JSON. If the test is successful, you will see the created record in the DynamoDB table.
{
"Item": {
"Id": 1001,
"Color": "tangerine",
"Pattern": "solid"
},
"TableName": "lambda-for-browser"
}The frontend code must be bundled in order to include the browser-compatible AWS SDK for JavaScript.
- Replace
IDENTITY_POOL_IDinfrontend/src/lambdaClient.jswith the ID of the identity pool created in Create an Identity pool and guest role. - Replace
REGIONinfrontend/src/lambdaClient.jswith the Region that is hosting the resources for this example. cd frontendnpm inpm run build- Launch an HTTP server at
dist. For example,npx http-server ./dist. - Navigate to the address of the server in your browser.
- Choose a color and pattern.
- Choose
Submit. - If the insertion was successful, you should see a success message.

