例如,我们在 cloudwatch 中有一个用于事件规则的资源标签
aws_cloudwatch_event_rule
问问题
2369 次
1 回答
2
您将需要使用aws_cloudwatch_event_rule的组合来匹配您想要使用 EventBridge 处理的事件,并使用 aws_cloudwatch_event_target转发到另一个 EventBridge 事件总线或可以直接处理事件的 Lambda 之类的东西,或者转发到可以处理事件的 SQS 队列由消费者处理。
为此目的,这是一个完全通用的模块:
主文件:
# ------------------------------------------------------------------------------
# CREATE CLOUDWATCH RULES FOR EACH LOGICAL ROUTE TO MATCH EVENTS OF INTEREST
# ------------------------------------------------------------------------------
resource "aws_cloudwatch_event_rule" "captures" {
for_each = var.event_routes
name = replace(replace(each.key, "[^\\.\\-_A-Za-z0-9]+", "-"), "_", "-")
description = each.value.description
event_pattern = jsonencode({
"detail-type" = each.value.event_names
})
}
# ------------------------------------------------------------------------------
# CONFIGURE EACH RULE TO FORWARD MATCHING EVENTS TO THE CORRESPONDING TARGET ARN
# ------------------------------------------------------------------------------
resource "aws_cloudwatch_event_target" "route" {
for_each = var.event_routes
target_id = each.key
rule = aws_cloudwatch_event_rule.captures[each.key].name
arn = each.value.target_arn
}
变量.tf:
variable "event_routes" {
description = "A map from a meaningful operator shorthand to the target ARN and list of the event names that CloudWatch should forward to them."
type = map(object({
description = string
event_names = list(string)
target_arn = string
}))
/*
event_routes = {
forward_to_kpi_tracker = {
description = "Forward events to KPI tracker"
event_names = [
"UserSignedUp",
"UserWatchedLessonVideo",
]
target_arn = "arn:aws:events:ca-central-1:000000000000:event-bus/default"
}
}
*/
}
输出.tf:
output "event_rule_name" {
value = { for route_shorthand, route_details in var.event_routes :
route_shorthand => aws_cloudwatch_event_rule.captures[route_shorthand].name
}
}
output "event_rule_arn" {
value = { for route_shorthand, route_details in var.event_routes :
route_shorthand => aws_cloudwatch_event_rule.captures[route_shorthand].arn
}
}
目标可以是以下任何一种:
- EC2 实例
- SSM 运行命令
- SSM 自动化
- AWS Lambda 函数
- Amazon Kinesis Data Streams 中的数据流
- Amazon Kinesis Data Firehose 中的数据传输流
- 亚马逊 ECS 任务
- AWS Step Functions 状态机
- AWS 批处理作业
- AWS CodeBuild 项目
- AWS CodePipeline 中的管道
- Amazon Inspector 评估模板
- Amazon SNS 主题
- Amazon SQS 队列,包括 FIFO 队列
- 另一个 AWS 账户的默认事件总线
于 2020-06-20T16:17:28.740 回答