“start_instances()仅接受AWS EC2 Boto3中的关键字参数”错误

时间:2017-11-06 23:52:38

标签: python amazon-web-services amazon-ec2 boto3

我正在尝试使用EC2 instance开始boto3。当我执行以下代码时,它可以正常工作

import boto3


ec2client = boto3.client('ec2')

class StartInstances:

    def start_ec_instances(self):
        response = ec2client.start_instances(InstanceIds=['i-XXXXXXXXXX'])
        return

StartInstances().start_ec_instances()

但是当我运行下面的代码时,我得到了

import boto3


ec2client = boto3.client('ec2')

class StartInstances:

    def start_ec_instances(self, instanceid):
        response = ec2client.start_instances(instanceid)
        return

StartInstances().start_ec_instances('InstanceIds=[\'i-XXXXXXXXXX\']')
  

Traceback(最近一次调用最后一次):文件   “/Users/xxx/PycharmProjects/ctm-scripting-utils/ec2/start_instances.py”   第25行,在       StartInstances()。start_ec_instances( “InstanceIds = [\ 'I-XXXXXXXXXX \']”)   文件   “/Users/xxx/PycharmProjects/ctm-scripting-utils/ec2/start_instances.py”   第11行,在start_ec_instances中       response = ec2client.start_instances(instanceids)File“/Users/xxx/Library/Python/3.6/lib/python/site-packages/botocore/client.py”,   第310行,在_api_call中       “%s()只接受关键字参数。” %py_operation_name)TypeError:start_instances()只接受关键字参数。

1 个答案:

答案 0 :(得分:4)

更多的Python问题。您正在尝试传递string'InstanceIds=[\'i-XXXXXXXXXX\']'而不是kwargsInstanceIds=[..]。一种可能的解决方法是:

class StartInstances:
    def start_ec_instances(self, instanceid):
        response = ec2client.start_instances(InstanceIds=[instanceid])
        return

StartInstances().start_ec_instances('i-XXXXXXXXXX')