在 cygwin 上启动亚马逊 ec2 实例 python boto3
starting amazon ec2 instance python boto3 on cygwin
我正在尝试通过 python3 在 cygwin 上启动一个 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-...")
我上面的代码有什么错误。
我执行脚本的方式有误。
而不是使用它
python3 start_instance.py i-03e7f6391a0f523ee on
我应该输入
python3 start_instance.py on i-03e7f6391a0f523ee
这不会出错。
我正在尝试通过 python3 在 cygwin 上启动一个 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-...")
我上面的代码有什么错误。
我执行脚本的方式有误。 而不是使用它
python3 start_instance.py i-03e7f6391a0f523ee on
我应该输入
python3 start_instance.py on i-03e7f6391a0f523ee
这不会出错。