#!/usr/bin/python # Usage : # aws ec2 describe-instances --filter="Name=instance-state-name,Values=running" | aws-instances-lookup 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')