Use Lambda with Node.js to Insert Data Into DynamoDB
Table of Contents
- Introduction
- Prerequisites
- Create AWS Role for Lambda to Access DynamoDB
- Create Node.js Code for Lambda
- Prepare Package to Deploy to AWS Lambda
- Test Lambda Function
- Conclusion
Introduction
This blog shows how to create an AWS Lambda with Node.js and insert a record into DynamoDB. It also shows how to manually deploy Lambda as a package to AWS from your local development environment.
Prerequisites
-
Have an AWS Account
-
Setup AWS CLI Config
-
Create AWS DynamoDB Table
-
Install Node.js on Mac
Create AWS Role for Lambda to Access DynamoDB
Here, we'll create a new Lambda policy to access DynamoDB. Go to the AWS Roles and click Create role.
Choose Lambda service and click add permission.
At the attach permissions policies page, click create policy. You can use either the visual editor or JSON view. What we are going to do here is provide the Lambda with DynamoDB PutItem permission. Following is a code snippet showing the JSON view. As you can see, this policy allows the dynamodb PutItem operation because our code later will use PutItem to add a record to DynamoDB.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "dynamodb:PutItem",
"Resource": "*"
}
]
}
Create Node.js Code for Lambda
Now, we'll go back to our local development environment and write some code. First
we'll need to set up the Node.js package file. You can run the following command to
create a default package file. This will create a
package.json file.
Create package.json File
npm init -y
Create Lambda Node.js Code
Run this command to open the command editor.
nano index.js
At the command editor, paste the following code and save the index.js file. This will be the file that we'll deploy to Lambda later.
The following assumes we already have a table called
S3BucketImages. You can go to AWS DynamoDB to create the
table and make sure the table's region matches your code. Set the Partition
key as UniqueKey.
onst AWS = require('aws-sdk');
const dynamodbClient = new AWS.DynamoDB.DocumentClient();
exports.handler = (event, context, callback) => {
AWS.config.update({
region: "us-west-1",
});
let putRequest = {
TableName: 'S3BucketImages',
Item: {
"UniqueKey": new Date().toString()
}
};
await dynamodbClient.put(putRequest).promise()
.then((data) => {
console.info('successfully update to dynamodb', data)
})
.catch((err) => {
console.info('failed adding data dynamodb', err)
});
};
Prepare Package to Deploy to AWS Lambda
Now, we'll start to prepare the deployment file as a zip file and deploy it to AWS
using the AWS CLI command line. The code we have only has a dependency on
aws-sdk, so let's install it. This will create node_modules in
your local folder.
npm install aws-sdk
Run this command at your terminal to include our index.js and node_modules and create a zip file.
zip -r index.zip index.js node_modules
We'll then run the command below to create a Lambda function. You'll need to replace the AWS account ID and the role created earlier.
aws lambda create-function --function-name my-test-lambda \
--zip-file fileb://index.zip --handler index.handler --runtime nodejs12.x \
--timeout 10 --memory-size 1024 \
--role arn:aws:iam::{Your-AWS-Account-ID}:role/{The-AWS-Role-Can-Access-DynamoDB}
Test Lambda Function
This should be pretty much everything you need to access DynamoDB from Lambda. You can use the Test tab in the Lambda console to make sure it actually adds a new record into DynamoDB.
Conclusion
In this blog, we walked through how to create an AWS Lambda function with Node.js that inserts data into DynamoDB. We also covered how to create the necessary IAM role, package the code, and deploy it manually using the AWS CLI. This approach is useful for quick prototyping or small-scale deployments.