简体   繁体   中英

Inject ECS Task ARN into AWS Cloudwatch Event Rule using Terraform

I am trying to create an AWS Cloudwatch event rule to send SNS email notifications when a container running a particular task finishes running successfully. I have scoured through the Terraform documentation, but can't find a good way to dynamically inject in the 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
}

For example, the documentation examples here provide the same hardcoded <<PATTERN example I am using above.

Is there a template file way to dynamically insert in my ARNs without having them be hardcoded in?

As @matt Schuchard said, did you manage it with terraform resource attributes? (By the way, it is not called 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM