DynamoDB
SQS
AWS
message queue
tutorial

How to Dynamodb send message to SQS

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Amazon Web Services (AWS) offers a robust set of tools that enable developers to build highly scalable and distributed systems. Among these are Amazon DynamoDB, a NoSQL database service, and Amazon Simple Queue Service (SQS), a fully managed message queuing service. Integrating these two services allows for seamless data processing and communication between distributed components. This article will provide a detailed walkthrough on how to configure DynamoDB to send messages to SQS, discussing the use of DynamoDB Streams and AWS Lambda functions.

DynamoDB Streams

DynamoDB Streams capture a time-ordered sequence of item-level modifications in a table. These streams can be used to trigger AWS Lambda functions, which can then further process the data or send messages to other AWS services, such as SQS.

Setting Up DynamoDB Streams

To configure DynamoDB to work with SQS, you must first enable DynamoDB Streams on your table:

  1. Create a DynamoDB Table: Ensure your DynamoDB table is up and running. It should contain items that, when modified, require communication to an SQS queue.
  2. Enable DynamoDB Streams:
    • Access the AWS Management Console.
    • Navigate to the DynamoDB page and select your table.
    • Under the "Exports and streams" tab, enable streams by selecting the appropriate "Stream view type":
      • KEYS_ONLY: Only the keys of the modified item.
      • NEW_IMAGE: The item after it was modified.
      • OLD_IMAGE: The item before it was modified.
      • NEW_AND_OLD_IMAGES: Both before and after images.

Lambda Function as an Intermediary

AWS Lambda serves as an intermediary, processing changes recorded in the DynamoDB Streams and sending them to an SQS queue. It is designed to respond to events and can easily be triggered by record changes in DynamoDB.

Creating a Lambda Function

  1. Create a New Lambda Function:
    • Go to the AWS Lambda service in the AWS Management Console.
    • Select "Create Function" and choose "Author from scratch."
    • Provide a name, such as DynamoToSQSFunction, and an appropriate runtime, such as Python or Node.js.
  2. Configure DynamoDB Stream as Event Source:
    • While setting up the Lambda function, add the DynamoDB stream as a trigger.
    • Select the correct stream for your table and configure required settings.
  3. Implement Message Sending Logic:
    • Use an AWS SDK (such as boto3 for Python) to send messages from Lambda to SQS.
    • Sample Python code to send a message to SQS:
python
1     import json
2     import boto3
3
4     def lambda_handler(event, context):
5         sqs = boto3.client('sqs')
6         queue_url = 'https://sqs.us-east-1.amazonaws.com/123456789012/MyQueue'
7         
8         for record in event['Records']:
9             if record['eventName'] == 'INSERT':
10                 new_image = record['dynamodb']['NewImage']
11                 message_body = {
12                     'id': new_image['id']['S'],
13                     'value': new_image['value']['S']
14                 }
15                 response = sqs.send_message(
16                     QueueUrl=queue_url,
17                     MessageBody=json.dumps(message_body)
18                 )
19                 print(f"Message sent: {response['MessageId']}")
20

Sending Messages to SQS

Each item modification recorded in the DynamoDB Stream can be processed by the Lambda function, which formats the data as needed and sends it to the designated SQS queue. Ensure the Lambda function has the necessary permissions to publish messages to SQS.

Setting Permissions

  • IAM Role for Lambda: Assign an IAM role to the Lambda function with policies allowing:
    • DynamoDB Stream Reading: dynamodb:GetRecords, dynamodb:GetShardIterator, dynamodb:DescribeStream, and dynamodb:ListStreams.
    • SQS SendMessage: Allow the function to utilize sqs:SendMessage.

Summary Table of the Process

ComponentDescription
DynamoDB StreamsRepresents item-level modifications in a DynamoDB table allowing for event-driven responses.
AWS Lambda FunctionProcesses stream records and forwards messages to SQS.
SQSReceives messages for distribution to other systems or components in the architecture.
IAM RoleEnsures appropriate permissions are granted to Lambda for accessing DynamoDB Streams and sending to SQS.

Additional Considerations

  • Error Handling: Implement robust error handling within the Lambda function to manage edge cases and potential failures during the send operation.
  • Testing: Before full deployment, thoroughly test the end-to-end process in a staging environment to ensure all configurations and permissions are correct.
  • Monitoring and Alerts: Utilize AWS CloudWatch to monitor the performance and success of the Lambda executions, and set up alerts for failure cases.

Integrating DynamoDB with SQS through Streams and AWS Lambda enables powerful event-driven architectures, providing scalability, reliability, and improved communication across system components. This setup allows even complex distributed systems to efficiently manage data changes and deliver messages automatically, streamlining data processing workflows.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.