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.
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:
- 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.
- 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
- 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.
- 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.
- 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:
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, anddynamodb:ListStreams. - SQS SendMessage: Allow the function to utilize
sqs:SendMessage.
Summary Table of the Process
| Component | Description |
| DynamoDB Streams | Represents item-level modifications in a DynamoDB table allowing for event-driven responses. |
| AWS Lambda Function | Processes stream records and forwards messages to SQS. |
| SQS | Receives messages for distribution to other systems or components in the architecture. |
| IAM Role | Ensures 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
- How to efficiently compute average on the fly moving average?
- How to efficiently create Kafka topics with testcontainers?
- how to efficiently merge int ranges in a stream?
- how to efficiently move data from Kafka to an Impala table?
- How to edit files in AWS S3 in the browser?
- How to edit httpd.conf file in AMAZON EC2
- How to easily remember Red-Black Tree insert and delete?
- How to efficiently check if a list of consecutive numbers is missing any elements

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.