1

I am running machine learning scripts that take a long time to finish. I want to run them on AWS on a faster processor and stop the instance when it finishes.

Can boto be used within the running script to stop its own instance? Is there a simpler way?

3
  • You can use boto, here is the documentation for EC2 management docs.pythonboto.org/en/latest/ec2_tut.html. Commented Nov 21, 2016 at 20:26
  • Yes. Boto can be used. Boto3 is better. When do you mean by close down? stop or terminate? Commented Nov 21, 2016 at 20:27
  • Stop. So it can even close down the instance that is running the boto script? Commented Nov 21, 2016 at 20:29

2 Answers 2

2

If your EC2 instance is running Linux, you can simply issue a halt or shutdown command to stop your EC2 instance. This allows you to shutdown your EC2 instance without requiring IAM permissions.

Sign up to request clarification or add additional context in comments.

4 Comments

Take a look here on executing a shell command from Python: stackoverflow.com/questions/89228/…
import subprocess followed by subprocess.call(["halt"]).
@shahar_m did you try this out? It looks a little less elegant but a lot simpler.
1

See Creating a Connection on how to create a connection. Never tried this one before, so use caution. Also make sure the instance is EBS backed. Otherwise the instance will be terminated when you stop it.

import boto.ec2
import boto.utils

conn = boto.ec2.connect_to_region("us-east-1") # or your region

# Get the current instance's id
my_id = boto.utils.get_instance_metadata()['instance-id']

conn.stop_instances(instance_ids=[my_id])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.