Gavin Yap
Gavin Yap

Reputation: 762

how to create aws lambda trigger in terraform?

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

Upvotes: 16

Views: 13332

Answers (2)

akosky
akosky

Reputation: 1184

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
}

Upvotes: 24

Asri Badlah
Asri Badlah

Reputation: 2123

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"
}

Upvotes: 16

Related Questions