ec2#

Abstract dataclass for EC2 instance.

simple_aws_ec2.ec2.get_response(url: str) str[source]#

Get the text response from the url.

class simple_aws_ec2.ec2.EC2InstanceStatusEnum(value)[source]#

EC2 instance status enumerations.

See also: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/monitoring-instance-state-changes.html

class simple_aws_ec2.ec2.EC2InstanceStatusGroupEnum[source]#

Aggregate EC2 instance status into logical groups.

Variables:
  • ended – status won’t change anymore

  • in_transition – status is in transition

class simple_aws_ec2.ec2.EC2InstanceArchitectureEnum(value)[source]#

Ec2 instance architecture enumerations.

class simple_aws_ec2.ec2.Ec2InstanceHypervisorEnum(value)[source]#

Ec2 instance hypervisor enumerations.

class simple_aws_ec2.ec2.Ec2Instance(id: str, status: str, status_transition_reason: ~typing.Optional[str] = None, public_ip: ~typing.Optional[str] = None, private_ip: ~typing.Optional[str] = None, public_dns_name: ~typing.Optional[str] = None, private_dns_name: ~typing.Optional[str] = None, vpc_id: ~typing.Optional[str] = None, subnet_id: ~typing.Optional[str] = None, security_groups: ~typing.List[~typing.Dict[str, str]] = <factory>, image_id: ~typing.Optional[str] = None, platform: ~typing.Optional[str] = None, platform_details: ~typing.Optional[str] = None, instance_type: ~typing.Optional[str] = None, launch_time: ~typing.Optional[~datetime.datetime] = None, key_name: ~typing.Optional[str] = None, architecture: ~typing.Optional[str] = None, ebs_optimized: ~typing.Optional[bool] = None, ena_support: ~typing.Optional[bool] = None, hypervisor: ~typing.Optional[str] = None, iam_instance_profile_arn: ~typing.Optional[str] = None, iam_instance_profile_id: ~typing.Optional[str] = None, instance_lifecycle: ~typing.Optional[str] = None, root_device_name: ~typing.Optional[str] = None, root_device_type: ~typing.Optional[str] = None, spot_instance_request_id: ~typing.Optional[str] = None, sriov_net_support: ~typing.Optional[str] = None, virtualization_type: ~typing.Optional[str] = None, boot_mode: ~typing.Optional[str] = None, usage_operation: ~typing.Optional[str] = None, usage_operation_update_time: ~typing.Optional[~datetime.datetime] = None, current_instance_boot_mode: ~typing.Optional[str] = None, ipv6_address: ~typing.Optional[str] = None, tags: ~typing.Dict[str, str] = <factory>, data: ~typing.Dict[str, ~typing.Any] = <factory>)[source]#

Represent an EC2 instance.

classmethod from_dict(dct: dict)[source]#

Create an EC2 instance object from the describe_instances API response.

Ref:

is_pending() bool[source]#

Check if EC2 instance is pending.

is_running() bool[source]#

Check if EC2 instance is running.

is_shutting_down() bool[source]#

Check if EC2 instance is shutting down.

is_terminated() bool[source]#

Check if EC2 instance is terminated.

is_stopping() bool[source]#

Check if EC2 instance is stopping.

is_stopped() bool[source]#

Check if EC2 instance is stopped.

is_ready_to_stop() bool[source]#

Check if EC2 instance is ready to stop.

is_ready_to_start() bool[source]#

Check if EC2 instance is ready to start.

start_instance(ec2_client: EC2Client) StartInstancesResultTypeDef[source]#

Start instance.

stop_instance(ec2_client: EC2Client) StopInstancesResultTypeDef[source]#

Stop instance.

terminate_instance(ec2_client: EC2Client) TerminateInstancesResultTypeDef[source]#

Terminate instance.

wait_for_status(ec2_client: EC2Client, stop_status: Union[EC2InstanceStatusEnum, List[EC2InstanceStatusEnum]], gap: Union[int, float] = 1, delays: Union[int, float] = 10, timeout: Union[int, float] = 300, instant: bool = True, error_status: Optional[Union[EC2InstanceStatusEnum, List[EC2InstanceStatusEnum]]] = None, indent: int = 0, verbose: bool = True) Ec2Instance[source]#

wait until the EC2 instance reaches the specified status defined in stop_status. If reaches any of ``error_status ``, raise error.

Parameters:
  • ec2_client

  • stop_status – status to stop waiting

  • gap – the time to wait before making first status check

  • delays – delay between each check

  • timeout – timeout in seconds

  • instant – if True, then the first check is instant

  • error_status – status to raise error

  • indent – indent level for logging

  • verbose – whether to print log

Returns:

the Ec2Instance representing the latest status.

