Webux Lab

By Studio Webux

Send a JSON message with an SNS

TG
Tommy Gingras Studio Webux 2021-07-28

Send an SNS message in JSON format using the CLI

Example:

aws sns publish \
  --topic-arn="$SNS_ARN" \
  --message "{\"data\":\"Some Data here\",\"date\":\"$(date)\"}" \
  --subject "Testing JSON format with SNS"

The lambda will receive the data and extract it like that :

module.exports = (event, context) => {
  const jsonData =
    (event &&
    event.Records[0].Sns &&
    event.Records[0].Sns.Message &&
    typeof event.Records[0].Sns.Message === "string"
      ? JSON.parse(event.Records[0].Sns.Message)
      : event.Records[0].Sns.Message) || {};
};

A payload example format received by a lambda from an SNS :

{
  "Records": [
    {
      "EventSource": "aws:sns",
      "EventVersion": "1.0",
      "EventSubscriptionArn": "arn:aws:sns:us-east-1:123456789012:lambda_topic:0b6941c3-f04d-4d3e-a66d-b1df00e1e381",
      "Sns": {
        "Type": "Notification",
        "MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
        "TopicArn": "arn:aws:sns:us-east-1:123456789012:lambda_topic",
        "Subject": "TestInvoke",
        "Message": "<message payload>",
        "Timestamp": "2015-04-02T07:36:57.451Z",
        "SignatureVersion": "1",
        "Signature": "r0Dc5YVHuAglGcmZ9Q7SpFb2PuRDFmJNprJlAEEk8CzSq9Btu8U7dxOu++uU",
        "SigningCertUrl": "http://sns.us-east-1.amazonaws.com/SimpleNotificationService-d6d679a1d18e95c2f9ffcf11f4f9e198.pem",
        "UnsubscribeUrl": "http://cloudcast.amazon.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:123456789012:example_topic:0b6941c3-f04d-4d3e-a66d-b1df00e1e381",
        "MessageAttributes": { "key": { "Type": "String", "Value": "value" } }
      }
    }
  ]
}

Source


Search