0
votes

In my pipeline I have an event notification on an S3 bucket which triggers an SNS topic. That SNS topic in turn has a lambda function subscribed to it. I need the SNS topic to send a hard coded message body to the lambda because it get's used in that function. Since the SNS topic publishes the message automatically when the S3 event notification is set off I am wondering if and how I can edit the message that gets sent to lambda?

To be clear: I want the same message sent every time. The goal is for lambda to get a variable which is only dependent on which topic the lambda was triggered from.

Currently I am building this through the UI but will eventually code it in terraform for production.

1
I don't think it is possible, because the integration between S3 <> SNS is basically to send events from S3 to SNS, you can't change the message. So if you set an event on ObjectCreated, it will send: "An object was created" and the same for ObjectRemoved and etc. But you can easily ignore it in your lambda, the SNS will trigger the lambda but you don't need to read what's inside the message. - juanfontes
So while we can't change the message S3 sends to SNS, is there a way I can pre-define a message that gets sent from SNS to lambda every time the topic publishes? Or is it limited to "forwarding" the message from the S3 event notification? - timH

1 Answers

0
votes

When Amazon SNS triggers an AWS Lambda function, the information it sends includes SNS TopicArn.

You could use that ARN to determine which SNS Topic triggered the Lambda function, and therefore which action it should process.

{
  "Records": [
    {
      "EventSource": "aws:sns",
      "EventVersion": "1.0",
      "EventSubscriptionArn": "arn:aws:sns:us-east-1:{{{accountId}}}:ExampleTopic",
      "Sns": {
        "Type": "Notification",
        "MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
        "TopicArn": "arn:aws:sns:us-east-1:123456789012:ExampleTopic",
        "Subject": "example subject",
        "Message": "example message",
        "Timestamp": "1970-01-01T00:00:00.000Z",
        "SignatureVersion": "1",
        "Signature": "EXAMPLE",
        "SigningCertUrl": "EXAMPLE",
        "UnsubscribeUrl": "EXAMPLE",
        "MessageAttributes": {
          "Test": {
            "Type": "String",
            "Value": "TestString"
          },
          "TestBinary": {
            "Type": "Binary",
            "Value": "TestBinary"
          }
        }
      }
    }
  ]
}