16

Can anyone reference or show me an example on how to create a AWS Lambda trigger with Terraform?

In the AWS console, after clicking a function name and selecting the configuration tab, you can create triggers E.g. a SNS trigger

1
  • Its trigger specific. For sns you create aws_sns_topic_subscription. Commented Mar 29, 2021 at 11:22

2 Answers 2

28

For an SNS trigger it is also necessary to add a resource-based policy for the lambda to allow it to be executed by the SNS subscription.

When creating the trigger from the AWS Console this is done automatically. When using Terraform this requires adding an aws_lambda_permission:

resource "aws_sns_topic_subscription" "my_sns_subscription" {
  topic_arn = aws_sns_topic.my_sns_topic.arn
  protocol  = "lambda"
  endpoint  = aws_lambda_function.my_lambda_function.arn
}

resource "aws_lambda_permission" "with_sns" {
  statement_id  = "AllowExecutionFromSNS"
  action        = "lambda:InvokeFunction"
  function_name = aws_lambda_function.my_lambda_function.function_name
  principal     = "sns.amazonaws.com"
  source_arn    = aws_sns_topic.my_sns_topic.arn
}
Sign up to request clarification or add additional context in comments.

1 Comment

it should be most voted
16

For sns you need to create sns subscription

resource "aws_sns_topic_subscription" "user_updates_lampda_target" {
  topic_arn = “sns topic arn”
  protocol  = "lambda"
  endpoint  = “lambda arn here”
}

To allows Lambda functions to get events from Kinesis, DynamoDB and SQS you can use event source mapping

resource "aws_lambda_event_source_mapping" "example" {
  event_source_arn  = aws_dynamodb_table.example.stream_arn
  function_name     = aws_lambda_function.example.arn
  starting_position = "LATEST"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.