#!/usr/bin/env python # coding: utf-8 # ### End to End demo of starting spot instance in AWS # In[53]: get_ipython().run_line_magic('reload_ext', 'autoreload') get_ipython().run_line_magic('autoreload', '2') get_ipython().run_line_magic('matplotlib', 'inline') # In[54]: from aws_setup import * # #### Define parameters # In[3]: name='fast-ai' # #### Create new VPC by tag name # In[4]: vpc = create_vpc(name) # #### Create SSH key # In[ ]: create_ec2_keypair(name) # In[ ]: ec2 # #### Request Spot instance # In[ ]: instance_name = f'{name}-instance' instance_type = 't2.micro' # In[ ]: spot_prices = get_spot_prices(); spot_prices[instance_type] # In[ ]: launch_specs = LaunchSpecs(vpc, instance_type=instance_type).build() # In[6]: instance = create_spot_instance(instance_name, launch_specs); instance # Request on demand instance (if spot error) # In[68]: # instance = create_instance(instance_name, vpc, instance_type='t2.micro'); instance # In[74]: instance = get_instance(f'{instance_name}'); instance # #### Attach EBS volume (Optional) # In[75]: volume_tag = f'{name}-ebs-volume' # In[ ]: volume = create_volume(volume_tag, size=100) # In[ ]: _ = attach_volume(instance, volume_tag, device='/dev/xvdf') # #### Create EFS # In[ ]: efs_tag = f'{name}-efs' # In[ ]: efs = create_efs(efs_tag, vpc) # ### SSH into instance # In[77]: client = connect_to_instance(instance) # #### Mount EBS # In[63]: upload_path = Path.cwd()/'upload_scripts/mount_ebs.sh' upload_file(client, str(upload_path), 'mount_ebs.sh') # In[64]: out, _ = run_command(client, 'chmod 755 mount_ebs.sh') # In[81]: out, _ = run_command(client, './mount_ebs.sh --device /dev/xvdf') # no reformatting # out, _ = run_command(client, './mount_ebs.sh --device /dev/xvdf --reformat true') # #### Mount EFS # In[55]: efs_addr = get_efs_address('fast-ai-efs'); efs_addr # In[60]: out, _ = run_command(client, 'mkdir ~/efs_mount') # In[79]: efs_mount_cmd = f'sudo mount -t nfs -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 {efs_addr}:/ ~/efs_mount' # In[80]: out, _ = run_command(client, efs_mount_cmd) # In[ ]: