【问题标题】:"start_instances() only accepts keyword arguments" error in AWS EC2 Boto3AWS EC2 Boto3 中的“start_instances() 仅接受关键字参数”错误
【发布时间】:2017-11-06 23:52:38
【问题描述】:

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

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 响应 = ec2client.start_instances(instanceids) 文件“/Users/xxx/Library/Python/3.6/lib/python/site-packages/botocore/client.py”, 第 310 行,在 _api_call 中 “%s() 只接受关键字参数。” % py_operation_name) TypeError: start_instances() 只接受关键字参数。

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-01
      • 2018-07-24
      • 2015-08-18
      相关资源
      最近更新 更多