[英]aws terraform cloudwatch rule as lambda trigger
我正在嘗試配置cloudwatch規則,該規則將在特定的日期/時間使用以下命令觸發lambda函數:
resource "aws_lambda_function" "cleanup_daily" {
filename = "name"
function_name = "name"
role = "arn<removed>"
handler = "snapshotcleanup.lambda_handler"
source_code_hash = "${base64sha256(file("file_name"))}"
runtime = "python2.7"
timeout = "20"
description = "desc"
}
resource "aws_cloudwatch_event_rule" "daily_rule" {
name = "name"
description = "desc"
schedule_expression = "cron(....)"
}
resource "aws_cloudwatch_event_target" "daily_target" {
rule = "${aws_cloudwatch_event_rule.daily_rule.name}"
arn = "${aws_lambda_function.cleanup_daily.arn}"
}
但是,lambda函數無法運行。 如果我查看lambda並檢查“觸發器”選項卡,則那里什么也沒有。 如果我查看cloudwatch規則並在“目標”下查看,則會顯示lambda函數,如果單擊它,則會重定向到該函數本身。 有什么想法可能在這里出什么問題嗎?
對於其中一種cloudwatch規則,我單擊了edit-> save-> configure details-> update,而沒有進行任何更改,現在顯示在lambda的Trigger選項卡下,但仍然需要其他步驟才能進行此步驟,
每當不同的AWS服務進行交互時,有必要使用AWS IAM授予它們必要的訪問權限。
在這種情況下,Cloudwatch Events必須有權執行相關的Lambda函數。
AWS教程的步驟2描述了如何使用AWS CLI執行此操作。 aws lambda add-permission
命令的Terraform等效項是aws_lambda_permission
資源 ,該資源可與問題中的配置示例一起使用,如下所示:
data "aws_caller_identity" "current" {
# Retrieves information about the AWS account corresponding to the
# access key being used to run Terraform, which we need to populate
# the "source_account" on the permission resource.
}
resource "aws_lambda_permission" "allow_cloudwatch" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.cleanup_daily.function_name}"
principal = "events.amazonaws.com"
source_account = "${data.aws_caller_identity.current.account_id}"
source_arn = "${aws_cloudwatch_event-rule.daily_rule.arn}"
}
AWS Lambda權限是對IAM角色和策略的抽象。 有關IAM角色和策略的一些一般背景信息,請參閱我對另一個需要更多手動配置的問題的較長答案 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.