I have the following pipeline:
Lambda #1 -> SNS -> SQS -> Lambda #2
Lambda #1 will publish some messages in batch to SNS, which will propagate that to subscriptions, in this case, an SQS queue.
SQS will then invoke Lambda via event invocations with the message from Lambda #1.
This entire pipelines works, but when the payload finally gets to Lambda #2, it's double stringified, so if I send the message {foo: bar}
, I'll get a response like this:
{
"Records": [
{
...
"body": "{\n \"Type\" : \"Notification\",\n \"MessageId\" : \"some id\",\n \"TopicArn\" : \"arn:aws:sns:us-west-2:xxx:topicName\",\n \"Message\" : \"{\\\"foo\\\": \\\"bar\\\"}\",\n
... rest of SNS payload}",
... rest of SQS payload
}
]
}
It seems SNS gets stringified and then sent to SQS as the body of a message, then given to Lambda.
Is this to be expected or did I configure incorrectly?
After a bit of digging, turns out you need to enable RawMessageDelivery
: https://docs.aws.amazon.com/sns/latest/dg/sns-large-payload-raw-message-delivery.html
To avoid having Amazon Kinesis Data Firehose, Amazon SQS, and HTTP/S endpoints process the JSON formatting of messages, Amazon SNS allows raw message delivery:
- When you enable raw message delivery for Amazon Kinesis Data Firehose or Amazon SQS endpoints, any Amazon SNS metadata is stripped from the published message and the message is sent as is.
- When you enable raw message delivery for HTTP/S endpoints, the HTTP header x-amz-sns-rawdelivery with its value set to true is added to the message, indicating that the message has been published without JSON formatting.
- When you enable raw message delivery for HTTP/S endpoints, the message body, client IP, and the required headers are delivered. When you specify message attributes, it won't be sent.
- When you enable raw message delivery for Kinesis Data Firehose endpoints, the message body is delivered. When you specify message attributes, it won't be sent.