Allows an SNS topic to invoke a Lambda function.

Terraform Template

resource "aws_iam_role" "default" {
  assume_role_policy = jsonencode({
 Version = "2012-10-17"
 Statement = [
 {
 Action = "sts:AssumeRole"
 Effect = "Allow"
 Sid = ""
 Principal = {
 Service = "lambda.amazonaws.com"
 }
 },
 ]
 })
  name = "iam_for_lambda_with_sns"
}

resource "aws_lambda_function" "func" {
}

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

resource "aws_sns_topic" "default" {
  name = "call-lambda-maybe"
}

resource "aws_sns_topic_subscription" "lambda" {
  endpoint = aws_lambda_function.func.arn
  protocol = "lambda"
  topic_arn = aws_sns_topic.default.arn
}