Skip to content

Instantly share code, notes, and snippets.

@pcolazurdo
Created January 3, 2020 15:13
Show Gist options
  • Save pcolazurdo/264d256d7e549fb339f40101cdf3889c to your computer and use it in GitHub Desktop.
Save pcolazurdo/264d256d7e549fb339f40101cdf3889c to your computer and use it in GitHub Desktop.
List key details for ec2 instances
#!/bin/bash
# $1 region
MYREGION=${1:-eu-west-1}
total_instances_output=$(aws ec2 describe-instances --region $MYREGION)
total_instances=$(echo "${total_instances_output}" | jq '.Reservations[].Instances[] | [.InstanceId] | @tsv')
named_instances=$(echo "$total_instances_output" |jq '.Reservations[].Instances[] | try select (.Tags[].Value = "Key") | [.InstanceId, .PublicIpAddress,.PrivateIpAddress,.State.Name,(.Tags | from_entries | .Name)] | @tsv')
echo Named Instances: "$(echo "$named_instances" | grep -v -e '^$'| wc -l)"
echo
echo "$total_instances_output" | \
jq -r '.Reservations[].Instances[] | try select (.Tags[].Value = "Key") | [.InstanceId, .State.Name, (.Tags | from_entries | .Name) // "NONAME", .PrivateIpAddress, .PublicIpAddress] | @tsv' | \
awk '{printf ("%-19s %-10s %-40s %15s %15s \n", $1, $2, substr($3,1,40), $4, $5)}'
echo
echo total Instances: "$(echo "$total_instances" | grep -v -e '^$'| wc -l)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment