In today’s data-driven world, businesses need to analyze real-time data to make informed decisions. Amazon DynamoDB, a fully managed NoSQL database, is a popular choice for storing high-velocity transactional data. However, for advanced analytics and reporting, organizations often rely on Amazon Redshift, a fully managed data warehouse.
But how do you bridge the gap between DynamoDB and Redshift to enable real-time data integration? In this blog, we’ll walk through a step-by-step guide to achieve this using AWS Lambda, ensuring your data flows seamlessly from DynamoDB to Redshift.
Why Integrate DynamoDB with Redshift?
DynamoDB is excellent for handling high-throughput, low-latency workloads, but it’s not designed for complex queries or large-scale analytics. On the other hand, Redshift excels at running analytical queries on massive datasets. By integrating the two, you can:
- Enable Real-Time Analytics: Analyze transactional data as soon as it’s ingested into DynamoDB.
- Centralize Data: Combine DynamoDB data with other datasets in Redshift for a unified view.
- Improve Reporting: Power business intelligence tools like Tableau or QuickSight with up-to-date data.
Architecture Overview
Here’s how the integration works:

Figure 1: High-level architecture of DynamoDB to Redshift integration using Lambda.
Step-by-Step Implementation
Let’s dive into the implementation details.
1. Set Up a DynamoDB Table

First, create a DynamoDB table to store your transactional data. For this example, let’s create a table named Transactions
with the following schema:
- Partition Key:
TransactionID
(String) - Attributes:
CustomerID
(String),Amount
(Number),Timestamp
(String)
Enable DynamoDB Streams on the table to capture changes. DynamoDB Streams will act as the source of truth for all data modifications.
2. Create an Amazon Redshift Cluster
Next, set up an Amazon Redshift cluster. Once the cluster is ready, create a table to store the transaction data:

CREATE TABLE transactions (
TransactionID VARCHAR(255) PRIMARY KEY,
CustomerID VARCHAR(255),
Amount DECIMAL(10, 2),
Timestamp TIMESTAMP
);


Make note of the Redshift cluster endpoint, database name, and credentials, as you’ll need them to connect from the Lambda function.

3. Create an IAM Role for Lambda

AWS Lambda needs permissions to read from DynamoDB Streams and write to Redshift. Create an IAM role with the following permissions:
dynamodb:DescribeStream
dynamodb:GetRecords
dynamodb:GetShardIterator
dynamodb:ListStreams
redshift:ExecuteStatement
redshift-data:ExecuteStatement
Attach the AmazonRedshiftDataFullAccess
and AmazonDynamoDBReadOnlyAccess
policies to the role.
4. Create an AWS Lambda Function
Create a Lambda function named DynamoDBToRedshiftIntegration
with Python 3.9 as the runtime. Attach the IAM role you created earlier.
Here’s the Python code for the Lambda function:

import json
import psycopg2
from psycopg2 import sql
# Redshift connection details
REDSHIFT_HOST = "your-redshift-cluster-endpoint"
REDSHIFT_PORT = "5439"
REDSHIFT_DB = "your-database"
REDSHIFT_USER = "your-username"
REDSHIFT_PASSWORD = "your-password"
def lambda_handler(event, context):
# Initialize Redshift connection
conn = psycopg2.connect(
host=REDSHIFT_HOST,
port=REDSHIFT_PORT,
dbname=REDSHIFT_DB,
user=REDSHIFT_USER,
password=REDSHIFT_PASSWORD
)
cursor = conn.cursor()
# Process DynamoDB stream records
for record in event['Records']:
if record['eventName'] == 'INSERT':
new_image = record['dynamodb']['NewImage']
transaction_id = new_image['TransactionID']['S']
customer_id = new_image['CustomerID']['S']
amount = new_image['Amount']['N']
timestamp = new_image['Timestamp']['S']
# Insert data into Redshift
insert_query = sql.SQL("""
INSERT INTO transactions (TransactionID, CustomerID, Amount, Timestamp)
VALUES (%s, %s, %s, %s)
""")
cursor.execute(insert_query, (transaction_id, customer_id, amount, timestamp))
conn.commit()
cursor.close()
conn.close()
return {
'statusCode': 200,
'body': json.dumps('Data inserted into Redshift successfully!')
}
5. Configure the Lambda Trigger
Set up a trigger for the Lambda function using DynamoDB Streams. Choose the Transactions
table and the LATEST
stream view type. This ensures that the Lambda function is invoked whenever a new record is inserted into DynamoDB.


6. Test the Integration
Insert a sample record into the DynamoDB Transactions
table:

{
"TransactionID": "12345",
"CustomerID": "67890",
"Amount": 100.50,
"Timestamp": "2023-10-01T12:34:56Z"
}

Check the Redshift transactions
table to verify that the data has been inserted successfully.
Best Practices
- Error Handling: Implement retry logic and error logging in the Lambda function to handle failures gracefully.
- Batch Processing: Process records in batches to improve performance and reduce the number of Redshift queries.
- Monitoring: Use Amazon CloudWatch to monitor Lambda function execution and DynamoDB Streams metrics.
- Security: Ensure that IAM roles and Redshift credentials are securely managed.
By integrating DynamoDB with Redshift using AWS Lambda, you can unlock the power of real-time analytics and reporting. This setup is scalable, cost-effective, and easy to implement, making it an excellent choice for businesses looking to derive insights from their transactional data.
Whether you’re building a real-time dashboard or performing complex analytics, this integration ensures that your data is always up-to-date and ready for analysis. Give it a try, and let us know how it works for your use case!