Trigger an email when a new record is created in DynamoDB


This post will show how to build a simple NodeJS application to trigger Email notifications automatically when an entry is added to a database. For this implementation, I’ll use AWS Lambda, AWS API Gateway, Amazon DynamoDB and Amazon SES as the AWS services and SLAppForge Sigma, the simple, browser-based IDE as the development tool.

Process flow

What I’m expecting to do is, configure two Lambda functions as add-db-entry and send-email. Firstly, expose an API Gateway HTTP endpoint which accepts POST requests with a JSON payload and triggers the add-db-entry Lambda function which processes the request and inserts a new entry to the feedback DynamoDB table. Then enable a DynamoDB Stream on that table. Configure the send-email Lambda to poll this DynamoDB Stream and, when it detects a new stream record, send an email to the relevant recipient using Amazon SES.

Getting Started

Let’s start with a free Sigma account and create a new project by entering just a few details as below. The base platform is AWS and the primary language is NodeJS.

project creation

Step 1: Creating add-db-entry Lambda and adding the API Gateway trigger

The project comes with a default Lambda function with the handler code snippet. Let’s rename it as add-db-entry. Then to define an API Gateway endpoint to accept requests and set it as the trigger for our Lambda, drag an API Gateway resource from the Resources panel on the left and drop it on top of the red coloured event variable of the function handler. Then on the configuration panel, fill the API details for a POST request (if you wish to make the API call from Sigma’s Toolbox, make sure to enable CORS in your API). When you click Inject after a successful configuration, the event variable would turn green.

API gateway trigger

The exposed HTTPS endpoint will accept POST requests with a JSON payload which carries data: Name, Email and Feedback. After integrating API Gateway with the Lambda function, the request payload received by the API will be injected to the Lambda function as the event object.

Sample JSON payload may look like this

"Name": "Kumudika",
"Email": "",
"Feedback": "Excellent"

Step 2: Extract values from the API request

After the API request payload passes data to the event object, we have to extract the necessary information such as the Name, Email and Feedback from the request and assign them to variables for further use as follows.

Step 3: Creating the DynamoDB table and adding a Put Document operation

Then drag-n-drop a DynamoDB entry from the resource panel and in the New Table tab, set:
Table Name = feedback
Partition Key = Name
Operation = Put Document
Enter Name, Email and Feedback as fields of the item (document), and click the Inject button.


Once you are done, the code will look like below.

Step 4: Creating send-email Lambda and adding the DynamoDB trigger

Click plus sign next to the root directory in the Project tab next to Resources panel and create a new NodeJS function file named send-email. Then drag the already existing DynamoDB resource from the resource panel and drop it on the event variable of the function handler. Ensure that the table feedback, and set:
batch size = 100,
Starting Position = Latest,
Streaming Mode = New Image (entry data after the update)and inject the trigger.

As in Step1, after a successful configuration, the event variable would turn green.

Step 5: Extracting the values from the trigger event

Add the following code into send-email.js file to extract values from DynamoDB trigger event.

Step 6: Sending the email

Now, drag-n-drop an SES resource, right before the return line of the send-email Lambda. For this article, let’s assume we have already verified our send and receive emails from AWS side. (If you haven’t, you can verify them via the Register A New Email tab of the resource configuration pane; in production, we would be requesting a SES limit increase from Amazon so that only the send address will have to be pre-verified) Then from the Use An Existing Verified Email tab, provide the relevant details.

ses configuration

You can test the email sending flow by giving your email address as the receiver's email. But before the actual deployment, change the receiver’s email address from the code to the variable email.

The final send-email.js code will look like this:

Step 7: Deploying the project

Once you are done with the code changes, let’s save everything. Click the Save Project button on the toolbar and commit your changes to GitHub/Bitbucket/GitLab. And then click the Deploy button and your project will be deployed to the AWS account you had configured in Sigma.
Once the deployment is successful, you’ll get the deployment summary view. Copy the API endpoint URL from the output parameter section(If you accidentally closed the deployment pop-up, you can always view the output parameters via the Deployment tab of Project → Show Info.).

changes summary

Step 8: HTTP request

Then, using an HTTP client like Postman, make a POST request to the above API endpoint URL with a message body as in the following structure.

"Name": "Kumudika",
"Email": "",
"Feedback": "Excellent"

If you have enabled CORS in Step 1, you can use Sigma’s own Toolbox to make the request.

sigma toolbox

If all goes well, you will receive an email to the given email address.