0
0
AWScloud~5 mins

Sending and receiving messages in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes apps need to talk to each other without being connected all the time. Sending and receiving messages lets apps pass information safely and reliably, even if one app is busy or offline.
When you want to send a task to another app to do later without waiting.
When you need to keep messages safe until the receiver is ready.
When you want to connect different parts of your system without them crashing each other.
When you want to handle many messages quickly and in order.
When you want to retry sending messages if something goes wrong.
Config File - sqs-queue.json
sqs-queue.json
{
  "QueueName": "example-queue",
  "Attributes": {
    "DelaySeconds": "0",
    "MessageRetentionPeriod": "86400"
  }
}

This JSON file creates a simple AWS SQS queue named example-queue. DelaySeconds sets no delay before messages are visible. MessageRetentionPeriod keeps messages for 1 day (86400 seconds) before deleting.

Commands
This command creates a new message queue named example-queue with no delay and messages kept for 1 day.
Terminal
aws sqs create-queue --queue-name example-queue --attributes DelaySeconds=0,MessageRetentionPeriod=86400
Expected OutputExpected
{"QueueUrl":"https://sqs.us-east-1.amazonaws.com/123456789012/example-queue"}
--queue-name - Sets the name of the queue to create.
--attributes - Sets queue properties like delay and retention.
This command sends a message with the text 'Hello from my app!' to the example-queue.
Terminal
aws sqs send-message --queue-url https://sqs.us-east-1.amazonaws.com/123456789012/example-queue --message-body "Hello from my app!"
Expected OutputExpected
{"MD5OfMessageBody":"5d41402abc4b2a76b9719d911017c592","MessageId":"f1a2b3c4-5678-90ab-cdef-EXAMPLE11111"}
--queue-url - Specifies the exact queue to send the message to.
--message-body - The content of the message being sent.
This command retrieves one message from the example-queue so your app can read it.
Terminal
aws sqs receive-message --queue-url https://sqs.us-east-1.amazonaws.com/123456789012/example-queue --max-number-of-messages 1
Expected OutputExpected
{"Messages":[{"MessageId":"f1a2b3c4-5678-90ab-cdef-EXAMPLE11111","ReceiptHandle":"AQEBwJnKyrHigUMZj6rYigCgxlaS3SLy0a...","MD5OfBody":"5d41402abc4b2a76b9719d911017c592","Body":"Hello from my app!"}]}
--queue-url - Specifies which queue to get messages from.
--max-number-of-messages - Limits how many messages to receive at once.
This command deletes the message from the queue after it has been processed, so it won't be received again.
Terminal
aws sqs delete-message --queue-url https://sqs.us-east-1.amazonaws.com/123456789012/example-queue --receipt-handle AQEBwJnKyrHigUMZj6rYigCgxlaS3SLy0a...
Expected OutputExpected
No output (command runs silently)
--queue-url - Specifies the queue where the message lives.
--receipt-handle - Unique identifier for the message to delete.
Key Concept

If you remember nothing else from this pattern, remember: messages are sent to a queue to wait safely until the receiver is ready to get and delete them.

Common Mistakes
Not deleting messages after receiving them
Messages stay in the queue and get delivered again, causing duplicate processing.
Always delete messages using their receipt handle after processing.
Using the wrong queue URL when sending or receiving messages
Commands fail or messages go to the wrong place.
Copy the exact queue URL from the create-queue output and reuse it.
Trying to receive messages immediately after sending without delay
Sometimes messages take a moment to appear, causing empty receives.
Wait a few seconds or retry receive-message if no messages appear.
Summary
Create an SQS queue to hold messages safely.
Send messages to the queue with send-message command.
Receive messages from the queue with receive-message command.
Delete messages after processing to avoid duplicates.