wait_for_running(ec2_client: EC2Client, gap: Union[int, float] = 1, delays: Union[int, float] = 10, timeout: Union[int, float] = 300, instant: bool = True, indent: int = 0, verbose: bool = True) Ec2Instance[source]#

Similar to Ec2Instance.wait_for_status(), but wait for EC2 instance to reach “running” status.

wait_for_stopped(ec2_client: EC2Client, gap: Union[int, float] = 1, delays: Union[int, float] = 10, timeout: Union[int, float] = 300, instant: bool = True, indent: int = 0, verbose: bool = True) Ec2Instance[source]#

Similar to Ec2Instance.wait_for_status(), but wait for EC2 instance to reach “stopped” status.

wait_for_terminated(ec2_client: EC2Client, gap: Union[int, float] = 1, delays: Union[int, float] = 10, timeout: Union[int, float] = 300, instant: bool = True, indent: int = 0, verbose: bool = True) Ec2Instance[source]#

Similar to Ec2Instance.wait_for_status(), but wait for EC2 instance to reach “terminated” status.

classmethod query(ec2_client: EC2Client, filters: List[dict] = Sentinel('NOTHING'), instance_ids: List[str] = Sentinel('NOTHING')) Ec2InstanceIterProxy[source]#

A wrapper around ec2_client.describe_instances.

Multiple filters join with logic “AND”, multiple values in a filter join with logic “OR”.

classmethod from_id(ec2_client: EC2Client, inst_id: str) Optional[Ec2Instance][source]#

Get ec2 instance details by it’s id.

classmethod from_ec2_inside(ec2_client: EC2Client) Optional[Ec2Instance][source]#

Use ec2 metadata API to get the instance id.

Note

This function should only be called on an EC2 instance

classmethod from_tag_key_value(ec2_client: EC2Client, key: str, value: Union[str, Iterable[str]]) Ec2InstanceIterProxy[source]#

Query EC2 Instance by tag key and values.

Parameters:
  • key – tag key

  • value – tag value or values

classmethod from_ec2_name(ec2_client: EC2Client, name: Union[str, Iterable[str]]) Ec2InstanceIterProxy[source]#

Get EC2 instance details by the tag:name.

classmethod get_ami_id() str[source]#

Get the AMI id of the EC2 instance. This method should only be used within EC2 instance.

classmethod get_instance_id() str[source]#

Get the instance id of the EC2 instance. This method should only be used within EC2 instance.

classmethod get_instance_type() str[source]#

Get the instance type of the EC2 instance. This method should only be used within EC2 instance.

classmethod get_hostname() str[source]#

Get the hostname of the EC2 instance. This method should only be used within EC2 instance.

classmethod get_local_hostname() str[source]#

Get the local hostname of the EC2 instance. This method should only be used within EC2 instance.

classmethod get_local_ipv4() str[source]#

Get the local ipv4 of the EC2 instance. This method should only be used within EC2 instance.

classmethod get_public_hostname() str[source]#

Get the public hostname of the EC2 instance. This method should only be used within EC2 instance.

classmethod get_public_ipv4() str[source]#

Get the public ipv4 of the EC2 instance. This method should only be used within EC2 instance.

classmethod get_security_groups() List[str][source]#

Get the security groups of the EC2 instance. This method should only be used within EC2 instance.

classmethod get_iam_info() Dict[str, str][source]#

Get the IAM info of the EC2 instance. This method should only be used within EC2 instance.

Example response:

{
    "Code" : "Success",
    "LastUpdated" : "2023-01-01T00:00:00Z",
    "InstanceProfileId" : "ABCD..."
    "InstanceProfileArn" : "arn:aws:iam::111122223333:instance-profile/profile-name",
}
classmethod get_placement_region() str[source]#

Get the placement region of the EC2 instance. This method should only be used within EC2 instance.

classmethod get_reservation_id() str[source]#

Get the reservation id of the EC2 instance. This method should only be used within EC2 instance.

class simple_aws_ec2.ec2.Ec2InstanceIterProxy(iterable: Iterable)[source]#

Advanced iterator proxy for Ec2Instance.

class simple_aws_ec2.ec2.ImageTypeEnum(value)[source]#

AMI Image type enumerations.

class simple_aws_ec2.ec2.ImageStateEnum(value)[source]#

AMI Image state enumerations.

class simple_aws_ec2.ec2.ImageRootDeviceTypeEnum(value)[source]#

AMI Image root device type enumerations.

class simple_aws_ec2.ec2.ImageVirtualizationTypeEnum(value)[source]#

AMI Image virtualization type enumerations.

class simple_aws_ec2.ec2.ImageBootModeEnum(value)[source]#

AMI Image boot mode enumerations.

