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 (将#修改为@)