%reload_ext autoreload
%autoreload 2
%matplotlib inline
from aws_setup import *
list(ec2.key_pairs.all())
[ec2.KeyPairInfo(name='AmazonEC2test'), ec2.KeyPairInfo(name='aws-key-fast-ai'), ec2.KeyPairInfo(name='aws-key-test-vpc'), ec2.KeyPairInfo(name='id_rsa'), ec2.KeyPairInfo(name='Mac')]
vpc_name='fast-ai'
instance_name = f'{vpc_name}-instance'
instance_type = 't2.medium'
vpc = get_vpc(vpc_name); vpc
ec2.Vpc(id='vpc-6e6b2a17')
spot_prices = get_spot_prices(); spot_prices[instance_type]
'0.031900'
launch_specs = LaunchSpecs(vpc, instance_type=instance_type, volume_size=80).build()
launch_specs
{'BlockDeviceMappings': [{'DeviceName': '/dev/sda1',
'Ebs': {'DeleteOnTermination': True,
'VolumeSize': 80,
'VolumeType': 'gp2'}}],
'ImageId': 'ami-8c4288f4',
'InstanceType': 't2.medium',
'KeyName': 'aws-key-fast-ai',
'NetworkInterfaces': [{'AssociatePublicIpAddress': True,
'DeviceIndex': 0,
'Groups': ['sg-f60fca88'],
'SubnetId': 'subnet-f056ff89'}]}
instance = create_spot_instance(instance_name, launch_specs, spot_price='0.5'); instance
Waiting on spot fullfillment... Fulfillment completed. InstanceId: i-07f29a1137e5bd67f Rebooting... Completed. SSH: ssh -i ~/.ssh/aws-key-fast-ai.pem ubuntu@34.217.9.117
ec2.Instance(id='i-07f29a1137e5bd67f')
Request on demand instance (if spot error)
# instance = create_instance(instance_name, vpc, instance_type='t2.micro'); instance
instance = get_instance(instance_name); instance
ec2.Instance(id='i-0976e3eb46f705bb0')
v1 = list(instance.volumes.all())[0]
v1.detach_from_instance()
--------------------------------------------------------------------------- ClientError Traceback (most recent call last) <ipython-input-79-b92755daa144> in <module>() ----> 1 v1.detach_from_instance() ~/miniconda3/envs/imagenet/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'): ~/miniconda3/envs/imagenet/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) ~/miniconda3/envs/imagenet/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) ~/miniconda3/envs/imagenet/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 (IncorrectState) when calling the DetachVolume operation: Volume 'vol-0ec18e0d7a4782ee8'is in the 'available' state.
v1.state
'in-use'
client = connect_to_instance(instance)
Connecting to SSH... Connected!
volume_tag = 'fast-ai-imagenet'
_ = attach_volume(instance, volume_tag, device='/dev/xvdf')
mount_volume(instance, device='/dev/xvdf', mount_dir='ebs_mount')
Connecting to SSH... Connected!
efs_addr = get_efs_address('fast-ai-efs'); efs_addr
'fs-0ea233a7.efs.us-west-2.amazonaws.com'
out, _ = run_command(client, 'mkdir ~/efs_mount_point')
run_command returned:
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_point'
out, _ = run_command(client, efs_mount_cmd)
run_command returned:
out, _ = run_command(client, 'ls efs_mount_point') # no reformatting
run_command returned: efs_saved.txt