Get all instance ID’s for an account
You can drop the filter for non-running.
aws ec2 describe-instances --filters Name=instance-state-name,Values=running | jq -r ".Reservations[].Instances[].InstanceId"
Get ec2 instance based on name filter with jq
Pull Private IP’s of all Linux instances
aws ec2 describe-instances --filters Name=instance-state-name,Values=running | jq -c -r '.Reservations[].Instances[] | [.InstanceId, .PrivateIpAddress, .PlatformDetails,.KeyName ]' | sed 's/\[//g' | sed 's/\]//g' | grep 'Linux/UNIX' | cut -d',' -f2 | sed 's/"//g'
Pull all subnets and create a json of the results with only the results you care about (this needs a better title)
aws ec2 describe-subnets | jq '.Subnets[] | { SubnetId: .SubnetId, OwnerId: .OwnerId }' | jq -s
Taking it a step further, I typically just want to pull a Subnet ID for a particular subnet forwhich I roughly know it’s name. The following will print actual JSON of the Subnet’s name and Subnet ID
aws ec2 describe-subnets | jq '.Subnets[] | select(.Tags[].Key=="Name") | {Name: .Tags[].Value, SubnetId: .SubnetId}' | jq -s
jq: error (at <stdin>:247): Cannot iterate over null (null)
[
{
"Name": "Some Subnet 0",
"SubnetId": "subnet-696969696969"
},
{
"Name": "Some Subnet 2",
"SubnetId": "subnet-420420420420"
}
]
I have to do this sometimes to find an instance in one of our larger accounts. To compile the results, I create something like this:
echo '{'
for account_name do
echo '"'$ACCOUNT_NAME'": [
aws ec2 describe-instances
echo ']'
done
echo '}'
There’s obviously a little bit more to it, but essentially the output that I’m chasing looks like this:
{
"account_name": [
"Reservations": [
{
...
"Instances": [
{data},
{data}
]
}
]
]
}
If I want to find an instance based on it’s Name
tag, I can just do the following:
aws ec2 describe-instances | jq '. | select(.Tags[].Key=="Name") | {Name: .Tags[].Value, SubnetId: .SubnetId}' | jq -s