flowermia
flowermia

Reputation: 409

SQS sending empty MessageBody

I'm trying to send a message via a Lambda to SQS, however the MessageBody is never sent - any ideas?

Sender:

sqs = boto3.client('sqs')
queue_url = '<queue url>'
# send message to sqs
response = sqs.send_message(
    QueueUrl = queue_url,
    DelaySeconds=10,
    MessageAttributes={
        'Author': {
            'DataType':'String',
            'StringValue':'Mia'
        }
    },
    MessageBody=(
        "127.0.0.1"
    )
)

Receiver:

# recieve message from sqs
sqs = boto3.client('sqs')
queue_url = '<queue url>'
response = sqs.receive_message(
    QueueUrl=queue_url,
    AttributeNames=[
        'SentTimestamp'
    ],
    MaxNumberOfMessages=1,
    MessageAttributeNames=[
        'All'
    ],
    VisibilityTimeout=0,
    WaitTimeSeconds=0
) 
if 'Messages' in response:
    message = response['Messages'][0]
    receipt_handle = message['ReceiptHandle']
    # delete message from queue
    sqs.delete_message(
        QueueUrl=queue_url,
        ReceiptHandle=receipt_handle
    )
    return message

When I print the response variable from the sender I get:

{'MD5OfMessageBody': '4cb7e68d651d2dcac370949dd9d47c6e', 'MD5OfMessageAttributes': 'd260c4584b674fcbf00879268dd2a419', 'MessageId': '4a7d636b-4955-424b-8c62-b70c03bd7983', 'ResponseMetadata': {'RequestId': 'a902c587-9a6a-529d-a016-9fdfa8adc719', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': 'a902c587-9a6a-529d-a016-9fdfa8adc719', 'date': 'Wed, 12 Jan 2022 01:36:08 GMT', 'content-type': 'text/xml', 'content-length': '459'}, 'RetryAttempts': 0}}

and the reciever:

{'ResponseMetadata': {'RequestId': '718d7c1e-8cc4-5e83-9533-2646bbfa63d5', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': '718d7c1e-8cc4-5e83-9533-2646bbfa63d5', 'date': 'Wed, 12 Jan 2022 01:55:10 GMT', 'content-type': 'text/xml', 'content-length': '240'}, 'RetryAttempts': 0}}

Why is there no Messages attribute in the received response?

Upvotes: 1

Views: 2091

Answers (1)

Samer A.
Samer A.

Reputation: 63

I had a similar problem. I would post one message to sqs, and I expected my Lambda to trigger, which would consume that one message by calling receive_message. But the message that the Lambda consumed off sqs was always empty (no Messages or Body).

I realized that the sqs message was automagically taken off the queue and passed into my Lambda via the event parameter.

def lambda_handler(event, context):

So there was no need to call receive_message in my Lambda. Again, since AWS already took the message off the queue, calling receive_message would come up empty because the queue was empty.

I bet if you print the contents of event, you'll see the message you expect.

Upvotes: 2

Related Questions