I am trying to launch an amazon ec2 instance via python3 on cygwin. Normally on cygwin command line I can start instance with following command
aws ec2 start-instances --instance-ids i-03e7f6391a0f523ee
But now programming in python3 and trying to start instance via python script gives me error. Here is code
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)
I run it on 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-...")
What is the mistake in my code above.
There was error the way I was executing the script. Rather than using it
python3 start_instance.py i-03e7f6391a0f523ee on
I should have typed
python3 start_instance.py on i-03e7f6391a0f523ee
This won't give errors.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.