Skip to content

Instantly share code, notes, and snippets.

@renaudhager
Last active September 26, 2017 17:29
Show Gist options
  • Select an option

  • Save renaudhager/3600a3572c1f748536d53e151c5b700f to your computer and use it in GitHub Desktop.

Select an option

Save renaudhager/3600a3572c1f748536d53e151c5b700f to your computer and use it in GitHub Desktop.
Query aws api to lookup for tag=value and print the instance name
#!/usr/bin/python
# Usage :
# aws ec2 describe-instances --filter="Name=instance-state-name,Values=running" | aws-instances-lookup <tag name> <tag value>
import sys
import json
def get_tag(instance, tag_key):
for tag in instance['Tags']:
if tag['Key'] == tag_key:
print tag['Value']
tag_key = sys.argv[1]
tag_value = sys.argv[2]
instances_id = []
instances_name = []
output = json.load(sys.stdin)
reservations = output["Reservations"]
for reservation in reservations:
instance = reservation['Instances'][0]
for tag in instance['Tags']:
if tag['Key'] == tag_key:
if tag['Value'] == tag_value:
instances_id.append(str(instance['InstanceId']))
get_tag(instance, 'Name')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment