Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add doc and sample dag for EC2 #23547

Merged
merged 1 commit into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions airflow/providers/amazon/aws/example_dags/example_ec2.py
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
8 changes: 8 additions & 0 deletions airflow/providers/amazon/aws/operators/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions airflow/providers/amazon/aws/sensors/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions airflow/providers/amazon/provider.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
83 changes: 83 additions & 0 deletions docs/apache-airflow-providers-amazon/operators/ec2.rst
Original file line number Diff line number Diff line change
@@ -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>`__
7 changes: 5 additions & 2 deletions docs/apache-airflow-providers-amazon/operators/ecs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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]

Expand Down
1 change: 1 addition & 0 deletions docs/spelling_wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,7 @@ reqs
reserialize
reserialized
resetdb
resizable
resourceVersion
resultset
resumable
Expand Down