javascriptv3/example_code/cross-services/lambda-for-browser/README.md
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
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:
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.
Identity pools in the sidebar.Create identity pool.Guest access check box.Next.Create a new IAM role.IAM role field.Next.Next.Create identity pool.Roles in the sidebar.Permissions tab.Edit button.{
"Version": "2012-10-17",
"Statement": [
{
"Action": "lambda:InvokeFunction",
"Resource": "*",
"Effect": "Allow"
}
]
}
Next.Save Changes.Create table.lambda-for-browser in the Table name field. The name of the table must match with TableName in ./frontend/src/index.js.Partition key, enter the key name Id and the type of Number.Sort key blank. There are no queries in this example, so it's irrelevant.Default settings selected.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.
IDENTITY_POOL_ID in lambda/src/ddbClient.js with the ID of the identity pool created in Create an Identity pool and guest role.cd lambdanpm inpm run build.zip file from lambda/dist/index.mjs.Create function.examplePutItem in the Function name field. This name must match FunctionName in frontend/src/index.js.Change default execution role section.Create a new role from AWS policy templates.Role name field.Simple microservice permissions option to grant the Lambda function access to DynamoDB.Create function.Code tab of the function page, choose Upload from and select .zip file.Upload and select the .zip file created in step 4 of Bundle the Lambda function.Save.Test and providing the following Event 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.
IDENTITY_POOL_ID in frontend/src/lambdaClient.js with the ID of the identity pool created in Create an Identity pool and guest role.REGION in frontend/src/lambdaClient.js with the Region that is hosting the resources for this example.cd frontendnpm inpm run builddist. For example, npx http-server ./dist.Submit.