boto3 how to get all tags for a single ec2 instance and append new ones

562 Views Asked by At

My task would seem to be simple but I am not seeing in the documentation nor on the web. I have an ec2 instance id and I want to get all the currents tags and add additional ones. All I see for examples is how to get all instances running or in a vpc. The boto2 document only shows how to retrieve a single tag or multiple but not all.

1

There are 1 best solutions below

0
John Rotenstein On

These days, you should be using boto3.

To retrieve the tags of an instance, use describe_instances().

It returns:

{
    'Reservations': [
        {
            'Instances': [
                {
                    'Tags': [
                        {
                            'Key': 'string',
                            'Value': 'string'
                        },

To set the tags on an instance, use create_tags(), specifying the ID of the instance.