【发布时间】:2018-08-19 11:47:02
【问题描述】:
我正在尝试通过 cygwin 上的 python3 启动亚马逊 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-...")
我上面的代码有什么错误。
【问题讨论】:
标签: python-3.x amazon-ec2 cygwin boto3 ec2-ami