pycloudlib.ec2.cloud module

AWS EC2 Cloud type.

class pycloudlib.ec2.cloud.EC2(tag: str, timestamp_suffix: bool = True, config_file: Path | StringIO | None = None, *, access_key_id: str | None = None, secret_access_key: str | None = None, region: str | None = None, profile: str | None = None)[source]

Bases: BaseCloud

EC2 Cloud Class.

__init__(tag: str, timestamp_suffix: bool = True, config_file: Path | StringIO | None = None, *, access_key_id: str | None = None, secret_access_key: str | None = None, region: str | None = None, profile: str | None = 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

  • profile – profile to use from ~/.aws/config

clean() List[Exception][source]

Cleanup ALL artifacts associated with this Cloud instance.

Cleanup any cloud artifacts created at any time during this class’s existence. This includes all instances, snapshots, resources, etc.

daily_image(release: str, *, arch: str = 'x86_64', image_type: ImageType = ImageType.GENERIC, include_deprecated: bool = False, **kwargs)[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

Returns:

string, id of latest image

delete_image(image_id, **kwargs)[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, *, username: str | None = None, **kwargs)[source]

Get an instance by id.

Parameters:
  • instance_id – ID used to identify the instance

  • username – username to use when connecting via SSH

  • **kwargs – dictionary of other arguments to be used by this method. Currently unused but provided for base class compatibility.

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, image_type: ImageType = ImageType.GENERIC)[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='t3.micro', user_data=None, vpc=None, *, disk_size_gb: int = 15, username: str | None = None, enable_ipv6: bool = False, **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

  • vpc – optional vpc object to create instance under

  • disk_size_gb – size of instance disk in GB

  • username – username to use when connecting via SSH

  • enable_ipv6 – indicates if ipv6 IMDS support must be added to the instance

  • kwargs – other named arguments to add to instance JSON

Returns:

EC2 Instance object

Raises: ValueError on invalid image_id

list_keys()[source]

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

released_image(release: str, *, arch: str = 'x86_64', image_type: ImageType = ImageType.GENERIC, include_deprecated: bool = False, **kwargs)[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