使用 Terraform for ECS 在 CloudWatch 中包含退出代码 1 事件
Including an exit code 1 event in CloudWatch using Terraform for ECS
我已经 运行 ECS 上的容器,并使用 AWS Cloudwatch 事件在我的任务完成时通知我。所有基础设施都是使用 Terraform 创建的。但是,我无法在我的事件模式中获得正确的语法,因此我只会收到非零退出代码的通知。
以下资源效果很好,每次我的容器之一退出时都会向 SNS 发送通知:
resource "aws_cloudwatch_event_rule" "container-stopped-rule" {
name = "container-stopped"
description = "Notification for containers that exit for any reason. (error)."
event_pattern = <<PATTERN
{
"source": [
"aws.ecs"
],
"detail-type": [
"ECS Task State Change"
],
"detail": {
"lastStatus": [
"STOPPED"
],
"stoppedReason" : [
"Essential container in task exited"
]
}
}
PATTERN
}
但是,我正在尝试稍微修改模式,以便仅在容器退出并显示错误代码时才收到通知 - 由于我们收到了如此多的通知,我们已经开始调出电子邮件,有时不要注意容器因错误而退出的电子邮件通知:
resource "aws_cloudwatch_event_rule" "container-stopped-rule" {
name = "container-stopped"
description = "Notification for containers with exit code of 1 (error)."
event_pattern = <<PATTERN
{
"source": [
"aws.ecs"
],
"detail-type": [
"ECS Task State Change"
],
"detail": {
"containers": [
{
"exitCode": 1
}
],
"lastStatus": [
"STOPPED"
],
"stoppedReason" : [
"Essential container in task exited"
]
}
}
PATTERN
}
当我 terraform apply
:
时会触发以下错误
aws_cloudwatch_event_rule.container-stopped-rule: Updating CloudWatch
Event Rule failed: InvalidEventPatternException: Event pattern is not
valid. Reason: Match value must be String, number, true, false, or
null at [Source:
(String)"{"detail":{"containers":[{"exitCode":1}],"lastStatus":["STOPPED"],"stoppedReason":["Essential
container in task exited"]},"detail-type":["ECS Task State
Change"],"source":["aws.ecs"]}"; line: 1, column: 27] status code:
400
这让我感到困惑,因为我正在遵循 AWS CloudWatch documentation for containers 中列出的确切结构。我什至尝试在 1
周围加上双引号,以防 Terraform 需要字符串而不是数字。
我也尝试使用 AWS 控制台手动编辑事件模式 JSON,但收到此错误:
Validation error. Details: Event pattern contains invalid value (can
only be a nonempty array or nonempty object)
老实说,此时我有点难过,如果能就我的语法错误提供任何提示,我将不胜感激。
根据https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/CloudWatchEventsandEventPatterns.html,
For a pattern to match an event, the event must contain all the field names listed in the pattern. The field names must appear in the event with the same nesting structure.
您可以尝试在此处添加更多字段列表器 https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs_cwe_events.html,例如 clusterArn
、containerInstanceArn
等吗?
事件模式语法很奇怪,我运行遇到了同样的问题。以下将起作用:
{
"source": [
"aws.ecs"
],
"detail-type": [
"ECS Task State Change"
],
"detail": {
"lastStatus": [
"STOPPED"
],
"stoppedReason": [
"Essential container in task exited"
],
"containers": {
"exitCode": [
1
]
}
}
}
我在 Input T运行sformer 中使用 $.detail.group
来获取通知消息中的任务系列名称。
我已经 运行 ECS 上的容器,并使用 AWS Cloudwatch 事件在我的任务完成时通知我。所有基础设施都是使用 Terraform 创建的。但是,我无法在我的事件模式中获得正确的语法,因此我只会收到非零退出代码的通知。
以下资源效果很好,每次我的容器之一退出时都会向 SNS 发送通知:
resource "aws_cloudwatch_event_rule" "container-stopped-rule" {
name = "container-stopped"
description = "Notification for containers that exit for any reason. (error)."
event_pattern = <<PATTERN
{
"source": [
"aws.ecs"
],
"detail-type": [
"ECS Task State Change"
],
"detail": {
"lastStatus": [
"STOPPED"
],
"stoppedReason" : [
"Essential container in task exited"
]
}
}
PATTERN
}
但是,我正在尝试稍微修改模式,以便仅在容器退出并显示错误代码时才收到通知 - 由于我们收到了如此多的通知,我们已经开始调出电子邮件,有时不要注意容器因错误而退出的电子邮件通知:
resource "aws_cloudwatch_event_rule" "container-stopped-rule" {
name = "container-stopped"
description = "Notification for containers with exit code of 1 (error)."
event_pattern = <<PATTERN
{
"source": [
"aws.ecs"
],
"detail-type": [
"ECS Task State Change"
],
"detail": {
"containers": [
{
"exitCode": 1
}
],
"lastStatus": [
"STOPPED"
],
"stoppedReason" : [
"Essential container in task exited"
]
}
}
PATTERN
}
当我 terraform apply
:
aws_cloudwatch_event_rule.container-stopped-rule: Updating CloudWatch Event Rule failed: InvalidEventPatternException: Event pattern is not valid. Reason: Match value must be String, number, true, false, or null at [Source: (String)"{"detail":{"containers":[{"exitCode":1}],"lastStatus":["STOPPED"],"stoppedReason":["Essential container in task exited"]},"detail-type":["ECS Task State Change"],"source":["aws.ecs"]}"; line: 1, column: 27] status code: 400
这让我感到困惑,因为我正在遵循 AWS CloudWatch documentation for containers 中列出的确切结构。我什至尝试在 1
周围加上双引号,以防 Terraform 需要字符串而不是数字。
我也尝试使用 AWS 控制台手动编辑事件模式 JSON,但收到此错误:
Validation error. Details: Event pattern contains invalid value (can only be a nonempty array or nonempty object)
老实说,此时我有点难过,如果能就我的语法错误提供任何提示,我将不胜感激。
根据https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/CloudWatchEventsandEventPatterns.html,
For a pattern to match an event, the event must contain all the field names listed in the pattern. The field names must appear in the event with the same nesting structure.
您可以尝试在此处添加更多字段列表器 https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs_cwe_events.html,例如 clusterArn
、containerInstanceArn
等吗?
事件模式语法很奇怪,我运行遇到了同样的问题。以下将起作用:
{
"source": [
"aws.ecs"
],
"detail-type": [
"ECS Task State Change"
],
"detail": {
"lastStatus": [
"STOPPED"
],
"stoppedReason": [
"Essential container in task exited"
],
"containers": {
"exitCode": [
1
]
}
}
}
我在 Input T运行sformer 中使用 $.detail.group
来获取通知消息中的任务系列名称。