如何解决ec2.Instance'对象不可迭代
我正在尝试将指定的ec2标签复制到它们各自的卷中。当实例状态更改为“正在运行”时,将调用该函数。但是我不希望代码在每个实例上运行-对于代码的第一个版本,当我调用单个实例时,它将在所有实例上运行。即使是已经被标记的我只想让它在正在启动的特定实例上运行。
进行了一些更改,我得到错误:ec2.Instance的对象不可迭代 我对python真的很陌生,不确定如何进行。您有什么想法吗?
----这里是我的lambda代码----
import boto3
import json
def lambda_handler(event,context):
# is_test = context.function_name == 'test' # this value is injected by SAM local
instance = boto3.resource('ec2').Instance(id=event["detail"]["instance-id"])
tags_to_use = ['Stack-Name','StackID']
for instance in instance:
tags = instance.tags
to_tag = [t for t in tags if t['Key'] in tags_to_use]
for vol in instance.volumes.all():
print(f"Tagging volume {vol.id} from instance {instance.id}")
vol.create_tags(Tags=to_tag)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。