user2684206
user2684206

Reputation: 45

Check instance status using filter with EC2 API

I want to make sure the instance has passed the two status checks(System/Instance reachability check) using command line.

When I run this

ec2-describe-instance-status
ec2-describe-instance-status XX($InstanceID)

it would show running instances like

INSTANCE    $InstanceID $REGION running 16

But when I tried adding a filter to make sure the instance passed the status check

ec2-describe-instance-status XX($InstanceID) --filter instance-status.reachability=passed
ec2-describe-instance-status XX($InstanceID) --filter "instance-status.reachability=passed"
ec2-describe-instance-status --filter instance-status.reachability=passed

nothing ever returned.

I've double-checked the instances are running fine and actually passed the 2 status checks, but why nothing is returned after applying the filters?

Update: In response to Rico, I tried the -v option

ec2-describe-instance-status -v

returns one item in the instanceStatusSet, with the fields

    <item>
      <instanceId>i-XXX</instanceId>
      <availabilityZone>us-east-1d</availabilityZone>
      <instanceState>
        <code>16</code>
        <name>running</name>
      </instanceState>
    </item>

while

ec2-describe-instance-status --filter instance-status.reachability=passed -v
ec2-describe-instance-status --filter "instance-status.reachability=passed" -v

both return an empty instanceStatusSet...

Upvotes: 4

Views: 5453

Answers (2)

neoakris
neoakris

Reputation: 5075

Note: (If you have a ton of instances, as in 100's or 1000's)
Annoyingly the ec2 web console and aws cli don't let you filter instance status health check failures of a tagged subset of nodes. also --instance-ids only lets you specify a max of 100 ids, if you go over that it complains, so this also works around that.

It took me a while to come up with this hacky yet working command so I figured I'd share it in case it's of use to anyone else.

list_of_failed=$(aws --region=eu-west-1 ec2 describe-instance-status --filters "Name=instance-status.reachability,Values=failed" | cat | jq '.InstanceStatuses | .[].InstanceId')

list_of_tagged=$(aws ec2 describe-instances --filters "Name=tag:Env,Values=dev" --query 'Reservations[].Instances[]' | jq '.[].InstanceId')

echo $list_of_failed | sort > /tmp/list1
echo $list_of_tagged | sort > /tmp/list2
comm -1 -2 /tmp/list1 /tmp/list2
  • list_of_failed represents a list of instance ids that failed ec2 reachability health check. (that list is based on all possible ec2 instances in a region, and the command doesn't seem to offer you the ability to filter by tag)
  • list_of_tagged represents a list of instance ids that match tag (in the example the tag KV pair is Env=dev, you should be able to adjust the query to your needs.)
  • comm -1 -2 /tmp/list1 /tmp/list2 returns the common / union / overlapping values of the 2 lists. So it effectively gives you a list of instance id's that both failed the check and have the tag.

Upvotes: 0

Ben Whaley
Ben Whaley

Reputation: 34416

Use the AWS Command Line Interface unified tool instead.

aws ec2 describe-instance-status --instance-ids i-01234567 --filters Name=instance-status.reachability,Values=passed
{
"InstanceStatuses": [
    {
        "InstanceId": "i-01234567",
        "InstanceState": {
            "Code": 16,
            "Name": "running"
        },
        "AvailabilityZone": "us-west-2c",
        "SystemStatus": {
            "Status": "ok",
            "Details": [
                {
                    "Status": "passed",
                    "Name": "reachability"
                }
            ]
        },
        "InstanceStatus": {
            "Status": "ok",
            "Details": [
                {
                    "Status": "passed",
                    "Name": "reachability"
                }
            ]
        }
    }
]
}

Upvotes: 4

Related Questions