Search code examples
arraysamazon-web-servicesamazon-ecsamazon-cloudwatch

ECS task change


I am trying to check the last status of an ECS task, if it is STOPPED then it has to trigger lambda using cloudwatch event. I am getting below error message while validating the JSON event. Error message: Event pattern contains invalid element (can only be Strings enclosed in quotes, numbers, and the unquoted keywords true, false, and null)

    **Code**:
    {
  "source": [
    "aws.ecs"
  ],
  "detail-type": [
    "ECS Task State Change"
  ],
  "detail": {
    "clusterArn": [
      "arn:aws:ecs:us-west-2:17025:cluster/ecs-w2"
    ],
    "lastStatus": [
      "STOPPED"
    ],
    "overrides": {
      "containerOverrides":
        {
          "environment": 
            {
              "name": ["job_grp_nm"],
              "value": ["QA_INTEGRATION"]
            },
            {
              "name": ["secrets_targetdb"],
              "value": ["7034908/Snowflake/facilitiesNDI/QA"]
            },
        "name": ["suite-dev"]
        }
    }
  }
}
    

Please let me know what's wrong with the code.


Solution

  • There are two things wrong:

    1. The values should be expressed as array of string, e.g. ["value"]
    2. Your environment can be an array of objects.

    So it should be:

    {
      "source": [
        "aws.ecs"
      ],
      "detail-type": [
        "ECS Task State Change"
      ],
      "detail": {
        "clusterArn": [
          "arn:aws:ecs:us-west-2:17025:cluster/ecs-w2"
        ],
        "lastStatus": [
          "STOPPED"
        ],
        "overrides": {
          "containerOverrides":
            {
              "environment": 
                {
                  "name": ["job_grp_nm"],
                  "value": ["QA_INTEGRATION"]
                },
            "name": ["suite-dev"]
            }
        }
      }
    }
    

    The above should be accepted, but I can't verify if this will correctly match your event.