Webux Lab

Par Studio Webux

Envoyer un message JSON avec une SNS

TG
Tommy Gingras Studio Webux S.E.N.C 2021-07-28

Envoyer un message SNS avec le format JSON avec le CLI

Voici un exemple:

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

Puis la lambda qui reçoit les données:

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) || {};
};

Voici le payload reçu par une lambda à partir d’une 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


Recherche