[英]Starting multiple EC2 instances using Boto3
我正在使用下面的代碼獲取實例列表
def list_instances_by_tag_value(self, tagkey, tagvalue):
ec2client = boto3.client('ec2')
response = ec2client.describe_instances(
Filters=[
{
'Name': 'tag:'+tagkey,
'Values': [tagvalue]
}
]
)
instancelist = []
for reservation in (response["Reservations"]):
for instance in reservation["Instances"]:
instancelist.append(instance["InstanceId"])
return instancelist
現在,方法list_instances_by_tag_value
返回一個List
。 現在,我需要啟動EC2 instances
列表。 所以我正在做類似下面的事情
def start_ec_instances(self, instanceids):
response = ec2client.start_instances(InstanceIds=instanceids)
return
其中instanceids
是從第一個方法返回的列表。 但是ec2client.start_instances
僅接受String
而不接受List
。
我知道我可以將list
轉換為String
,然后解析它。 我需要在instanceID前面附加('),並在每個實例ID之間附加逗號(,)。
問題是,除了將列表轉換為字符串並執行某些append
操作之外,還有什么簡便的方法嗎?
它需要看起來像'i-XXXXXX', 'i-XXXXX', 'i-XXXXXXX'
編輯:當我從第一種方法將列表傳遞給start_instances
,它說Invalid type for parameter InstanceIds[0], value: ['i-ssss', 'i-YYYY', 'i-ZZZZ', 'i-KKKK'], type: <class 'list'>, valid types: <class 'str'>
您更有可能稱之為:
response = ec2client.start_instances(InstanceIds=[instanceids])
代替:
response = ec2client.start_instances(InstanceIds=instanceids)
start_instances
在InstanceIds
字段中獲取字符串列表 。 不確定從何處獲取逗號分隔的字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.