query your EC2 instances using AWS CLI


While AWS CLI is well documented, I was a bit surprised that I was not able to find many samples of tag querying and filtering…. here is small snippet I created last weekend

  • selects from one region
  • queries several fields including two custom tags, in my case “Name” and “instance_role”
  • wildcard filter on custom tag in my case Name like *postgres*
  • outputs as easy to read sorted table
aws ec2 describe-instances \
    --output   table \
    --region   us-east-1 \
    --query   'Reservations[].Instances[].[ Tags[?Key==`Name`].Value | [0], Tags[?Key==`instance_role`].Value | [0], PublicIpAddress, PrivateIpAddress, State.Name, Placement.AvailabilityZone, InstanceId, InstanceType, LaunchTime ]' \
    --filters 'Name=tag:Name,Values=*postgres*' \
   | sort -n -k 2

it cannot be easier than that

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: