March 5, 2025

How to Trigger an AWS Lambda Function from a Database (Step-by-Step Guide)

Introduction

Imagine you have a database, and whenever new data is added, updated, or deleted, you want to trigger an AWS Lambda function automatically. This is useful for automating tasks such as sending notifications, processing data, or even updating another database.

In this guide, we will go through the entire process of triggering an AWS Lambda function from a database, using AWS DynamoDB Streams and AWS RDS (PostgreSQL/MySQL) with EventBridge.

We will cover:

  • What is AWS Lambda?
  • How to trigger Lambda from DynamoDB
  • How to trigger Lambda from RDS
  • Step-by-step setup
  • Complete code examples
  • Best practices

By the end, you will have a fully working solution that can trigger a Lambda function whenever your database is modified.


What is AWS Lambda?

AWS Lambda is a serverless computing service that runs your code automatically in response to events. You do not need to manage servers; AWS does it for you. You just write your function, and AWS executes it whenever a trigger event occurs.


Method 1: Trigger AWS Lambda from DynamoDB

DynamoDB Streams allow you to capture table activity and use it as a trigger for AWS Lambda. When an item in the table is added, updated, or deleted, the stream captures the event, and AWS Lambda processes it.

Steps to Set Up DynamoDB to Trigger Lambda

  1. Create a DynamoDB Table

    • Go to AWS Console → DynamoDB → Create Table
    • Add necessary attributes (e.g., id, name, email)
    • Enable DynamoDB Streams (New Image or Both)
  2. Create an AWS Lambda Function

    • Go to AWS Console → Lambda → Create Function
    • Choose "Author from scratch"
    • Select runtime (e.g., Python, Node.js)
  3. Add Permissions for DynamoDB Streams

    • Attach AWSLambdaDynamoDBExecutionRole to your Lambda function
  4. Configure DynamoDB as an Event Source for Lambda

    • In the Lambda console, click on "Add Trigger"
    • Select DynamoDB Streams and choose your table’s stream
    • Set Batch Size and enable trigger
  5. Write Lambda Code to Process Events

import json

def lambda_handler(event, context):
    for record in event['Records']:
        print(f"DynamoDB Record: {json.dumps(record)}")
        
        if record['eventName'] == 'INSERT':
            new_item = record['dynamodb']['NewImage']
            print(f"New item added: {new_item}")
        
    return {'statusCode': 200, 'body': 'Processed'}
  1. Test and Deploy
    • Save the function, test it by inserting/updating records in DynamoDB, and check logs in AWS CloudWatch.

Method 2: Trigger AWS Lambda from RDS (PostgreSQL/MySQL) using EventBridge

DynamoDB has built-in support for Lambda triggers, but if you use Amazon RDS (MySQL/PostgreSQL), you need Amazon EventBridge to notify Lambda of database changes.

Steps to Set Up RDS to Trigger Lambda

  1. Create an RDS Database (MySQL/PostgreSQL)

    • Go to AWS Console → RDS → Create Database
    • Choose MySQL or PostgreSQL
    • Enable Database Activity Streams (if supported)
  2. Create an EventBridge Rule

    • Go to AWS Console → EventBridge → Rules → Create Rule
    • Choose "Event Pattern"
    • Select AWS Service: RDS
    • Choose Event Type: RDS Database Activity Event
    • Target: AWS Lambda Function
  3. Create AWS Lambda Function to Process Events

import json

def lambda_handler(event, context):
    print(f"RDS Event: {json.dumps(event)}")
    
    for record in event['detail']['records']:
        print(f"Change in database: {record}")
    
    return {'statusCode': 200, 'body': 'Processed'}
  1. Grant Lambda Permissions

    • Attach AmazonEventBridgeFullAccess policy to your Lambda role
  2. Test by Modifying Data in RDS

    • Insert/update data in the database
    • Check logs in AWS CloudWatch

Best Practices for Using AWS Lambda with Databases

  1. Optimize Lambda Execution Time

    • Use appropriate memory allocation to improve performance.
    • Avoid unnecessary computations inside Lambda.
  2. Use Retry Mechanisms

    • Configure error handling and retries to avoid data loss.
  3. Secure Your Lambda Function

    • Follow the least privilege principle (only grant necessary permissions).
    • Store database credentials securely in AWS Secrets Manager.
  4. Monitor and Debug Efficiently

    • Use AWS CloudWatch for logging and monitoring events.
    • Set up alerts in AWS CloudWatch Alarms to notify on failures.
  5. Avoid Infinite Loops

    • Be careful when Lambda updates the same table that triggers it to prevent recursion.
  6. Optimize Costs

    • Use efficient batch processing for DynamoDB Streams.
    • Set appropriate timeout values to avoid unnecessary billing.

Conclusion

AWS Lambda provides a powerful way to process database events without managing servers. Whether you are using DynamoDB Streams or RDS with EventBridge, the setup is straightforward and scalable.

This guide covered everything you need:

  • Triggering Lambda from DynamoDB
  • Triggering Lambda from RDS
  • Step-by-step setup
  • Complete code examples
  • Best practices

Now, you can implement this in your own AWS environment and automate your database workflows with AWS Lambda.

Have questions? Drop them in the comments!