| by Arround The Web | No comments

DynamoDB Triggers: Types of Triggers and Everything You Need to Know

DynamoDB triggers work like triggers in any other database. The feature enables the automatic execution of various custom actions based on an array of item-level level updates on your DynamoDB table or index. It remains the most efficient method to capture the method-level or item-level changes on an item within your DynamoDB table.

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:

aws version

The response for the previous command should take the following format. Note that this illustration uses AWS SDK for Python Boto3.

aws-cli/2.x.x Python/3.x.x Linux/4.x.x-xxx-std botocore/2.x.x

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:

import boto3

# Create a DynamoDB client
dynamodb = boto3.client('dynamodb')

# Create a table with a primary key 'PK', and a sort key 'SK'
table_name = 'my-table-name'
attribute_definitions = [
        'AttributeName': 'PK',
        'AttributeType': 'S'
        'AttributeName': 'SK',
        'AttributeType': 'S'
key_schema = [
        'AttributeName': 'PK',
        'KeyType': 'HASH'
        'AttributeName': 'SK',
        'KeyType': 'RANGE'
provisioned_throughput = {
    'ReadCapacityUnits': 10,
    'WriteCapacityUnits': 10

        'StreamEnabled': True,
        'StreamViewType': 'NEW_AND_OLD_IMAGES'

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:

import boto3

# Create a Lambda client
lambda_client = boto3.client('lambda')

# Set up the function details
function_name = 'my-function'
runtime = 'python3.11'
role = 'arn:aws:iam::x123456789012y:role/lambda-basic-execution'
handler = 'function.handler'

# Create the function
    # Other function configurations, such as environment variables, VPC settings, etc.

# Get the Amazon Resource Name (ARN) of the function
function_arn = lambda_client.get_function(FunctionName=function_name)['Configuration']['FunctionArn']

# Set up the stream trigger details
stream_arn = 'arn:aws:dynamodb:eu-west-1:123456789012:table/my-table/stream/2022-01-02T00:00:00.000'
starting_position = 'LATEST'

# Add the trigger to the function
    # Other configuration options, including batch size, maximum retry attempts, etc.

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.

Share Button

Source: linuxhint.com

Leave a Reply