Last Updated: January 06, 2019
·
397
· skyzyx

Status checks for EC2 instances which are part of an ECS cluster

This will lookup your AWS Elastic Container Service (ECS) cluster by name, discover which EC2 instances are part of that cluster, then check the status check results for those EC2 instances. Leverages the Unified AWS CLI Tools.

NOTE: Does NOT work with Fargate.

cluster_name="ecs-example-cluster"

aws ec2 describe-instance-status \
    --instance-ids $(
        aws ecs describe-container-instances \
            --cluster $cluster_name \
            --container-instances $(
                aws ecs list-container-instances \
                    --cluster $cluster_name \
                    | jq -r ".containerInstanceArns[]"
            ) \
            | jq -r ".containerInstances[].ec2InstanceId"
    ) \
    | jq -r '[.InstanceStatuses[] | {
        "id": .InstanceId,
        "state": .InstanceState.Name,
        "statusCheck1": .InstanceStatus.Status,
        "statusCheck2": .SystemStatus.Status
    }]' \
;

The output looks something like this:

[
  {
    "id": "i-08cc66e19e964b319",
    "state": "running",
    "statusCheck1": "ok",
    "statusCheck2": "ok"
  },
  {
    "id": "i-015bd3337c2c95177",
    "state": "running",
    "statusCheck1": "ok",
    "statusCheck2": "ok"
  },
  {
    "id": "i-0dfddc02842a64ded",
    "state": "running",
    "statusCheck1": "ok",
    "statusCheck2": "ok"
  }
]