Using Lambda Function with Amazon DynamoDB

Requisites:

To use Amazon DB and AWS Lambda, we need to follow the steps as shown below −

  1. Create a table in DynamoDB with a primary key
  2. Create a role that will have permission to work with DynamoDB and AWS Lambda.
  3. Create a function in AWS Lambda
  4. AWS Lambda Trigger to send mail
  5. Add data in DynamoDB
  1. Creating a table called the customer in Dynamodb table and how to enter data in that table.
  2. Triggering AWS Lambda function once the data is entered and sending mail using Amazon SES service.

AWS Lambda Trigger to Send Mail

AWS Lambda will get triggered when data is inserted into AWS

Sending an email

To send an email, you need to follow the steps 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");
}
});
}

Add Data in DynamoDB

Use the following sequence to add data in DynamoDB.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store