Python – How to delete tag from EBS using boto3

amazon-ebsamazon-lambdaamazon-web-servicesbotopython

I want to delete a tag from EBS volume using boto3/lambda. At this doc https://boto3.readthedocs.io/en/latest/reference/services/ec2.html#volume I see 'create_tags()' only. It is suitable for modifying tags and e.g. to set an empty value but I need to remove the tag completely.

I tried create_tags() using an empty value and just create_tags() with all tags I need to keep and excluding ones but it does not work: the tags I want to delete remain untouched.

    tagRes = boto3.resource('ec2', region_name=region);
    volume = tagRes.Volume(id);
    ...
    tagsToKeep.append({'Key': tagName, 'Value': 'keep it'})
    ...
    ret = volume.create_tags(
        Resources=[id],
        Tags=tagsToKeep
    );

So, how to delete a tag from EBS volume?

Best Answer

It works in quite unexpected way: despite you create tags using resource, you have to delete them using client:

id = 'vol-5e0e9c3333'
tagRes = boto3.client('ec2', region_name=region)
ret = tagRes.delete_tags(
    Resources=[id],
    Tags=[{'Key': 'tag_name_to_del' }]
);
Related Topic