%reload_ext autoreload
%autoreload 2
%matplotlib inline
from aws_setup import *
name='fast-ai'
vpc = create_vpc(name)
list(ec2.vpcs.filter(Filters=[{'Name':'tag:Name', 'Values':[name]}]))
[ec2.Vpc(id='vpc-ee793297')]
create_ec2_keypair(name)
Created keypair
instance_name = f'{name}-instance'
instance_type = 'c5.xlarge'
spot_prices = get_spot_prices(); spot_prices[instance_type]
'0.062500'
sorted(spot_prices.items(), key=lambda x: float(x[1]))
[('m3.medium', '0.006700'), ('m1.medium', '0.008700'), ('t2.medium', '0.013900'), ('c3.large', '0.027300'), ('c4.large', '0.028700'), ('r3.large', '0.030800'), ('m2.2xlarge', '0.049000'), ('c1.xlarge', '0.052000'), ('t2.xlarge', '0.055700'), ('t2.large', '0.055800'), ('m3.xlarge', '0.057400'), ('c4.xlarge', '0.059300'), ('c3.xlarge', '0.059800'), ('m4.xlarge', '0.060200'), ('c5.xlarge', '0.062500'), ('r3.xlarge', '0.069400'), ('r4.xlarge', '0.085600'), ('m2.4xlarge', '0.098000'), ('i3.xlarge', '0.102600'), ('c3.2xlarge', '0.113000'), ('c5.large', '0.122100'), ('r4.large', '0.123500'), ('m5.large', '0.123600'), ('m4.2xlarge', '0.123600'), ('r3.2xlarge', '0.133300'), ('c5.2xlarge', '0.140600'), ('m5.2xlarge', '0.147600'), ('r4.2xlarge', '0.154700'), ('c4.2xlarge', '0.160700'), ('h1.2xlarge', '0.165000'), ('m1.large', '0.175000'), ('i3.2xlarge', '0.191800'), ('m1.xlarge', '0.219000'), ('c3.4xlarge', '0.235000'), ('c4.4xlarge', '0.241100'), ('g2.2xlarge', '0.250800'), ('m4.4xlarge', '0.288400'), ('r3.4xlarge', '0.299000'), ('c5.4xlarge', '0.304000'), ('h1.4xlarge', '0.330000'), ('r4.4xlarge', '0.354000'), ('i3.4xlarge', '0.377100'), ('p2.xlarge', '0.454000'), ('m3.2xlarge', '0.482600'), ('c3.8xlarge', '0.491400'), ('x1e.2xlarge', '0.500400'), ('c4.8xlarge', '0.506300'), ('i2.2xlarge', '0.511500'), ('r3.8xlarge', '0.521300'), ('m4.10xlarge', '0.601800'), ('f1.2xlarge', '0.628000'), ('d2.2xlarge', '0.635000'), ('g3.8xlarge', '0.699500'), ('r4.8xlarge', '0.700400'), ('i3.8xlarge', '0.797700'), ('p3.2xlarge', '0.933500'), ('x1e.4xlarge', '1.000800'), ('g3.4xlarge', '1.078000'), ('r4.16xlarge', '1.095900'), ('c5.18xlarge', '1.150100'), ('i2.4xlarge', '1.231300'), ('cc2.8xlarge', '1.261500'), ('h1.16xlarge', '1.320000'), ('i3.16xlarge', '1.497600'), ('m5.24xlarge', '1.516500'), ('g2.8xlarge', '1.671100'), ('x1e.8xlarge', '2.001600'), ('i2.8xlarge', '2.046000'), ('h1.8xlarge', '2.132000'), ('p2.8xlarge', '2.160000'), ('c5.9xlarge', '2.197600'), ('d2.8xlarge', '2.334000'), ('cr1.8xlarge', '3.500000'), ('m4.16xlarge', '3.906800'), ('x1.32xlarge', '4.001400'), ('p3.16xlarge', '10.288000'), ('p3.8xlarge', '12.488000'), ('f1.16xlarge', '13.200000'), ('x1e.32xlarge', '26.688000')]
sorted([o for o in spot_prices.items() if o[0].startswith('c5')], key=lambda x: float(x[1]))
[('c5.xlarge', '0.062500'), ('c5.large', '0.122100'), ('c5.2xlarge', '0.140600'), ('c5.4xlarge', '0.304000'), ('c5.18xlarge', '1.150100'), ('c5.9xlarge', '2.197600')]
launch_specs = LaunchSpecs(vpc, instance_type=instance_type).build()
instance = create_spot_instance(instance_name, launch_specs); instance
Waiting on spot fullfillment... Fullfillment completed. InstanceId: i-00e902bf70c696999 Rebooting... Completed. SSH: ssh -i ~/.ssh/aws-key-fast-ai.pem ubuntu@35.162.88.124
ec2.Instance(id='i-00e902bf70c696999')
Request on demand instance (if spot error)
# instance = create_instance(instance_name, vpc, instance_type='t2.micro'); instance
instance = get_instance(f'{instance_name}'); instance
ec2.Instance(id='i-00e902bf70c696999')
volume_tag = f'{name}-ebs-volume'
az = list(vpc.network_interfaces.all())[0].availability_zone
az
'us-west-2b'
az
'us-west-2b'
volume = create_volume(volume_tag, az=az, size=100)
--------------------------------------------------------------------------- ClientError Traceback (most recent call last) <ipython-input-43-5d225daaeb7c> in <module>() ----> 1 volume = create_volume(volume_tag, az=az, size=100) /mnt/c/Users/j/git/imagenet-fast/aws/aws_setup.py in create_volume(name, az, size, volume_type) 238 }] 239 volume = ec2.create_volume(Size=size, VolumeType=volume_type, TagSpecifications=tag_specs, --> 240 AvailabilityZone=az) 241 return volume 242 ~/anaconda3/lib/python3.6/site-packages/boto3/resources/factory.py in do_action(self, *args, **kwargs) 518 # instance via ``self``. 519 def do_action(self, *args, **kwargs): --> 520 response = action(self, *args, **kwargs) 521 522 if hasattr(self, 'load'): ~/anaconda3/lib/python3.6/site-packages/boto3/resources/action.py in __call__(self, parent, *args, **kwargs) 81 operation_name, params) 82 ---> 83 response = getattr(parent.meta.client, operation_name)(**params) 84 85 logger.debug('Response: %r', response) ~/anaconda3/lib/python3.6/site-packages/botocore/client.py in _api_call(self, *args, **kwargs) 312 "%s() only accepts keyword arguments." % py_operation_name) 313 # The "self" in this scope is referring to the BaseClient. --> 314 return self._make_api_call(operation_name, kwargs) 315 316 _api_call.__name__ = str(py_operation_name) ~/anaconda3/lib/python3.6/site-packages/botocore/client.py in _make_api_call(self, operation_name, api_params) 610 error_code = parsed_response.get("Error", {}).get("Code") 611 error_class = self.exceptions.from_code(error_code) --> 612 raise error_class(parsed_response, operation_name) 613 else: 614 return parsed_response ClientError: An error occurred (InvalidParameterValue) when calling the CreateVolume operation: 'null' is not a valid taggable resource type for this operation.
_ = attach_volume(instance, volume_tag, device='/dev/xvdf')
efs_tag = f'{name}-efs'
efs = create_efs(efs_tag, vpc)
client = connect_to_instance(instance)
Connecting to SSH... Connected!
upload_path = Path.cwd()/'upload_scripts/mount_ebs.sh'
upload_file(client, str(upload_path), 'mount_ebs.sh')
('', '')
out, _ = run_command(client, 'chmod 755 mount_ebs.sh')
out, _ = run_command(client, './mount_ebs.sh --device /dev/xvdf') # no reformatting
# out, _ = run_command(client, './mount_ebs.sh --device /dev/xvdf --reformat true')
efs_addr = get_efs_address('fast-ai-efs'); efs_addr
'fs-080292a1.efs.us-west-2.amazonaws.com'
out, _ = run_command(client, 'mkdir ~/efs_mount')
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'
out, _ = run_command(client, efs_mount_cmd)
out
''
client.close()
keypath=f'{Path.home()}/.ssh/aws-key-fast-ai.pem'
username='ubuntu'
f'ssh -i {keypath} {username}@{instance.public_ip_address}'
'ssh -i /home/jhoward/.ssh/aws-key-fast-ai.pem ubuntu@35.162.88.124'