Skip to content

Instantly share code, notes, and snippets.

@sidja
Created May 17, 2017 08:45
Show Gist options
  • Save sidja/547f1d372a8516605ce926b0b910e356 to your computer and use it in GitHub Desktop.
Save sidja/547f1d372a8516605ce926b0b910e356 to your computer and use it in GitHub Desktop.

Revisions

  1. sidja created this gist May 17, 2017.
    15 changes: 15 additions & 0 deletions get_instance_tag_with_instance_id.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    import boto3

    def get_instance_name(fid):
    """
    When given an instance ID as str e.g. 'i-1234567', return the instance 'Name' from the name tag.
    :param fid:
    :return:
    """
    ec2 = boto3.resource('ec2')
    ec2instance = ec2.Instance(fid)
    instancename = ''
    for tags in ec2instance.tags:
    if tags["Key"] == 'Name':
    instancename = tags["Value"]
    return instancename