SNS 到 SQS 调用 Lambda,数据结构被双字符串化

SNS to SQS invoking Lambda, data structures are double-stringified

我有以下管道:

Lambda #1 -> SNS -> SQS -> Lambda #2

Lambda #1 将批量发布一些消息到 SNS,SNS 将其传播到订阅,在本例中为 SQS 队列。

SQS 随后将使用来自 Lambda #1 的消息通过事件调用来调用 Lambda。

整个管道都可以工作,但是当有效负载最终到达 Lambda #2 时,它会被双重字符串化,所以如果我发送消息 {foo: bar},我会得到这样的响应:

{
  "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
    }
  ]
}

似乎 SNS 被字符串化,然后作为消息正文发送到 SQS,然后提供给 Lambda。

这是正常情况还是我配置不正确?

经过一番挖掘,发现您需要启用 RawMessageDeliveryhttps://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.