Bird
0
0

You wrote this code snippet to receive messages from an AWS SQS queue but it returns no messages:

medium📝 Debug Q14 of 15
AWS - SNS and SQS
You wrote this code snippet to receive messages from an AWS SQS queue but it returns no messages:
response = sqs.receive_message(QueueUrl=queue_url, MaxNumberOfMessages=5)
messages = response['Messages']
for msg in messages:
    print(msg['Body'])

What is the likely cause of the error?
AThe response has no 'Messages' key if the queue is empty
BMaxNumberOfMessages must be 1, not 5
CThe queue URL is incorrect or empty
DYou must delete messages before receiving
Step-by-Step Solution
Solution:
  1. Step 1: Understand SQS receive_message response

    If the queue is empty, the response does not include the 'Messages' key, causing a KeyError when accessing it.
  2. Step 2: Identify the error cause

    Not checking if 'Messages' exists before looping causes failure when no messages are returned.
  3. Final Answer:

    The response has no 'Messages' key if the queue is empty -> Option A
  4. Quick Check:

    Empty queue means no 'Messages' key [OK]
Quick Trick: Check if 'Messages' key exists before using it [OK]
Common Mistakes:
MISTAKES
  • Assuming 'Messages' always exists
  • Setting MaxNumberOfMessages incorrectly
  • Thinking messages must be deleted before receiving

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More AWS Quizzes