boto3 start instance

407 views
Skip to first unread message

Massimo Canonico

unread,
Jun 6, 2017, 12:02:25 PM6/6/17
to boto-...@googlegroups.com
Hi all,

I'm starting using boto3 and the installation/configuration went well.
Now I'm trying to use the source code proposed to "start instance" (the
code is after my signature).

If I understand correctly, what I have to do is run the code with two
parameters instance_id and an action.

My point is that, since I want to start an instance, I still not have a
instance_id.

I have tried to run the code like this: python botoFirstExample.py
ami-c58c1dd3 on

but it complains:

[mex@hope ~]$ python botoFirst.py ami-c58c1dd3 on
Traceback (most recent call last):
File "botoFirst.py", line 28, in <module>
ec2.stop_instances(InstanceIds=[instance_id], DryRun=True)
File "/usr/lib/python2.7/site-packages/botocore/client.py", line 253,
in _api_call
return self._make_api_call(operation_name, kwargs)
File "/usr/lib/python2.7/site-packages/botocore/client.py", line 557,
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-...")

So, since I do not have a valid id (i-...), how can I use the source
code you provide me?

Thanks in advance,

Massimo

[mex@hope ~]$ cat botoFirst.py
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 witout dryrun
try:
response = ec2.stop_instances(InstanceIds=[instance_id],
DryRun=False)
print(response)
except ClientError as e:
print(e)



Victor Trac

unread,
Jun 6, 2017, 12:17:20 PM6/6/17
to boto-users
What you want is run_instances(), which takes an an AMI. See: http://boto.cloudhackers.com/en/latest/ec2_tut.html#launching-instances




--
You received this message because you are subscribed to the Google Groups "boto-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to boto-users+unsubscribe@googlegroups.com.
To post to this group, send email to boto-...@googlegroups.com.
Visit this group at https://groups.google.com/group/boto-users.
For more options, visit https://groups.google.com/d/optout.

Massimo Canonico

unread,
Jun 7, 2017, 11:19:35 AM6/7/17
to boto-...@googlegroups.com

Thank you Victor.

I have tried to start the "Amazon Linux AMI 2017.03.0 (HVM), SSD Volume Type - ami-c58c1dd3 since it worked on web console, but the last command in this sequence got an error:

import boto3

ec2 = boto3.resource('ec2')

ec2.create_instances(ImageId='ami-c58c1dd3', MinCount=1, MaxCount=1)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/boto3/resources/factory.py", line 520, in do_action
    response = action(self, *args, **kwargs)
  File "/usr/lib/python2.7/site-packages/boto3/resources/action.py", line 83, in __call__
    response = getattr(parent.meta.client, operation_name)(**params)


  File "/usr/lib/python2.7/site-packages/botocore/client.py", line 253, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/usr/lib/python2.7/site-packages/botocore/client.py", line 557, in _make_api_call
    raise error_class(parsed_response, operation_name)

botocore.exceptions.ClientError: An error occurred (InvalidParameterCombination) when calling the RunInstances operation: Non-Windows instances with a virtualization type of 'hvm' are currently not supported for this instance type.

The problem seems related to "hvm" but I'm not sure how is it related to boto.

The problem is that all linux VM on AmazonWS are "hvm".

M

To unsubscribe from this group and stop receiving emails from it, send an email to boto-users+...@googlegroups.com.

Victor Trac

unread,
Jun 7, 2017, 12:01:54 PM6/7/17
to boto-...@googlegroups.com

That's not true. See "PV" instances here: https://aws.amazon.com/amazon-linux-ami/

Also, the error message itself is helpful in this case: "... currently not supported for this instance type."  You can try passing in an instance type instead of using the default.

--Victor
Reply all
Reply to author
Forward
0 new messages