使用 terraform 设置由预定事件源触发的 lambda 函数 [英] Use terraform to set up a lambda function triggered by a scheduled event source
问题描述
我想每五分钟运行一次 AWS lambda 函数.在 AWS 管理控制台中,这很容易设置,在 lambda 函数的事件源"选项卡下,但如何使用 Terraform 进行设置一个>?
I would like to run an AWS lambda function every five minutes. In the AWS Management Console this is easy to set up, under the lambda function's "Event Sources" tab, but how do I set it up with Terraform?
我尝试使用一个aws_lambda_event_source_mapping
资源,但结果是 它使用的 API支持来自 Kinesis 和 DynamoDB 的事件.当我尝试将它与预定的事件源一起使用时,创建超时.
I tried to use an aws_lambda_event_source_mapping
resource, but it turns out that the API it uses only supports events from Kinesis and DynamoDB. When I try to use it with a scheduled event source, creation times out.
推荐答案
您可以使用 aws_cloudwatch_event_target
资源将计划的事件源(事件规则)与您的 lambda 函数联系起来.您需要授予它调用 lambda 函数的权限;您可以为此使用 aws_lambda_permission
资源.
You can use an aws_cloudwatch_event_target
resource to tie the scheduled event source (event rule) to your lambda function. You need to grant it permission to invoke your lambda function; you can use an aws_lambda_permission
resource for this.
示例:
resource "aws_lambda_function" "check_foo" {
filename = "check_foo.zip"
function_name = "checkFoo"
role = "arn:aws:iam::424242:role/something"
handler = "index.handler"
}
resource "aws_cloudwatch_event_rule" "every_five_minutes" {
name = "every-five-minutes"
description = "Fires every five minutes"
schedule_expression = "rate(5 minutes)"
}
resource "aws_cloudwatch_event_target" "check_foo_every_five_minutes" {
rule = "${aws_cloudwatch_event_rule.every_five_minutes.name}"
target_id = "check_foo"
arn = "${aws_lambda_function.check_foo.arn}"
}
resource "aws_lambda_permission" "allow_cloudwatch_to_call_check_foo" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.check_foo.function_name}"
principal = "events.amazonaws.com"
source_arn = "${aws_cloudwatch_event_rule.every_five_minutes.arn}"
}
这篇关于使用 terraform 设置由预定事件源触发的 lambda 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!