困惑,boto3 github project提到他们添加了对删除标记的支持。但是,当我在下面执行我的代码片段时,会引发以下异常:
ec2 = boto3.resource('ec2', region_name=aws_region)
ec2.delete_tags(Resources=[instance.id],Tags=[{"Key": non_compliant_tag_name}])
'ec2.ServiceResource' object has no attribute 'delete_tags'
$ pip show boto3
Name: boto3
Version: 1.4.4
我做错了什么?
答案 0 :(得分:3)
delete_tags()
方法应该在client
对象而不是resource
对象上调用:
import boto3
client = boto3.client('ec2', region_name='ap-southeast-2')
...
client.delete_tags(Resources=[instance.id],Tags=[{"Key": non_compliant_tag_name}])
答案 1 :(得分:0)
您可以在Python中如下使用它
import boto3
reservations = ec2.describe_instances(
Filters=[
#{'Name': 'tag:Type', 'Values': ['management']},
]
).get(
'Reservations', []
)
instances = sum(
[
[i for i in r['Instances']]
for r in reservations
], [])
for instance in instances:
# Delete the tag 'baz' if it exists
ec2.delete_tags(Resources=[instance['InstanceId']], Tags=[{"Key": "TAGNAME"}])