DynamoDB Triggers: Types of Triggers and Everything You Need to Know
Triggers are generally pieces of code that automatically run and respond to new events that occur to trigger an AWS Lambda function. You can use the DynamoDB triggers to build the powerful and functional event-driven applications that occur on your streams or database.
This article discusses the basics of DynamoDB triggers. You will know the types of DynamoDB triggers, how to enable each type, and a few use cases.
Types of DynamoDB Triggers
There are two types of DynamoDB triggers. They include:
A. DynamoDB Streams
A DynamoDB stream is an ordered or programmed flow of information based on changes associated with items in a DynamoDB table. Whenever you enable a stream on a particular table, DynamoDB captures the information about each change to a data item within the table and stores the information in a stream.
The streams can trigger the Lambda function once you configure the function. You can set up your streams to trigger a Lambda function using the following steps:
Step 1: Confirm If Your System Meets All the Prerequisites
First, you should be conversed with the basic AWS Lambda operations. You should also confirm if your machine has the latest AWS version. The following command should help:
The response for the previous command should take the following format. Note that this illustration uses AWS SDK for Python Boto3.
Our response is as illustrated in the following:
Step 2: Create a DynamoDB Table and Proceed to Enable the Streams On It
Create the table for which you want to enable your streams. Note that you can also enable the streams on an existing table.
We use the AWS SDK FOR Python to create our table as shown in the following command line:
Step 3: Create an AWS Lambda Function
It’s time to create an AWS Lambda function. You also need to specify the DynamoDB stream as the trigger. The Python command is as follows:
The previous code creates a new Lambda function with the specified name, runtime, and execution role. It also triggers the function that listens for changes on the specified DynamoDB stream.
Step 4: Enable an Access to the Steam Records
Use the event parameter in the previous code to access your stream records to access the stream records. You also need to use the event.Records field to access the individual stream records. Each record has a dynamodb field which contains the stream record’s data.
Step 5: Enable an Access to the New Image
Proceed to enable an access to the new image through the event.Records[i].dynamodb.NewImage field. Once done, set the event.Records[i].dynamodb.OldImage to access the old image of the item before any changes.
Use the event.Records[i].eventName section to determine the type of change that occurred (e.g., “INSERT”, “MODIFY”, “REMOVE”) and use the event.Records[i].eventID field to uniquely identify each stream record.
Step 6: Confirm the ARN of Your Stream
Use the event.Records[i].eventSourceARN field to know the Amazon Resource Name (ARN) of the enabled stream.
Step 7: Process the Stream Records Appropriately
Processing your stream as desired enables you to perform any necessary actions. With the stream analysis, you can acknowledge the receipt of your stream records by returning from the function.
B. DynamoDB Triggers Using AWS CloudWatch Events
The second type of DynamoDB triggers is using the CloudWatch events. You can use this feature to set up the rules that match the selected events in the stream of DynamoDB events. The CloudWatch event troggers an AWS Lambda function whenever a rule matches an event.
The following is an example of the steps that are involved when creating a CloudWatch Events rule to trigger an AWS Lambda function whenever you add an item to your DynamoDB table. We use the AWS Management Console.
- Set up a DynamoDB Stream on the table that you want to monitor.
- Create a Lambda function that the trigger invokes.
- Create a CloudWatch Events rule with a pattern that matches the DynamoDB Stream events on which you want to trigger.
- Select the Lambda function as the target for the rule.
Now, whenever you add, modify, or delete an item, the corresponding event is sent to the DynamoDB Stream which triggers the CloudWatch Events rule and invokes the Lambda function.
This article is everything that you need to know about DynamoDB triggers. More importantly, we hope that you can enable the DyanmoDB triggers on your streams directly or using CloudWatchEvents.