pycloudlib.ec2.cloud module

AWS EC2 Cloud type.

class pycloudlib.ec2.cloud.EC2(tag, timestamp_suffix=True, config_file: Union[pathlib.Path, _io.StringIO] = None, *, access_key_id=None, secret_access_key=None, region=None)[source]

Bases: pycloudlib.cloud.BaseCloud

EC2 Cloud Class.

__init__(tag, timestamp_suffix=True, config_file: Union[pathlib.Path, _io.StringIO] = None, *, access_key_id=None, secret_access_key=None, region=None)[source]

Initialize the connection to EC2.

boto3 will read a users /home/$USER/.aws/* files if no arguments are provided here to find values.

Parameters:
  • tag – string used to name and tag resources with
  • timestamp_suffix – bool set True to append a timestamp suffix to the tag
  • config_file – path to pycloudlib configuration file
  • access_key_id – user’s access key ID
  • secret_access_key – user’s secret access key
  • region – region to login to
create_key_pair()

Create and set a ssh key pair for a cloud instance.

Returns:A tuple containing the public and private key created
daily_image(release, arch='amd64', root_store='ssd')[source]

Find the id of the latest daily image for a particular release.

Parameters:
  • release – string, Ubuntu release to look for
  • arch – string, architecture to use
  • root_store – string, root store to use
Returns:

string, id of latest image

delete_image(image_id)[source]

Delete an image.

Parameters:image_id – string, id of the image to delete
delete_key(name)[source]

Delete an uploaded key.

Parameters:name – The key name to delete.
get_instance(instance_id)[source]

Get an instance by id.

Parameters:instance_id
Returns:An instance object to use to manipulate the instance further.
get_or_create_vpc(name, ipv4_cidr='192.168.1.0/20')[source]

Create a or return matching VPC.

This can be used instead of using the default VPC to create a custom VPC for usage.

Parameters:
  • name – name of the VPC
  • ipv4_cidr – CIDR of IPV4 subnet
Returns:

VPC object

image_serial(image_id)[source]

Find the image serial of a given EC2 image ID.

Parameters:image_id – string, Ubuntu image id
Returns:string, serial of latest image
launch(image_id, instance_type='t2.micro', user_data=None, wait=True, vpc=None, **kwargs)[source]

Launch instance on EC2.

Parameters:
  • image_id – string, AMI ID to use default: latest Ubuntu LTS
  • instance_type – string, instance type to launch
  • user_data – string, user-data to pass to instance
  • wait – boolean, wait for instance to come up
  • vpc – optional vpc object to create instance under
  • kwargs – other named arguments to add to instance JSON
Returns:

EC2 Instance object

list_keys()[source]

List all ssh key pair names loaded on this EC2 region.

released_image(release, arch='amd64', root_store='ssd')[source]

Find the id of the latest released image for a particular release.

Parameters:
  • release – string, Ubuntu release to look for
  • arch – string, architecture to use
  • root_store – string, root store to use
Returns:

string, id of latest image

snapshot(instance, clean=True)[source]

Snapshot an instance and generate an image from it.

Parameters:
  • instance – Instance to snapshot
  • clean – run instance clean method before taking snapshot
Returns:

An image id

upload_key(public_key_path, private_key_path=None, name=None)[source]

Use an existing already uploaded key.

Parameters:
  • public_key_path – path to the public key to upload
  • private_key_path – path to the private key to upload
  • name – name to reference key by
use_key(public_key_path, private_key_path=None, name=None)[source]

Use an existing already uploaded key.

Parameters:
  • public_key_path – path to the public key to upload
  • private_key_path – path to the private key to upload
  • name – name to reference key by