python boto3参数验证错误

vis*_*l.k 10 python amazon-web-services boto3

这里我写了一个python程序来启动一个符合所有条件的实例。但执行程序时出现以下错误。botocore.exceptions.ParamValidationError: Parameter validation failed: Invalid type for parameter InstanceIds, value: i-012345678, type: <type 'str'>, valid types: <type 'list'>, <type 'tuple'>.下面是我的代码:

import boto3
ec2=boto3.client('ec2',region_name='ap-south-1')
a=ec2.describe_instances()
for i in a['Reservations']:
    for x in i['Instances']:
       if x['InstanceId']=="i-12345678" and x['State'['Name']=='stopped':
            n = x['InstanceId']
            ec2.start_instances(InstanceIds=n)`
Run Code Online (Sandbox Code Playgroud)

Arp*_*nki 5

错误本身是不言自明的。您必须传递实例 ID 的列表或元组,而不仅仅是字符串。你可以在文档中看到这一点

请参阅下面更新的代码。

import boto3
ec2=boto3.client('ec2',region_name='ap-south-1')
a=ec2.describe_instances()
for i in a['Reservations']:
    for x in i['Instances']:
       if x['InstanceId']=="i-12345678" and x['State'['Name']=='stopped':
            n = x['InstanceId']
            ec2.start_instances(InstanceIds=[n])`
Run Code Online (Sandbox Code Playgroud)