2
votes

I'm trying to start an EC2 instance using boto3. When I execute the below code it works fine

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()

But when I run the code below I get

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 (most recent call last): File "/Users/xxx/PycharmProjects/ctm-scripting-utils/ec2/start_instances.py", line 25, in StartInstances().start_ec_instances("InstanceIds=[\'i-XXXXXXXXXX\']") File "/Users/xxx/PycharmProjects/ctm-scripting-utils/ec2/start_instances.py", line 11, in start_ec_instances response = ec2client.start_instances(instanceids) File "/Users/xxx/Library/Python/3.6/lib/python/site-packages/botocore/client.py", line 310, in _api_call "%s() only accepts keyword arguments." % py_operation_name) TypeError: start_instances() only accepts keyword arguments.

1

1 Answers

5
votes

More of a Python question. You are trying to pass a string: 'InstanceIds=[\'i-XXXXXXXXXX\']' instead of kwargs: InstanceIds=[..]. One possible way to fix is:

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

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