Using Python and Boto3 to get Instance Tag information

Here are 2 sample functions to illustrate how you can get information about Tags on instances using Boto3 in AWS.

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.
    ec2 = boto3.resource('ec2')
    ec2instance = ec2.Instance(fid)
    instancename = ''
    for tags in ec2instance.tags:
        if tags["Key"] == 'Name':
            instancename = tags["Value"]
    return instancename

In this function, I create the ec2 resource object using the instance ID passed to the function. I iterate through the Tags of the instance until I find the ‘Name’ Tag and return its value. This is a very simple function that can pull any tag value, really.

Next up, this function will list all instances with a certain Tag name and certain Value on that tag.

import boto3

def list_instances_by_tag_value(tagkey, tagvalue):
    # When passed a tag key, tag value this will return a list of InstanceIds that were found.

    ec2client = boto3.client('ec2')

    response = ec2client.describe_instances(
                'Name': 'tag:'+tagkey,
                'Values': [tagvalue]
    instancelist = []
    for reservation in (response["Reservations"]):
        for instance in reservation["Instances"]:
    return instancelist

Here I use ‘response’ to collect the instances which fall into the Filter used. Take note that I used the tag: + tagkey. tag-value would return any instance that this value on any tag. Tag-key returns any instance with the tag name field that matches here, regardless of the value. I want a specific tag and specific value.

Tagged , , , . Bookmark the permalink.

3 Responses to Using Python and Boto3 to get Instance Tag information

  1. Ryan says:

    I have tried the first solution above while passing the instance id from a cloudevent . It does not return anything. Any ideas, have things changed since you posted this?

    • mike says:

      By “not return anything” are you getting a timeout, python/boto error, does it just sit there and you have to ctrl+c it to break the process? Are you handling the ‘cloudevent’ through lambda?

      If you want some direction, I’d need more specifics on the error and how you are running the function.

      • ryan says:

        I get a successful response but no information.
        Running it like this
        def get_instance_name(thisInstanceID):
        ec2 = boto3.resource(‘ec2’)
        ec2instance = ec2.Instance(thisInstanceID)
        instancename = ”
        for tags in ec2instance.tags:
        if tags[“Key”] == ‘Name’:
        instancename = tags[“Value”]

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve : *
12 + 22 =