Skip to content

Instantly share code, notes, and snippets.

@TomA-R
Created July 12, 2018 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TomA-R/2dafe4ab20b554bf312e13e2182e51b0 to your computer and use it in GitHub Desktop.
Save TomA-R/2dafe4ab20b554bf312e13e2182e51b0 to your computer and use it in GitHub Desktop.
Print EC2 instances by tag value
#!/usr/bin/python
# Used on bastion host (jumpbox)
# e.g.: for feedserver in $(/usr/bin/getinstances feed-server); do echo $feedserver; ssh $feedserver; done;
# @todo: make VPC detection automatic
import sys
import json
import subprocess
from pprint import pprint
cluster = sys.argv[1]
cmd = 'aws ec2 describe-instances --filters "Name=tag-value,Values=' + cluster + '" "Name=instance-state-code,Values=16" "Name=vpc-id,Values=vpc-e5dd9d8d"'
push = subprocess.Popen(cmd, shell=True, stdout = subprocess.PIPE)
output, err = push.communicate()
parsed = json.loads(output)
for instances in parsed['Reservations']:
for instance in instances['Instances']:
print instance['PrivateIpAddress']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment