0
votes

I want to setup Cloudwatch events rule based on the task state changes of my ECS cluster via terraform. I would like to be notified whenever the task goes to STOPPED or RUNNING state.

Will this rule work and match whenever either of the events happen? I will then create resource "aws_cloudwatch_event_target" accordingly:

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
So what is the issue? Any errors?Marcin
@Marcin When I try to use the above mentioned config, the terraform apply fails stating Event pattern is not valid. Reason: "clusterArn" must be an object or an arrayMayur N

1 Answers

2
votes

From the docs:

Match values are always in arrays.

Therefore, I think you could try the following (assuming everything else is fine):

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
}