Using Lambda Function with Amazon DynamoDB


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) {
let tabledetails = JSON.parse(JSON.stringify(event.Records[0].dynamodb));
let customerid = tabledetails.NewImage.cust_id.S;
let name =;
let address = tabledetails.NewImage.address.S;
var eParams = {
Destination: {
ToAddresses: [""]
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: ""
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