Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 621
Gavin Yap Avatar asked Dec 21 '25 23:12

Gavin Yap


2 Answers

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
}
like image 112
akosky Avatar answered Dec 23 '25 22:12

akosky


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"
}
like image 38
Asri Badlah Avatar answered Dec 23 '25 22:12

Asri Badlah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!