Home > Net >  Unable to trigger AWS Lambda from SNS
Unable to trigger AWS Lambda from SNS

Time:02-11

I am trying to create trigger for AWS lambda from SNS codestar-notifications Lambda with SNS as trigger.

While creating a trigger using Console it automatically adds subscription to the SNS topic. Lambda Subscription to SNS Topic. Also, this works in alternate direction i.e. if I create a subscription for SNS as the Lambda function by explicitly adding its arn, it automatically links a trigger to Lambda function.

But when using terraform to create a subscription as below:

resource "aws_sns_topic_subscription" "subscribe_lambda_to_first_topic" {
  topic_arn = module.first_topic.sns-topic-detail.arn
  protocol  = "lambda"
  endpoint  = module.lambda_function.lambda_function.arn
}

it doesn't create a trigger in AWS Lambda.

I tried creating a trigger using event source mapping in Terraform as below

resource "aws_lambda_event_source_mapping" "lambda_source" {
 event_source_arn  = module.first_topic.sns-topic-detail.arn
 function_name     = module.lambda_function.lambda_function.arn
 starting_position = "LATEST"
}

it throws me an error saying it is possible only for

Error: error creating Lambda Event Source Mapping (arn:aws:sns:us-west-2:619867110810:codestar-notifications-emc-sns-to-lambda): InvalidParameterValueException: Unrecognized event source, must be kinesis, dynamodb stream or sqs. Unsupported source arn : arn:aws:sns:us-west-2:619867110810:codestar-notifications-emc-sns-to-lambda { RespMetadata: { StatusCode: 400, RequestID: "83bf57cb-b50d-49a8-9547-72fac69778d1" }, Message_: "Unrecognized event source, must be kinesis, dynamodb stream or sqs. Unsupported source arn : arn:aws:sns:us-west-2:619867110810:codestar-notifications-emc-sns-to-lambda", Type: "User" }

with aws_lambda_event_source_mapping.lambda_source, on main.tf line 43, in resource "aws_lambda_event_source_mapping" "lambda_source": 43: resource "aws_lambda_event_source_mapping" "lambda_source" {

CodePudding user response:

aws_lambda_event_source_mapping is not for SNS, just like the error message says. Instead you use aws_sns_topic_subscription as you did.

However, you forgot about aws_lambda_permission which should be (generic form from the docs - you need to adjust to your own setup):

resource "aws_lambda_permission" "with_sns" {
  statement_id  = "AllowExecutionFromSNS"
  action        = "lambda:InvokeFunction"
  function_name = aws_lambda_function.func.function_name
  principal     = "sns.amazonaws.com"
  source_arn    = aws_sns_topic.default.arn
}
  • Related