Forked from sidja/get_instance_tag_with_instance_id.py
Created
April 9, 2020 03:35
-
-
Save jonathanhle/740ba3a326d46bed095e194240d0a3ff to your computer and use it in GitHub Desktop.
Using Python and Boto3 to get Instance Tag information with instance id
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment