Update DynamoDB Items Using DynamoDB Streams and AWS Lambda

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

  • An AWS account.
  • A DynamoDB table (we'll use CustomerOrders as our example).
  • Basic knowledge of AWS Lambda and DynamoDB.
  • A DynamoDB stream enabled on your table.

Step 1: Enable DynamoDB Streams

First, make sure DynamoDB Streams is enabled on your table:

  1. Go to the DynamoDB Console.
  2. Select Your Table (CustomerOrders).
  3. Navigate to the "Exports and streams" tab.
  4. Enable DynamoDB Stream and select the view type that suits your needs (e.g., NEW_IMAGE for capturing the updated state of items).

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:

  1. Go to the AWS Lambda Console.
  2. Click "Create function" and choose "Author from scratch".
  3. Name your function (e.g., UpdateCustomerOrdersFunction).
  4. Select Python 3.x as the runtime.
  5. Choose a role with basic Lambda permissions or create a new role.
  6. Create the function.

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:


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:

  1. Go to the AWS Lambda Console.
  2. Select your Lambda function.
  3. Navigate to the "Configuration" tab.
  4. Click "Add trigger" and select DynamoDB.
  5. Choose the DynamoDB table (CustomerOrders).
  6. Select the stream ARN (if prompted).
  7. Configure batch size and other settings as needed.
  8. Add the trigger.

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:

  1. Insert or update an item in your DynamoDB table.
  2. Check the CloudWatch Logs for your Lambda function to see detailed execution logs and verify that the function is processing events as expected.

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!

To view or add a comment, sign in

More articles by Jefferson Gandolfi

Others also viewed

Explore content categories