class simple_aws_ec2.ec2.ImageOwnerGroupEnum(value)[source]#

AMI Image owner group enumerations.

class simple_aws_ec2.ec2.Image(id: str, image_location: ~typing.Optional[str] = None, image_type: ~typing.Optional[str] = None, architecture: ~typing.Optional[str] = None, creation_date: ~typing.Optional[str] = None, public: ~typing.Optional[bool] = None, kernel_id: ~typing.Optional[str] = None, owner_id: ~typing.Optional[str] = None, platform: ~typing.Optional[str] = None, platform_details: ~typing.Optional[str] = None, usage_operation: ~typing.Optional[str] = None, ramdisk_id: ~typing.Optional[str] = None, state: ~typing.Optional[str] = None, state_reason_code: ~typing.Optional[str] = None, state_reason_message: ~typing.Optional[str] = None, description: ~typing.Optional[str] = None, ena_support: ~typing.Optional[bool] = None, hypervisor: ~typing.Optional[str] = None, image_owner_alias: ~typing.Optional[str] = None, name: ~typing.Optional[str] = None, root_device_name: ~typing.Optional[str] = None, root_device_type: ~typing.Optional[str] = None, sriov_net_support: ~typing.Optional[str] = None, virtualization_type: ~typing.Optional[str] = None, boot_mode: ~typing.Optional[str] = None, tpm_support: ~typing.Optional[str] = None, deprecation_time: ~typing.Optional[str] = None, imds_support: ~typing.Optional[str] = None, tags: ~typing.Dict[str, str] = <factory>, data: ~typing.Dict[str, ~typing.Any] = <factory>)[source]#

Represent an AMI image.

classmethod from_dict(dct: dict)[source]#

Create an AMI Image object from the describe_images API response.

Ref:

image_type_is_machine() bool[source]#

Check if the image type is machine.

image_type_is_kernel() bool[source]#

Check if the image type is kernel.

image_type_is_ramdisk() bool[source]#

Check if the image type is ramdisk.

is_pending() bool[source]#

Check if the image status is pending.

is_available() bool[source]#

Check if the image status is available.

is_invalid() bool[source]#

Check if the image status is invalid.

is_deregistered() bool[source]#

Check if the image status is deregistered.

is_transient() bool[source]#

Check if the image status is transient.

is_failed() bool[source]#

Check if the image status is failed.

is_error() bool[source]#

Check if the image status is error.

is_disabled() bool[source]#

Check if the image status is disabled.

image_root_device_type_is_ebs() bool[source]#

Check if the image root device type is ebs.

image_root_device_type_is_instance_store() bool[source]#

Check if the image root device type is instance store.

image_virtualization_type_is_hvm() bool[source]#

Check if the image virtualization type is hvm.

image_virtualization_type_is_paravirtual() bool[source]#

Check if the image virtualization type is paravirtual.

image_boot_mode_is_legacy_bios() bool[source]#

Check if the image boot mode is legacy bios.

image_boot_mode_is_uefi() bool[source]#

Check if the image boot mode is uefi.

image_boot_mode_is_uefi_preferred() bool[source]#

Check if the image boot mode is uefi preferred.

property os_type: ImageOSTypeEnum#

Try to use the image name and description to determine the OS type.

If the OS type cannot be determined, raise CannotDetectOSTypeError.

is_amazon_linux_os() bool[source]#

Check if the image OS is Amazon Linux.

is_centos_os() bool[source]#

Check if the image OS is CentOS.

is_cent_os_os() bool#

Check if the image OS is CentOS.

is_debian_os() bool[source]#

Check if the image OS is Debian.

is_fedora_os() bool[source]#

Check if the image OS is Fedora.

is_rhel_os() bool[source]#

Check if the image OS is RHEL.

is_suse_os() bool[source]#

Check if the image OS is SUSE.

is_ubuntu_os() bool[source]#

Check if the image OS is Ubuntu.

is_oracle_os() bool[source]#

Check if the image OS is Oracle.

is_bitnami_os() bool[source]#

Check if the image OS is Bitnami.

is_other_os() bool[source]#

Check if the image OS is other.

property users: List[str]#

Return the potential default user names for the Image. It try to use the image name and description to determine the OS type. If the OS type cannot be determined, raise CannotDetectOSTypeError.

property ebs_snapshot_id_list: List[str]#

Get the list of snapshot ids associated with the AMI.

classmethod query(ec2_client: EC2Client, filters: List[dict] = Sentinel('NOTHING'), image_ids: List[str] = Sentinel('NOTHING'), executable_users: List[str] = Sentinel('NOTHING'), owners: List[str] = Sentinel('NOTHING'), include_deprecated: bool = Sentinel('NOTHING')) ImageIterProxy[source]#

A wrapper around ec2_client.describe_images.

Multiple filters join with logic “AND”, multiple values in a filter join with logic “OR”.

classmethod from_id(ec2_client: EC2Client, image_id: str) Optional[Image][source]#

Get Image object by the image id.

classmethod from_tag_key_value(ec2_client: EC2Client, key: str, value: Union[str, Iterable[str]]) ImageIterProxy[source]#

Query AMI Image by tag key and values.

Parameters:
  • key – tag key

  • value – tag value or values

classmethod from_image_name(ec2_client: EC2Client, name: Union[str, Iterable[str]]) ImageIterProxy[source]#

Get image details by the name of the AMI (provided during image creation). This name is not the tag:name

classmethod from_ec2_inside(ec2_client: EC2Client) Optional[Image][source]#

Use ec2 metadata API to get the instance id, then get the image details

Note

This function should only be called on an EC2 instance

deregister(ec2_client: EC2Client, delete_snapshot: bool = False, skip_prompt: bool = False, verbose: bool = False)[source]#

Deregister this image.

Parameters:
  • delete_snapshot – if True, also delete the snapshot.

  • skip_prompt – by default, it prompts to confirm. You can set it to True, to skip the prompt.

  • verbose – whether to print log

wait_for_status(ec2_client: EC2Client, stop_status: Union[ImageStateEnum, List[ImageStateEnum]], gap: Union[int, float] = 1, delays: Union[int, float] = 10, timeout: Union[int, float] = 300, instant: bool = True, error_status: Optional[Union[ImageStateEnum, List[ImageStateEnum]]] = None, indent: int = 0, verbose: bool = True) Image[source]#

wait until the AMI Image reaches the specified status defined in stop_status. If reaches any of ``error_status ``, raise error.

Parameters:
  • ec2_client

  • stop_status – status to stop waiting

  • gap – the time to wait before making first status check

  • delays – delay between each check

  • timeout – timeout in seconds

  • instant – if True, then the first check is instant

  • error_status – status to raise error

  • indent – indent level for logging

  • verbose – whether to print log

Returns:

the Image representing the latest status.

wait_for_available(ec2_client: EC2Client, gap: Union[int, float] = 1, delays: Union[int, float] = 10, timeout: Union[int, float] = 300, instant: bool = True, indent: int = 0, verbose: bool = True) Image[source]#

Similar to Image.wait_for_status(), but wait for AMI to reach “available” status.

wait_for_deregistered(ec2_client: EC2Client, gap: Union[int, float] = 1, delays: Union[int, float] = 10, timeout: Union[int, float] = 300, instant: bool = True, indent: int = 0, verbose: bool = True) Image[source]#

Similar to Image.wait_for_status(), but wait for AMI to reach “deregistered” status.

class simple_aws_ec2.ec2.ImageIterProxy(iterable: Iterable)[source]#

Advanced iterator proxy for Image.

class simple_aws_ec2.ec2.Eip(allocation_id: ~typing.Optional[str] = None, public_ip: ~typing.Optional[str] = None, association_id: ~typing.Optional[str] = None, instance_id: ~typing.Optional[str] = None, domain: ~typing.Optional[str] = None, network_interface_id: ~typing.Optional[str] = None, network_interface_owner_id: ~typing.Optional[str] = None, private_ip_address: ~typing.Optional[str] = None, public_ipv4_pool: ~typing.Optional[str] = None, network_border_group: ~typing.Optional[str] = None, customer_owned_ip: ~typing.Optional[str] = None, customer_owned_ipv4_pool: ~typing.Optional[str] = None, carrier_ip: ~typing.Optional[str] = None, tags: ~typing.Dict[str, str] = <factory>, data: ~typing.Dict[str, ~typing.Any] = <factory>)[source]#

Represent an Elastic IP Address.

classmethod from_dict(dct: dict)[source]#

Create an AMI Image object from the describe_images API response.

Ref:

is_associated() bool[source]#

Check if the EIP is associated with an instance.

classmethod query(ec2_client: EC2Client, filters: List[dict] = Sentinel('NOTHING'), allocation_ids: List[str] = Sentinel('NOTHING'), public_ips: List[str] = Sentinel('NOTHING')) EipIterProxy[source]#

A wrapper around ec2_client.describe_addresses.

Multiple filters join with logic “AND”, multiple values in a filter join with logic “OR”.

classmethod from_id(ec2_client: EC2Client, allocation_id: str) Optional[Eip][source]#

Get Eip object by the allocation id.

classmethod from_public_ip(ec2_client: EC2Client, public_ip: str) Optional[Eip][source]#

Get Eip object by the public ip.

class simple_aws_ec2.ec2.EipIterProxy(iterable: Iterable)[source]#

Advanced iterator proxy for Eip.