在cygwin上启动Amazon ec2实例python boto3

时间:2018-08-19 02:15:02

标签: python-3.x amazon-ec2 cygwin boto3 ec2-ami

我正在尝试在cygwin上通过python3启动一个Amazon ec2实例。 通常在cygwin命令行上,我可以使用以下命令

启动实例
aws ec2 start-instances --instance-ids i-03e7f6391a0f523ee

但是现在在python3中编程并尝试通过python脚本启动实例给了我错误。这是代码

import sys
import boto3
from botocore.exceptions import ClientError

instance_id = sys.argv[2]
action = sys.argv[1].upper()

ec2 = boto3.client('ec2')


if action == 'ON':
    # Do a dryrun first to verify permissions
    try:
        ec2.start_instances(InstanceIds=[instance_id], DryRun=True)
    except ClientError as e:
        if 'DryRunOperation' not in str(e):
            raise

    # Dry run succeeded, run start_instances without dryrun
    try:
        response = ec2.start_instances(InstanceIds=[instance_id], DryRun=False)
        print(response)
    except ClientError as e:
        print(e)
else:
    # Do a dryrun first to verify permissions
    try:
        ec2.stop_instances(InstanceIds=[instance_id], DryRun=True)
    except ClientError as e:
        if 'DryRunOperation' not in str(e):
            raise

    # Dry run succeeded, call stop_instances without dryrun
    try:
        response = ec2.stop_instances(InstanceIds=[instance_id], DryRun=False)
        print(response)
    except ClientError as e:
        print(e)

我在cygwin上运行

 python3 start_instance.py i-03e7f6391a0f523ee on
Traceback (most recent call last):
  File "start_instance.py", line 28, in <module>
    ec2.stop_instances(InstanceIds=[instance_id], DryRun=True)
  File "/usr/lib/python3.6/site-packages/botocore/client.py", line 314, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/usr/lib/python3.6/site-packages/botocore/client.py", line 612, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (InvalidInstanceID.Malformed) when calling the StopInstances operation: Invalid id: "on" (expecting "i-...")

上面我的代码有什么错误。

1 个答案:

答案 0 :(得分:0)

我执行脚本的方式出错。 而不是使用

python3 start_instance.py i-03e7f6391a0f523ee on

我应该输入

python3 start_instance.py on i-03e7f6391a0f523ee 

这不会出错。