Skip to content

Instantly share code, notes, and snippets.

@niksrc
Created July 18, 2018 06:49
Show Gist options
  • Save niksrc/02c38d849ef26654c4ecf19d2e6c1c2e to your computer and use it in GitHub Desktop.
Save niksrc/02c38d849ef26654c4ecf19d2e6c1c2e to your computer and use it in GitHub Desktop.
Get non reserved instances running in EC2
reserved=$(aws ec2 describe-reserved-instances | jq '.ReservedInstances | map(select(.State == "active")) | map({ (.InstanceType): .InstanceCount }) | map(to_entries) | add | group_by(.key) | map({key: .[0].key, value: map(.value) | add }) | from_entries')
running=$(aws ec2 describe-instances | jq '.Reservations | map(.Instances) | flatten | map(select(.State.Code == 16 and .InstanceLifecycle != "spot")) | map(.InstanceType) | group_by(.) | map({ (.[0]|tostring) : .|length }) | add')
echo $reserved, $running
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment