0
votes

I am trying to edit tag name in EC2 with a Lambda function in python 3.7

Unfortunately, I get the following error :

[ERROR] AttributeError: 'ec2.ServiceResource' object has no attribute 'delete_tags'

But I have checked the documentation on boto3 and "delete_tags" class exist : https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#id996

The "create_tags" class work well.

Here is the lambda function :

import boto3
ec2 = boto3.resource('ec2')

def lambda_handler(event, context):
    filters = [{
            'Name': 'tag:Scheduler',
            'Values': ['True']
        }]
    instances = ec2.instances.filter(Filters=filters)
    ids = [instance.id for instance in instances] 
    print(ids)
    ec2.delete_tags(Resources=ids,Tags=[{'Key': 'Scheduler','Value': 'True'}])
    ec2.create_tags(Resources=ids,Tags=[{'Key': 'Scheduler','Value': 'False'}])
1
You are using boto3.resource but the class that you found is coming from boto3.client.Lamanus

1 Answers

0
votes

The syntax for delete_tags is :

response = instance.delete_tags(
    DryRun=True|False,
    Tags=[
        {
            'Key': 'string',
            'Value': 'string'
        },
    ]
)

Thus you should just use :

ec2.delete_tags(Tags=[{'Key': 'Scheduler','Value': 'True'}])

# similar for create_tags
ec2.create_tags(Tags=[{'Key': 'Scheduler','Value': 'False'}])