#!/usr/bin/env python # coding: utf-8 # ### End to End demo of starting spot instance in AWS # In[1]: get_ipython().run_line_magic('reload_ext', 'autoreload') get_ipython().run_line_magic('autoreload', '2') get_ipython().run_line_magic('matplotlib', 'inline') # In[3]: from aws_setup import * # In[4]: name='fast-ai' # In[5]: vpc = create_vpc(name) # In[9]: list(ec2.vpcs.filter(Filters=[{'Name':'tag:Name', 'Values':[name]}])) # In[11]: create_ec2_keypair(name) # #### Request Spot instance # In[25]: instance_name = f'{name}-instance' instance_type = 'c5.xlarge' # In[26]: spot_prices = get_spot_prices(); spot_prices[instance_type] # In[21]: sorted(spot_prices.items(), key=lambda x: float(x[1])) # In[24]: sorted([o for o in spot_prices.items() if o[0].startswith('c5')], key=lambda x: float(x[1])) # In[27]: launch_specs = LaunchSpecs(vpc, instance_type=instance_type).build() # In[28]: 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[29]: instance = get_instance(f'{instance_name}'); instance # #### Attach EBS volume (Optional) # In[30]: volume_tag = f'{name}-ebs-volume' # In[40]: az = list(vpc.network_interfaces.all())[0].availability_zone az # In[42]: az # In[43]: volume = create_volume(volume_tag, az=az, size=100) # In[ ]: _ = attach_volume(instance, volume_tag, device='/dev/xvdf') # #### Create EFS # In[45]: efs_tag = f'{name}-efs' # In[47]: efs = create_efs(efs_tag, vpc) # ### SSH into instance # In[48]: client = connect_to_instance(instance) # #### Mount EBS # In[49]: upload_path = Path.cwd()/'upload_scripts/mount_ebs.sh' upload_file(client, str(upload_path), 'mount_ebs.sh') # In[50]: out, _ = run_command(client, 'chmod 755 mount_ebs.sh') # In[51]: 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[52]: efs_addr = get_efs_address('fast-ai-efs'); efs_addr # In[53]: out, _ = run_command(client, 'mkdir ~/efs_mount') # In[54]: mount_opts = 'nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2' efs_mount_cmd = f'sudo mount -t nfs -o {mount_opts} {efs_addr}:/ ~/efs_mount' # In[59]: out, _ = run_command(client, efs_mount_cmd) # In[60]: out # In[61]: client.close() # In[63]: keypath=f'{Path.home()}/.ssh/aws-key-fast-ai.pem' username='ubuntu' f'ssh -i {keypath} {username}@{instance.public_ip_address}' # In[ ]: