Skip to content

Instantly share code, notes, and snippets.

@nmagee
Last active October 2, 2018 20:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmagee/d13a67b82859fcef53acff568ecb114d to your computer and use it in GitHub Desktop.
Save nmagee/d13a67b82859fcef53acff568ecb114d to your computer and use it in GitHub Desktop.
Use JQ to parse AWS CLI requests
#!/bin/bash
# Display the EIPs associated with all of your instances:
aws ec2 describe-instances \
| jq -r .Reservations[].Instances[].PublicIpAddress
# Display the EIP attached to a specific EC2 instance:
aws ec2 describe-instances --instance-ids i-1a2b3c4d5e6f7g8h9i \
| jq -r .Reservations[0].Instances[0].PublicIpAddress
# Retrieve all tags associated with an instance:
aws ec2 describe-instances --instance-ids i-1a2b3c4d5e6f7g8h9i \
| jq -r .Reservations[0].Instances[0].Tags
# List all EC2 instances attached to an ELB:
aws elb describe-load-balancers \
| jq -r .LoadBalancerDescriptions[0].Instances[0].InstanceId
# Put a value into a variable for logic. This request tells you
# the state of an instance [stopped|running]
state=`aws ec2 describe-instances --instance-ids i-02438e5e535b738a0 \
| jq -r .Reservations[0].Instances[0].State.Name`
echo $state
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment