Amazon Elastic Compute Cloud (EC2)

Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides resizable computing capacity—literally, servers in Amazon’s data centers—that you use to build and host your software systems.

Prerequisite Tasks

To use these operators, you must do a few things:

Generic Parameters

aws_conn_id

Reference to Amazon Web Services Connection ID. If this parameter is set to None then the default boto3 behaviour is used without a connection lookup. Otherwise use the credentials stored in the Connection. Default: aws_default

region_name

AWS Region Name. If this parameter is set to None or omitted then region_name from AWS Connection Extra Parameter will be used. Otherwise use the specified value instead of the connection value. Default: None

verify

Whether or not to verify SSL certificates.

  • False - Do not validate SSL certificates.

  • path/to/cert/bundle.pem - A filename of the CA cert bundle to use. You can specify this argument if you want to use a different CA cert bundle than the one used by botocore.

If this parameter is set to None or is omitted then verify from AWS Connection Extra Parameter will be used. Otherwise use the specified value instead of the connection value. Default: None

botocore_config

The provided dictionary is used to construct a botocore.config.Config. This configuration can be used to configure Avoid Throttling exceptions, timeouts, etc.

Example, for more detail about parameters please have a look botocore.config.Config
{
    "signature_version": "unsigned",
    "s3": {
        "us_east_1_regional_endpoint": True,
    },
    "retries": {
      "mode": "standard",
      "max_attempts": 10,
    },
    "connect_timeout": 300,
    "read_timeout": 300,
    "tcp_keepalive": True,
}

If this parameter is set to None or omitted then config_kwargs from AWS Connection Extra Parameter will be used. Otherwise use the specified value instead of the connection value. Default: None

Note

Specifying an empty dictionary, {}, will overwrite the connection configuration for botocore.config.Config

Operators

Start an Amazon EC2 instance

To start an Amazon EC2 instance you can use EC2StartInstanceOperator.

tests/system/amazon/aws/example_ec2.py[source]

start_instance = EC2StartInstanceOperator(
    task_id="start_instance",
    instance_id=instance_id,
)

Stop an Amazon EC2 instance

To stop an Amazon EC2 instance you can use EC2StopInstanceOperator.

tests/system/amazon/aws/example_ec2.py[source]

stop_instance = EC2StopInstanceOperator(
    task_id="stop_instance",
    instance_id=instance_id,
)

Create and start an Amazon EC2 instance

To create and start an Amazon EC2 instance you can use EC2CreateInstanceOperator.

tests/system/amazon/aws/example_ec2.py[source]

create_instance = EC2CreateInstanceOperator(
    task_id="create_instance",
    image_id=image_id,
    max_count=1,
    min_count=1,
    config=config,
)

Terminate an Amazon EC2 instance

To terminate an Amazon EC2 instance you can use EC2TerminateInstanceOperator.

tests/system/amazon/aws/example_ec2.py[source]

terminate_instance = EC2TerminateInstanceOperator(
    task_id="terminate_instance",
    instance_ids=instance_id,
    wait_for_completion=True,
)

Reboot an Amazon EC2 instance

To reboot an Amazon EC2 instance you can use EC2RebootInstanceOperator.

tests/system/amazon/aws/example_ec2.py[source]

reboot_instance = EC2RebootInstanceOperator(
    task_id="reboot_instace",
    instance_ids=instance_id,
)

Hibernate an Amazon EC2 instance

To hibernate an Amazon EC2 instance you can use EC2HibernateInstanceOperator.

tests/system/amazon/aws/example_ec2.py[source]

hibernate_instance = EC2HibernateInstanceOperator(
    task_id="hibernate_instace",
    instance_ids=instance_id,
)

Sensors

Wait on an Amazon EC2 instance state

To check the state of an Amazon EC2 instance and wait until it reaches the target state you can use EC2InstanceStateSensor.

tests/system/amazon/aws/example_ec2.py[source]

await_instance = EC2InstanceStateSensor(
    task_id="await_instance",
    instance_id=instance_id,
    target_state="running",
)

Reference

Was this entry helpful?