Red*_*ket 3 amazon-web-services aws-lambda terraform aws-event-bridge
我正在尝试创建一个事件桥规则,该规则将每 30 分钟运行一次 Lambda 函数。我的代码基于我在 SO Use terraform to set up a lambda functiontriggered 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
}
Run Code Online (Sandbox Code Playgroud)
监控/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 {
...
}
}
Run Code Online (Sandbox Code Playgroud)
监控/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 = ...
}
Run Code Online (Sandbox Code Playgroud)
监控/cloudwatch_event_rule/variables.tf
...
variable "schedule_expression" {
type = string
default = "rate(30 minutes)"
}
...
Run Code Online (Sandbox Code Playgroud)
监控/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
}
Run Code Online (Sandbox Code Playgroud)
这最终会创建 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
}
Run Code Online (Sandbox Code Playgroud)
确保source_arn正确指向您的事件规则的 ARN。
| 归档时间: |
|
| 查看次数: |
2896 次 |
| 最近记录: |