Home > other >  What is the proper parsing for SQS receive_message?
What is the proper parsing for SQS receive_message?

Time:12-22

I've been troubleshooting this for a few days now and I can't seem to understand where to address this error. I've tested against the receive_message response syntax based on what is documented in Boto3, SQS documentation and Lambda tests to no avail.

{
      "errorMessage": "'Messages'",
      "errorType": "KeyError",
      "stackTrace": [
        "  File \"/var/task/lambda_function.py\", line 24, in lambda_handler\n    message = response['Messages'][0]\n"
      ]
 }

I took this Python snippet directly from the Boto3 SDK documentation:

import boto3
import os

# Create SQS client
sqs = boto3.client('sqs')

QUEUE_URL = os.environ['QUEUE_URL']

def lambda_handler(event, context):
    # Receive message from SQS queue
    response = sqs.receive_message(
        QueueUrl=QUEUE_URL,
        AttributeNames=[
            'SentTimestamp'
        ],
        MaxNumberOfMessages=10,
        MessageAttributeNames=[
            'All'
        ],
        VisibilityTimeout=0,
        WaitTimeSeconds=0
    )
    
    message = response['Messages'][0]
    receipt_handle = message['ReceiptHandle']
    
    # Delete received message from queue
    sqs.delete_message(
        QueueUrl=queue_url,
        ReceiptHandle=receipt_handle
    )
    print('Received and deleted message: %s' % message)

CodePudding user response:

If you have configured Amazon SQS to trigger the AWS Lambda function, then you do not need to call any SQS commands. Instead, the message(s) are provided in the event that is passed to the Lambda function:

for record in event['Records']:
    payload = record['body']
    print(payload)

If the Lambda function successfully exits, then the message(s) will be automatically deleted from the queue.

See: Using Lambda with Amazon SQS - AWS Lambda

  • Related