Chris Alexander
Chris Alexander

Reputation: 1

Trying to define messageId for SQS messages to be placed in S3

I am trying to use the following lambda code to place a sqs message in a S3 bucket. The code will do that however I need to figure out how to define the messageId so that each object placed in the S3 bucket has the same name as the sqs messageid.

import json
import boto3

def lambda_handler(event, context):
    s3 = boto3.client("s3")
    data = json.loads(event["Records"][0]["body"])
    s3.put_object(Bucket="dlqbucket", Key="messageId", Body=json.dumps(data))
    
    # TODO implement
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from lambda')
    }
        

Upvotes: 0

Views: 896

Answers (1)

John Rotenstein
John Rotenstein

Reputation: 269071

Here is a sample incoming record from Amazon SQS to an AWS Lambda function:

{
  "Records": [
    {
      "messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb78",
      "receiptHandle": "MessageReceiptHandle",
      "body": "Hello from SQS!",
      "attributes": {
        "ApproximateReceiveCount": "1",
        "SentTimestamp": "1523232000000",
        "SenderId": "123456789012",
        "ApproximateFirstReceiveTimestamp": "1523232000001"
      },
      "messageAttributes": {},
      "md5OfBody": "{{{md5_of_body}}}",
      "eventSource": "aws:sqs",
      "eventSourceARN": "arn:aws:sqs:ap-southeast-2:123456789012:MyQueue",
      "awsRegion": "ap-southeast-2"
    }
  ]
}

Therefore, you can obtain the messageId with:

event["Records"][0]["messageId"]

ReceiveMessage Receive Elements @ docs.aws.amazon.com

Upvotes: 2

Related Questions