Skip to content
Permalink
Browse files
Add doc and sample dag for EC2 (#23547)
  • Loading branch information
vincbeck committed May 9, 2022
1 parent b7f8627 commit 389676bb0dba7c1e191f61992f244a03393c6922
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 3 deletions.
@@ -0,0 +1,56 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import os
from datetime import datetime

from airflow import DAG
from airflow.models.baseoperator import chain
from airflow.providers.amazon.aws.operators.ec2 import EC2StartInstanceOperator, EC2StopInstanceOperator
from airflow.providers.amazon.aws.sensors.ec2 import EC2InstanceStateSensor

INSTANCE_ID = os.getenv("INSTANCE_ID", "instance-id")

with DAG(
dag_id='example_ec2',
schedule_interval=None,
start_date=datetime(2021, 1, 1),
tags=['example'],
catchup=False,
) as dag:
# [START howto_operator_ec2_start_instance]
start_instance = EC2StartInstanceOperator(
task_id="ec2_start_instance",
instance_id=INSTANCE_ID,
)
# [END howto_operator_ec2_start_instance]

# [START howto_sensor_ec2_instance_state]
instance_state = EC2InstanceStateSensor(
task_id="ec2_instance_state",
instance_id=INSTANCE_ID,
target_state="running",
)
# [END howto_sensor_ec2_instance_state]

# [START howto_operator_ec2_stop_instance]
stop_instance = EC2StopInstanceOperator(
task_id="ec2_stop_instance",
instance_id=INSTANCE_ID,
)
# [END howto_operator_ec2_stop_instance]

chain(start_instance, instance_state, stop_instance)
@@ -21,7 +21,7 @@
from airflow.providers.amazon.aws.operators.ecs import EcsOperator

with DAG(
dag_id='example_ecs_ec2',
dag_id='example_ecs',
schedule_interval=None,
start_date=datetime(2021, 1, 1),
tags=['example'],
@@ -30,6 +30,10 @@ class EC2StartInstanceOperator(BaseOperator):
"""
Start AWS EC2 instance using boto3.
.. seealso::
For more information on how to use this operator, take a look at the guide:
:ref:`howto/operator:EC2StartInstanceOperator`
:param instance_id: id of the AWS EC2 instance
:param aws_conn_id: aws connection to use
:param region_name: (optional) aws region name associated with the client
@@ -72,6 +76,10 @@ class EC2StopInstanceOperator(BaseOperator):
"""
Stop AWS EC2 instance using boto3.
.. seealso::
For more information on how to use this operator, take a look at the guide:
:ref:`howto/operator:EC2StopInstanceOperator`
:param instance_id: id of the AWS EC2 instance
:param aws_conn_id: aws connection to use
:param region_name: (optional) aws region name associated with the client
@@ -31,6 +31,10 @@ class EC2InstanceStateSensor(BaseSensorOperator):
Check the state of the AWS EC2 instance until
state of the instance become equal to the target state.
.. seealso::
For more information on how to use this sensor, take a look at the guide:
:ref:`howto/sensor:EC2InstanceStateSensor`
:param target_state: target state of instance
:param instance_id: id of the AWS EC2 instance
:param region_name: (optional) aws region name associated with the client
@@ -71,6 +71,8 @@ integrations:
- integration-name: Amazon EC2
external-doc-url: https://aws.amazon.com/ec2/
logo: /integration-logos/aws/Amazon-EC2_light-bg@4x.png
how-to-guide:
- /docs/apache-airflow-providers-amazon/operators/ec2.rst
tags: [aws]
- integration-name: Amazon ECS
external-doc-url: https://aws.amazon.com/ecs/
@@ -0,0 +1,83 @@
.. Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
.. http://www.apache.org/licenses/LICENSE-2.0
.. Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
=========================================
Amazon Amazon Elastic Compute Cloud (EC2)
=========================================

`Amazon Elastic Compute Cloud (Amazon EC2) <https://aws.amazon.com/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
------------------

.. include:: _partials/prerequisite_tasks.rst

.. _howto/operator:EC2StartInstanceOperator:

Operators
---------

Start an Amazon EC2 instance
============================

To start an Amazon EC2 instance you can use
:class:`~airflow.providers.amazon.aws.operators.ec2.EC2StartInstanceOperator`.

.. exampleinclude:: /../../airflow/providers/amazon/aws/example_dags/example_ec2.py
:language: python
:dedent: 4
:start-after: [START howto_operator_ec2_start_instance]
:end-before: [END howto_operator_ec2_start_instance]

.. _howto/operator:EC2StopInstanceOperator:

Stop an Amazon EC2 instance
===========================

To stop an Amazon EC2 instance you can use
:class:`~airflow.providers.amazon.aws.operators.ec2.EC2StopInstanceOperator`.

.. exampleinclude:: /../../airflow/providers/amazon/aws/example_dags/example_ec2.py
:language: python
:dedent: 4
:start-after: [START howto_operator_ec2_stop_instance]
:end-before: [END howto_operator_ec2_stop_instance]

Sensors
-------

.. _howto/sensor:EC2InstanceStateSensor:

Amazon EC2 instance state sensor
================================

To check the state of an Amazon EC2 instance and wait until it reaches the target state you can use
:class:`~airflow.providers.amazon.aws.sensors.ec2.EC2InstanceStateSensor`.

.. exampleinclude:: /../../airflow/providers/amazon/aws/example_dags/example_ec2.py
:language: python
:dedent: 4
:start-after: [START howto_sensor_ec2_instance_state]
:end-before: [END howto_sensor_ec2_instance_state]

Reference
---------

For further information, look at:

* `Boto3 Library Documentation for EC2 <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html>`__
@@ -54,14 +54,16 @@ Launch Types
* If you are using EC2 as the compute resources in your ECS Cluster, set the parameter to EC2.
* If you have integrated external resources in your ECS Cluster, for example using ECS Anywhere, and want to run your containers on those external resources, set the parameter to EXTERNAL.

.. exampleinclude:: /../../airflow/providers/amazon/aws/example_dags/example_ecs_ec2.py
.. exampleinclude:: /../../airflow/providers/amazon/aws/example_dags/example_ecs.py
:language: python
:dedent: 4
:start-after: [START howto_operator_ecs]
:end-before: [END howto_operator_ecs]


.. exampleinclude:: /../../airflow/providers/amazon/aws/example_dags/example_ecs_fargate.py
:language: python
:dedent: 4
:start-after: [START howto_operator_ecs]
:end-before: [END howto_operator_ecs]

@@ -71,8 +73,9 @@ CloudWatch Logging

To stream logs to AWS CloudWatch, you need to define these parameters. Using the example Operators above, we would add these additional parameters to enable logging to CloudWatch. You will need to ensure that you have the appropriate level of permissions (see next section)

.. exampleinclude:: /../../airflow/providers/amazon/aws/example_dags/example_ecs_ec2.py
.. exampleinclude:: /../../airflow/providers/amazon/aws/example_dags/example_ecs.py
:language: python
:dedent: 4
:start-after: [START howto_awslogs_ecs]
:end-before: [END howto_awslogs_ecs]

@@ -1265,6 +1265,7 @@ reqs
reserialize
reserialized
resetdb
resizable
resourceVersion
resultset
resumable

0 comments on commit 389676b

Please sign in to comment.