Using Lambda Function with Amazon DynamoDB
DynamoDB can trigger AWS Lambda when the data is added to the tables, updated, or deleted. In this chapter, we will work on a simple example that will add items to the DynamoDB table and AWS Lambda which will read the data and send a mail with the data added.
Requisites:
To use Amazon DB and AWS Lambda, we need to follow the steps as shown below −
- Create a table in DynamoDB with a primary key
- Create a role that will have permission to work with DynamoDB and AWS Lambda.
- Create a function in AWS Lambda
- AWS Lambda Trigger to send mail
- Add data in DynamoDB
Let us discuss each of these steps in detail.
Example:
We are going to work out the following example which shows the basic interaction between DynamoDB and AWS Lambda. This example will help you to understand the following operations −
- Creating a table called the customer in Dynamodb table and how to enter data in that table.
- Triggering AWS Lambda function once the data is entered and sending mail using Amazon SES service.
The basic block diagram that explains the flow of the example is as shown below −
Create Table in DynamoDB with Primary Key:
Log in to the AWS console. Go to AWS Services and select DynamoDB as shown below. Select DynamoDB.
DynamoDB shows the options as shown below −
Now, click Create table to create the table as shown. We have named the table as a customer with the primary key for that table as cust_id. Click on the Create button to add the table to Dynamodb.
The table created is as shown below −
We can add items to the table created as follows −
Click Items and click the Create item button as shown −
Creating Role with Permissions to Work with DynamoDB and AWS Lambda
To create a role, Go to AWS services and click IAM.
Let us create a policy to be used only for the DynamoDB table created earlier
Now, choose a Service. Observe that the service we have selected is DynamoDB. For Actions, we have taken all actions i.e access to the list, read, and write. For resources, we will select the table resource type actions. When you click it, you can see a screen as follows −
Now, select the table and Add ARN to it as shown. We will get ARN details from the customer table created as shown below −
Enter ARN details here −
Click the Add button to save the changes. Once done click on the Review policy. Enter the name of the policy, description, etc as shown below −
Click on to save it. Add the policy to the role to be created. Select the Role from the left side and enter the details.
Observe that the policies added are and amazon SES full access. Add the role and will use it while creating the AWS Lambda function. Thus, we have created the Lambda function called as shown.
Now, let us add DynamodDB trigger to the AWS Lambda created. The runtime we shall use is Node.js.
You can find the following details in Dynamodb trigger that are to be configured for AWS Lambda −
Now, simply click Add to add the trigger to AWS Lambda.
AWS Lambda Trigger to Send Mail
AWS Lambda will get triggered when data is inserted into AWS
Lambda. The event parameter will have the Dynamodb data inserted. This will read the data from the event and send an email.
Sending an email
To send an email, you need to follow the steps given below −
Step 1: Go to AWS service and select SES (simple email service). Validate the email to which we need to send an email as shown −
Step 2: Click the button Verify new Email Address to add the email address.
Step 3: Enter an email address to verify it. The email address will receive an activation mail from Amazon which needs to be clicked. Once the activation is done, the email id is verified and can be used with AWS services.
Step 4: The AWS Lambda code which reads data from the event and sends an email is given below −
var aws = require('aws-sdk');
var ses = new aws.SES({
region: 'us-east-1'
});
exports.handler = function(event, context, callback) {
console.log(event);
let tabledetails = JSON.parse(JSON.stringify(event.Records[0].dynamodb));
console.log(tabledetails.NewImage.address.S);
let customerid = tabledetails.NewImage.cust_id.S;
let name = tabledetails.NewImage.name.S;
let address = tabledetails.NewImage.address.S;var eParams = {
Destination: {
ToAddresses: ["XXXXX@gmail.com"]
},
Message: {
Body: {
Text: {
Data: "The data added is as follows:\n CustomerId:"+customerid+"\n Name:"+name+"\nAddress:"+address
}
},
Subject: {
Data: "Data Inserted in Dynamodb table customer"
}
},
Source: "xxxxx@gmail.com"
};
console.log('===SENDING EMAIL===');
var email = ses.sendEmail(eParams, function(err, data) {
if (err) console.log(err);
else {
console.log("===EMAIL SENT==="); console.log("EMAIL CODE END");
console.log('EMAIL: ', email); context.succeed(event);
callback(null, "email is send");
}
});
}
Now, save the Lambda function and data in the DynamoDB table.
Add Data in DynamoDB
Use the following sequence to add data in DynamoDB.
Step 1: Go to the table customer created in Dynamodb.
Step 2: Click Create item.
Step 3:Click the Save button and check the email id provided in AWS Lambda to see if the mail has been sent by AWS Lambda.