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 pools
in the sidebar. - Choose
Create identity pool
. - Select the
Guest access
check box. - Choose
Next
. - Select
Create a new IAM role
. - Enter a role name in the
IAM role
field. - 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
Roles
in the sidebar. - Search for the guest role created in the preceding section.
- Choose the role link.
- Expand the default permissions policy on the
Permissions
tab. - Choose the
Edit
button. - 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-browser
in theTable name
field. The name of the table must match withTableName
in./frontend/src/index.js
. - For the
Partition key
, enter the key nameId
and the type ofNumber
. - Keep the
Sort key
blank. There are no queries in this example, so it's irrelevant. - Keep
Default settings
selected. - 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_ID
inlambda/src/ddbClient.js
with the ID of the identity pool created in Create an Identity pool and guest role. cd lambda
npm i
npm run build
- Create a compressed
.zip
file fromlambda/dist/index.mjs
.
- Open Lambda in the AWS Management Console.
- Choose
Create function
. - Enter the name
examplePutItem
in theFunction name
field. This name must matchFunctionName
infrontend/src/index.js
. - Expand the
Change default execution role
section. - Select
Create a new role from AWS policy templates
. - Enter a name in the
Role name
field. - Select the
Simple microservice permissions
option to grant the Lambda function access to DynamoDB. - Choose
Create function
. - In the
Code
tab of the function page, chooseUpload from
and select.zip file
. - Choose
Upload
and select the.zip
file created in step 4 of Bundle the Lambda function. - Choose
Save
. - (Optional) Test the function in the console by choosing
Test
and 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_ID
infrontend/src/lambdaClient.js
with the ID of the identity pool created in Create an Identity pool and guest role. - Replace
REGION
infrontend/src/lambdaClient.js
with the Region that is hosting the resources for this example. cd frontend
npm i
npm 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.