Rajkumar
Rajkumar

Reputation: 11

AWS lambda subscribed to SNS topic not working

I was creating a lambda function using node js and executing the simple function, in case of the function returns failure need to trigger an email.

  1. Lambda Function code
exports.handler = async function(event) {
const promise = new Promise(function(resolve, reject) {
  https.get(url, (res) => {
      resolve(res.statusCode)
    }).on('error', (e) => {
      reject(Error(e))
    })
  })
return promise
}  
  1. Create a topic and subscribed email notification
  2. I have configured Configuration -> Permission - Role Name configured

AWSLambdaSNSTopicDestinationExecutionRole JSON format

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "sns:Publish",
            "Resource": "arn:aws:sns:xxx-xxxxxx-x:12435693:XXXXXXX"
        }
    ]
}

AWSLambdaVPCAccessExecutionRole

  1. SNS TOPIC Policy
{
  "Version": "2008-10-17",
  "Id": "__default_policy_ID",
  "Statement": [
    {
      "Sid": "__default_statement_ID",
      "Effect": "Allow",
      "Principal": {
        "AWS": "*"
      },
      "Action": [
        "SNS:Publish",
        "SNS:RemovePermission",
        "SNS:SetTopicAttributes",
        "SNS:DeleteTopic",
        "SNS:ListSubscriptionsByTopic",
        "SNS:GetTopicAttributes",
        "SNS:AddPermission",
        "SNS:Subscribe"
      ],
      "Resource": "arn:aws:sns:xxxx-2:xxxxxx1:xxxxxx",
      "Condition": {
        "StringEquals": {
          "AWS:SourceOwner": "xxxxxxxx"
        }
      }
    },
    {
      "Sid": "__console_sub_0",
      "Effect": "Allow",
      "Principal": {
        "AWS": [
          "arn:aws:iam::xxxxxx1:root",
          "arn:aws:iam::xxxxxx2:root"
        ]
      },
      "Action": "SNS:Subscribe",
      "Resource": "arn:aws:sns:xxxx-2:xxxxxx1:xxxxxx_TP"
    }
  ]
}
  1. Lambda's Destination is subscribed to the topic

When I tied to execute the lambda function in case of failure, I am not getting any email. Even I tried to subscribe to the topic for success case as well I am not getting any email. Could anyone face this issue?

Upvotes: 0

Views: 662

Answers (1)

Marcin
Marcin

Reputation: 238051

Lambda's destination is only for asynchronous invocations of your functions. If you run the function from a console, or a normal synchronous way, destination are not triggered.

Upvotes: 2

Related Questions