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.

Revisions

  1. renaudhager revised this gist Sep 26, 2017. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion aws-instances-lookup.py
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,6 @@

    import sys
    import json
    import subprocess

    def get_tag(instance, tag_key):
    for tag in instance['Tags']:
  2. renaudhager created this gist Sep 26, 2017.
    30 changes: 30 additions & 0 deletions aws-instances-lookup.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    #!/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
    import subprocess

    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')