创建Cloudwatch事件规则以通过Terraform更改任务状态

时间:2020-08-26 14:35:21

标签: amazon-web-services terraform amazon-ecs amazon-cloudwatch

我想根据我的ECS集群通过Terraform更改任务状态来设置Cloudwatch事件规则。每当任务进入STOPPED或RUNNING状态时,我都会收到通知。

无论任何一个事件发生,此规则都将起作用并匹配吗?然后,我将相应地创建资源“ aws_cloudwatch_event_target”:

resource "aws_cloudwatch_event_rule" "ecs-sns-rule" {
  name = "ecs task state change"
  
  event_pattern = <<PATTERN
{
  "source": [
    "aws.ecs"
    ],
  "detail-type": [
    "ECS Task State Change"
    ],
  "detail": {

    "lastStatus": [
      "STOPPED",
      "RUNNING"
    ],
   "clusterArn": "arn:aws:ecs:us-west-2:XXXXXXXXX:Cluster/XXXXXXXX"
}
}
PATTERN
}

1 个答案:

答案 0 :(得分:1)

来自docs

匹配值总是数组

因此,我认为您可以尝试以下操作(假设其他条件都很好):

resource "aws_cloudwatch_event_rule" "ecs-sns-rule" {
  name = "ecs task state change"
  
  event_pattern = <<PATTERN
{
  "source": [
    "aws.ecs"
    ],
  "detail-type": [
    "ECS Task State Change"
    ],
  "detail": {

    "lastStatus": [
      "STOPPED",
      "RUNNING"
    ],
   "clusterArn": ["arn:aws:ecs:us-west-2:XXXXXXXXX:Cluster/XXXXXXXX"]
}
}
PATTERN
}