简体   繁体   中英

Create Cloudwatch event rule for task state change via terraform

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
}

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
}

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