0
votes

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?

1
You can use variables in that inlined JSON.Matt Schuchard

1 Answers

1
votes

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
}