Update DynamoDB Items Using DynamoDB Streams and AWS Lambda
In today's fast-paced world, keeping your data synchronized and up-to-date is crucial. AWS DynamoDB Streams combined with AWS Lambda provides a seamless way to automate updates and reactions to changes in your DynamoDB tables. In this guide, I'll show you how to use DynamoDB Streams and AWS Lambda to automatically update items in a DynamoDB table, using an example table named CustomerOrders.
What You’ll Need
Step 1: Enable DynamoDB Streams
First, make sure DynamoDB Streams is enabled on your table:
Step 2: Create a Lambda Function
Now, let’s create a Lambda function that will process events from the DynamoDB stream and update items accordingly:
Here's the code for the Lambda function that updates items in the CustomerOrders table when new images are inserted or existing items are modified:
Recommended by LinkedIn
import json
import boto3
import logging
from datetime import datetime
# Configure logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
dynamodb = boto3.resource('dynamodb')
table_name = 'CustomerOrders'
table = dynamodb.Table(table_name)
def lambda_handler(event, context):
logger.info('Event: %s', json.dumps(event, indent=2))
for record in event['Records']:
logger.info('Processing record: %s', json.dumps(record, indent=2))
if record['eventName'] in ['INSERT', 'MODIFY']:
new_image = record['dynamodb']['NewImage']
# Extracting key attributes
item_key = {
'OrderId': new_image['OrderId']['S'],
'CustomerId': new_image['CustomerId']['S']
}
logger.info('Item key: %s', json.dumps(item_key, indent=2))
# Prepare update expression and attribute values
update_expression = 'SET LastUpdated = :val'
expression_attribute_values = {
':val': int(datetime.utcnow().timestamp())
}
logger.info('Update expression: %s', update_expression)
logger.info('Expression attribute values: %s', json.dumps(expression_attribute_values, indent=2))
# Perform the update operation
try:
response = table.update_item(
Key=item_key,
UpdateExpression=update_expression,
ExpressionAttributeValues=expression_attribute_values
)
logger.info('Update response: %s', json.dumps(response, indent=2))
except Exception as e:
logger.error('Update failed: %s', str(e))
return {
'statusCode': 200,
'body': json.dumps('Update successful')
}
Step 3: Configure Lambda to Trigger on DynamoDB Stream Events
To connect your Lambda function with DynamoDB Streams:
Step 4: Test Your Lambda Function
To ensure everything is set up correctly, test your Lambda function by inserting or modifying items in the CustomerOrders table. Here’s how:
Conclusion
By leveraging DynamoDB Streams and AWS Lambda, you can automate the process of updating DynamoDB items based on stream events. This setup not only keeps your data in sync but also allows you to perform automated tasks efficiently. Whether you're handling customer orders, tracking inventory, or managing any other type of data, this approach can streamline your operations and enhance your application's responsiveness. Happy coding!