20

This is the Cloudformation template code related to my problem:

"SNSTopic": {
  "Type": "AWS::SNS::Topic",
  "Properties": {
    "TopicName": "JumpboxPresenceTopic",
    "DisplayName": "Jumpbox Presence Topic",
    "Subscription": [
      {
        "Endpoint": {
          "Fn::GetAtt": [
            "Lambda",
            "Arn"
          ]
        },
        "Protocol": "lambda"
      }
    ]
  }
},
"Lambda": {
  "Type": "AWS::Lambda::Function",
  "Properties": [...]

I can see the topic in the SNS dashboard: enter image description here

But it does not display in the lambda function Event Sources panel: enter image description here

The weird thing about this, is that if I create a new subscription from the SNS dashboard for that same lambda function, no new subscription is created since it would be an exact duplicate. However, now if I check the Event Sources panel in the Lambda dashboard, I can see a new entry for the SNS: JumpboxPresenceTopic: enter image description here

I feel like it's an issue on Amazon's side but I could be wrong. Is there something wrong with my approach or is it a limitation of AWS ?

Laurent Jalbert Simard
  • 5,949
  • 1
  • 28
  • 36

2 Answers2

14

You must grant SNS permission to invoke Lambda first. Here is a example from AWS. Please change it from S3 to SNS and don't forget to set SourceArn as the SNS Topic ARN.

http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-permission.html

Xing-Wei Lin
  • 329
  • 3
  • 5
  • 1
    I'm experiencing a similar issue, and while this answer does help get the source permissions setup in lambda, the sns notification is not invoking it, any other ideas why? – Onema Jan 30 '16 at 05:38
  • 5
    @Onema I had a similar issue, AWS support told me to remove the "SourceAccount" and add the "SourceArn" pointed to the SNS topic in the Permission resource, hope that helps! – deviavir Feb 11 '16 at 07:54
  • @deviavir, Yes! that resolved my problem as well. I ended up using only the "SourceArn". – Onema Feb 11 '16 at 07:59
  • 2
    @deviavir, worked for me as well. Its critical that "SourceAccount" is not present (I had both SourceAccount and SourceArn in my script, and it wasn't working until I removed SourceAccount). – Geoff Jun 19 '16 at 03:08
  • Adding SourceArn didn't work for me. The AWS Lambda Console now displays the SNS topic in the Triggers tab, but only the SNS topics I manually set up in the console. Any SNS topic I create via CloudFormation doesn't show up in the Triggers tab, and the function still isn't being invoked when a message is published to the SNS topic. – Alex Glover Aug 01 '16 at 21:14
  • Note that if you want to map a SNS topic to an _alias_, you need to reference that alias in the subscription _and_ and Lambda permission. So in `'AWS::Lambda::Permission'`: `Properties.FunctionName = { Ref: 'MyLambdaAlias' }`. And in `AWS::SNS::Topic`: `Properties.Subscriptions[].Endpoint = { Ref: 'MyLambdaAlias' }` – edan Jan 24 '17 at 19:49
  • Does this mean setting up the permission for SNS to invoke the Lambda function is enough to assign it as the event source? We don't need to create anything `AWS::Lambda::EventSourceMapping` resource? – Anugerah Erlaut Jan 20 '23 at 09:57
3

Adding the proper function name and sourcearn in permissions helped solving the issue

"MySNSTopic": {
            "Type": "AWS::SNS::Topic",
            "Properties": {
                "TopicName": "MyTopic",
                "DisplayName": "My Test Topic",
                "Subscription": [
                {
                    "Endpoint": { "Fn::GetAtt" : ["Lambda", "Arn"] },
                    "Protocol": "lambda"
                }
                ]
            }
    },
    "PermissionForEventsToInvokeLambda": {
          "Type": "AWS::Lambda::Permission",
          "Properties": {
            "FunctionName": { "Fn::GetAtt" : ["Lambda", "Arn"] },
            "Action": "lambda:InvokeFunction",
            "Principal": "sns.amazonaws.com",
            "SourceArn": { "Ref": "MySNSTopic" }
          }
      }
   },
suganya123
  • 31
  • 2