gpt4 book ai didi

amazon-web-services - 为什么我的 Event Bridge 规则不执行我的 Lambda 函数?

转载 作者:行者123 更新时间:2023-12-05 02:43:48 25 4
gpt4 key购买 nike

我正在尝试创建一个事件桥规则,该规则将每 30 分钟运行一次我的 Lambda 函数。我的代码基于我在 SO Use terraform to set up a lambda function triggered by a scheduled event source 上找到的这个答案

这是我的地形代码:

监控/main.tf:

...

module "cloudwatch_event_rule" {
source = "./cloudwatch_event_rule"
extra_tags = local.extra_tags
}

module "lambda_function" {
source = "./lambda_functions"
extra_tags = local.extra_tags
alb_names = var.alb_names
slack_webhook_url = var.slack_webhook_url
environment_tag = local.environment_tag
}

module "cloudwatch_event_target" {
source = "./cloudwatch_event_target"
lambda_function_arn = module.lambda_function.detect_bad_rejects_on_alb_lambda_arn
cloudwatch_event_rule_name = module.cloudwatch_event_rule.cloudwatch_event_rule_name
extra_tags = local.extra_tags
}

监控/lambda_functions/main.tf:

resource "aws_lambda_function" "detect_bad_rejects_on_alb" {
filename = var.filename
function_name = var.function_name
role = aws_iam_role.detect_bad_reject_on_alb.arn
handler = var.handler
source_code_hash = filebase64sha256(var.filename)
runtime = var.runtime
timeout = var.timeout
environment {
...
}

}

监控/cloudwatch_event_rule/main.tf

resource "aws_cloudwatch_event_rule" "event_rule" {
name = var.rule_name
description = var.description
schedule_expression = var.schedule_expression
tags = ...
}

监控/cloudwatch_event_rule/variables.tf

...

variable "schedule_expression" {
type = string
default = "rate(30 minutes)"
}

...

监控/cloudwatch_event_target/main.tf

resource "aws_cloudwatch_event_target" "event_target" {
arn = var.lambda_function_arn
rule = var.cloudwatch_event_rule_name
input = var.input
}

这最终创建了 lambda 函数和事件桥接规则,我的 lambda 函数作为其目标,调度表达式 “rate(30 minutes)” 但 lambda 函数从未执行过?我做错了什么?

最佳答案

从您发布的内容来看,您似乎没有为调用添加权限。您的代码未显示 aws_lambda_permission 的创建有适当的规则。所以您应该添加这样的权限,以便 EventBridge 可以调用您的函数(示例):

resource "aws_lambda_permission" "event-invoke" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = var.function_name
principal = "events.amazonaws.com"
source_arn = module.cloudwatch_event_rule.cloudwatch_event_rule_arn
}

确保 source_arn 正确指向事件规则的 ARN。

关于amazon-web-services - 为什么我的 Event Bridge 规则不执行我的 Lambda 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66755831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com