python - Mount EBS volume using boto3
<p>I want to use AWS Spot instances to train Neural Networks. To prevent loss of the model when the spot instance is terminated, I plan to create a snapshot of the EBS volume, make a new volume and attach it to a reserved instance. How can I mount, or make the EBS volume available using python &amp; boto3. </p>

<p>These are the steps used to <a href="http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-using-volumes.html" rel="nofollow">make the volume available</a> on Linux, but I want to automate the process so that I don't need to SSH into the instance every time. Here is the code I use to attach the volume - </p>

<pre><code>import boto3
ec2 = boto3.resource('ec2')

spot = ec2.Instance('i-9a8f5082')
res = ec2.Instance('i-86e65a13')

snapshot = ec2.create_snapshot(VolumeId="vol-5315f7db", Description="testing spot instances")
volume = ec2.create_volume(SnapshotId=snapshot.id, AvailabilityZone='us-west-2a')
res.attach_volume(VolumeId="vol-5315f7db", Device='/dev/sdy')
snapshot.delete()
</code></pre>

<p>What's wrong with sending and execute ssh script remotely? Assume you are using ubuntu , i.e. </p>

<pre><code>ssh -i  your.pem ubuntu@ec2_name_or_ip  'sudo bash -s' &lt; mount_script.sh 
</code></pre>

<p>If you attach tag to those resources, you can later use boto3 to inquired the resources by universal tag name, instead tied to the specific static id. </p>

python linux amazon-web-services boto3