使用Terraform将ECS任务ARN注入AWS Cloudwatch事件规则

时间:2019-04-05 17:36:36

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

我正在尝试创建一个AWS Cloudwatch事件规则,以在运行特定任务的容器成功完成运行时发送SNS电子邮件通知。我遍历了Terraform文档,但找不到一种动态注入taskArn的好方法:

resource "aws_cloudwatch_event_rule" "important-task-complete-rule" {
  name = "reporting-task-completed"
  description = "Notification for when an important task finishes running successfully."

  event_pattern = <<PATTERN
{
  "source": [
    "aws.ecs"
    ],
  "detail-type": [
    "ECS Task State Change"
    ],
  "detail": {

    "lastStatus": [
      "STOPPED"
    ],
"stoppedReason" : [
    "Essential container in task exited"
  ],
"containers": {
  "exitCode": [
        0
      ],
  "taskArn": "arn:aws:ecs:us-east-1:MY_ACCOUNT:task/MY_TASK_ID_THAT_I_WANT_TO_INJECT"
    }
}
}
PATTERN
}

例如,此处的documentation examples提供了我上面使用的相同的硬编码<<PATTERN示例。

是否存在一种模板文件方式可以动态插入我的ARN中而无需对其进行硬编码?

1 个答案:

答案 0 :(得分:1)

正如@matt Schuchard所说,您是否使用terraform resource attributes?来管理它(顺便说一句,它不叫做variables)。

resource "aws_ecs_task_definition" "service" {
   ...
}

resource "aws_cloudwatch_event_rule" "important-task-complete-rule" {
  name = "reporting-task-completed"
  description = "Notification for when an important task finishes running successfully."

  event_pattern = <<PATTERN
{
  "source": [
    "aws.ecs"
    ],
  "detail-type": [
    "ECS Task State Change"
    ],
  "detail": {

    "lastStatus": [
      "STOPPED"
    ],
"stoppedReason" : [
    "Essential container in task exited"
  ],
"containers": {
  "exitCode": [
        0
      ],
  "taskArn": "${aws_ecs_task_definition.service.arn}"
    }
}
}
PATTERN
}