Configure Amazon DynamoDB triggers with AWS Lambda

Borhen Cherif
3 min readMar 22, 2021

--

Enable Streams on a DynamoDB Table

DynamoDB Streams are designed to allow external applications to monitor table updates and react in real-time.

Enable DynamoDB Streams on a table, an ordered flow of record modifications will become available via a custom API endpoint. Basically, every time the action of creating, update or delete records from the table, DynamoDB will write a new stream record containing the corresponding record data.

Implement Lambda Function batch processing logic

Create a new Lambda Function and connect it to the DynamoDB Stream. The table will contain basic data about users, defining only the following fields:

  • Id: Primary Key (PK) for the table.
  • Email: The user’s email address.
  • IsPersonalEmail: An automatically computed field, based on the Email value.

Implement a simple trigger to keep the IsPersonalEmail and Email fields synchronized: every time a new record is created, the Lambda Function will add the computed field (IsPersonalEmail). Additionally, each time a record is updated, the two fields will keep in sync.

The following is a list of possible scenarios to account for:

  1. A new record is created: The Lambda Function initializes IsPersonalEmail with the correct value.
  2. A record is modified, but Email hasn’t changed: No operation.
  3. A record is modified, Email has changed, but IsPersonalEmail is still the same: No operation.
  4. A record is modified, Email has changed and IsPersonalEmail needs to be updated: The record will be modified.

The Lambda handler is split into three functions:

  • is_personal_email: Checks whether the given email contains a commercial email domain commonly used for personal accounts
  • update_record: Updates the DynamoDB record, given an ID and a boolean value
  • lambda_handler: Analyzes the given batch of records and extracts the data for each record
  • Add a trigger to execute the function whenever a change is made to the table.

Test the DynamoDB Trigger

Verify that the trigger created in the previous step is working properly by creating an item in the database. The item will create a stream record that will then trigger the Lambda function via the stream.

--

--

Borhen Cherif
Borhen Cherif

Written by Borhen Cherif

System Administrator | RHCSA | Devops | Cloud

No responses yet