Guided Lab: Getting Started with Amazon Simple Queue Service
Description
Amazon SQS is a fully managed message queuing service that enables you to decouple and scale microservices, distributed systems, and serverless applications. It allows you to send, store, and receive messages between software components at any volume without losing messages or requiring other services to be available.
Use Case Example: Consider an e-commerce platform like Amazon, where customers place orders for products to be delivered. When an order is placed, it triggers several backend processes: inventory management, payment processing, packaging, and shipping. Using SQS, each process can communicate asynchronously through messages in a queue. This decouples the components, allowing them to operate independently, scale efficiently, and handle failures gracefully.
In this lab, you will explore the basics of SQS, including how to create queues, send and receive messages manually, and integrate SQS with AWS Lambda functions for automated message processing.
Prerequisites
This lab assumes you have the following:
- Basic knowledge and understanding of AWS services, Amazon SQS, and AWS Lambda
- Familiarity with Python programming language.
If you find any gaps in your knowledge, consider taking the following lab:
Objectives
By the end of this lab, you will:
- Understand the fundamentals of Amazon SQS.
- Create an SQS queue and manually send and receive messages.
- Develop a Lambda function to send messages to the SQS queue.
- Create another Lambda function to receive messages from the SQS queue.
Lab Steps
Exploring Amazon SQS
1. Navigate to the Amazon SQS service:
- Use the search bar to find “SQS,” then select it from the list of services.
2. Create a new queue:
a. Click on “Create queue”.
b. Choose “Standard queue” for this lab.
Standard queues offer maximum throughput, best-effort ordering, and at least-once delivery.
c. Enter a name for your queue (e.g., OrderQueue).
d. Keep the default settings and click “Create Queue”.
3. Take note of your SQS Queue URL; we will need this URL for our next steps.
4. Send Messages Manually
a. Click on “Send and receive messages”.
b. In the “Message body” field, enter the following JSON:
{
"orderId": "12345",
"product": "Book",
"quantity": 1
}
This represents an order message similar to a customer purchasing a book.
c. Before sending the message, take your time to explore the dashboard and check the progress of the Receive messages, Messages, and Polling.
d. Click “Send message”.
e. You will receive a pop-up message similar to the image below:
f. Click on View details to review the details
g. Click Done
5. Receive and Poll Messages
a. In the same window, click “Poll for messages”.
Polling checks the queue for available messages.
b. Observe new messages were added and take a look at the Polling progress:
6. View the message:
a. Once the message appears, click on it to view its details.
b.Click on Done.
7. Delete the message:
a. After reviewing, select the checkbox and click “Delete” to remove it from the queue.
Deleting messages after processing prevents them from being reprocessed.
b. Confirm by clicking Delete.
Creating a Lambda Function to Send Messages to SQS
1. Navigate to AWS Lambda: Search for “Lambda” in the AWS Management Console.
2. Create a new Lambda function using the following configurations:
- Choose Author from scratch.
- Function name: SendMessageFunction
- Select Python 3.12 as the runtime.
- Execution role:
- Select Use an Existing Role: PlayCloud-Sanbox
- Click Create function
3. Edit the function code:
a. In the code editor, replace the default code with the following:
import json
import boto3
import random
def lambda_handler(event, context):
sqs = boto3.client('sqs')
queue_url = '<YOUR_QUEUE_URL>'
# Generate multiple messages for different orders
orders = [
{"orderId": str(random.randint(10000, 99999)), "product": "Book", "quantity": 1},
{"orderId": str(random.randint(10000, 99999)), "product": "Laptop", "quantity": 2},
{"orderId": str(random.randint(10000, 99999)), "product": "Headphones", "quantity": 1},
{"orderId": str(random.randint(10000, 99999)), "product": "Camera", "quantity": 1},
]
for order in orders:
# Send each order as a message
response = sqs.send_message(
QueueUrl=queue_url,
MessageBody=json.dumps(order)
)
print(f"Sent order {order['orderId']} - Response: {response['MessageId']}")
return {
'statusCode': 200,
'body': json.dumps('All orders sent successfully!'),
'orders: ' : orders
}
Take your time to review the code:
This function sends a message to the specified SQS queue using the event data.
b. Update the queue URL:
-
-
- Replace <YOUR_QUEUE_URL> with the URL of your OrderQueue.
- Find the queue URL in the SQS console under “Details.”
-
c. Save the code:
-
-
- Click “Deploy” to save your changes.
-
4. Test the Lambda Function
a. Click “Test” in the Lambda console.
b. Enter an event name (e.g., TestOrderEvent).
c. Leave the Template hello-world and the Event JSON as is.
d. Click Save.
e. Run the test by clicking “Test” again.
This simulates an event that triggers the Lambda function.
f. Verify the result:
g. Check the SQS queue to confirm the message was received. Go to Amazon SQS > Queues > OrderQueue > Send and receive messages.
NOTE: DO NOT Poll the messages yet. Another Lambda will do it for us.
Creating a Lambda Function to Receive Messages from SQS
1. Create a new Lambda function using the following configurations:
- Choose Author from scratch.
- Function name: ReceiveMessageFunction
- Select Python 3.12 as the runtime.
- Execution role:
- Select Use an Existing Role: PlayCloud-Sandbox
- Click the Create function.
2. Edit the function code:
- Replace the default code with:
import json
import boto3
import random
def lambda_handler(event, context):
sqs = boto3.client('sqs')
queue_url = '<YOUR_QUEUE_URL>'
# List of available delivery trucks
delivery_trucks = ['Truck-1', 'Truck-2', 'Truck-3', 'Truck-4']
# Randomly choose the number of messages to process (between 1 and 4)
num_messages_to_process = random.randint(1, 4)
# Poll messages (up to the randomly chosen number)
response = sqs.receive_message(
QueueUrl=queue_url,
MaxNumberOfMessages=num_messages_to_process,
WaitTimeSeconds=10
)
messages = response.get('Messages', [])
if not messages:
return {
'statusCode': 200,
'body': 'No messages to process.'
}
# Store processed order details for the response
processed_orders = []
for message in messages:
order = json.loads(message['Body'])
# Randomly assign the order to a delivery truck
assigned_truck = random.choice(delivery_trucks)
# Record the processed order details
processed_order = {
"orderId": order['orderId'],
"product": order['product'],
"quantity": order['quantity'],
"assignedTruck": assigned_truck
}
processed_orders.append(processed_order)
# Simulate order processing log
print(f"Order {processed_order['orderId']} - Product: {processed_order['product']} "
f"(Quantity: {processed_order['quantity']}) has been assigned to {processed_order['assignedTruck']} "
f"for delivery.")
# Delete the message from the queue after processing
sqs.delete_message(
QueueUrl=queue_url,
ReceiptHandle=message['ReceiptHandle']
)
# Return the response as a dictionary to avoid escape characters
return {
'statusCode': 200,
'body': {
'message': 'Processed random orders and assigned to delivery trucks!',
'processedOrders': processed_orders
}
}
Take your time to review the code:
This function receives a message from the queue, processes it, and deletes it.
3. Update the queue URL:
- Replace <YOUR_QUEUE_URL> with your queue’s URL.
4. Save the code:
- Click “Deploy.”
5. Test the Lambda Function
a. Since we have already used the SendMessageFunction to add messages to the queue, we can proceed with testing.
b. Click “Test” in the Lambda console.
c. Enter an event name (e.g., TestReceivedEvent).
d. Leave the Template hello-world and the Event JSON as is.
e. Click Save
f. Run the test by clicking “Test” again.
This simulates an event that triggers the Lambda function.
g. Verify the result:
h. Check the SQS console to ensure the message count has decreased. Go to Amazon SQS > Queues > OrderQueue > Send and receive messages. Refresh if you must.
i. Run the Test event in the ReceiveMessageFunction again until the Message available in your queue is Zero ( 0 ) “
-
-
- again…
-
-
-
- and again…
-
-
-
- Then, lastly, check the available message in your queue
-
Congratulations on completing the Amazon SQS lab! You have gained hands-on experience creating and managing SQS queues and integrating AWS Lambda functions for automated message processing. By leveraging Amazon SQS, you’ve learned how to easily build scalable, decoupled systems capable of handling high volumes of messages.
As a best practice, remember to delete any resources no longer in use, such as SQS queues and Lambda functions, to maintain a clutter-free AWS environment.
Thank you for joining this lab, and happy learning!