diff --git a/airflow/providers/amazon/aws/example_dags/example_ec2.py b/airflow/providers/amazon/aws/example_dags/example_ec2.py new file mode 100644 index 0000000000000..7d12aef36746e --- /dev/null +++ b/airflow/providers/amazon/aws/example_dags/example_ec2.py @@ -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) diff --git a/airflow/providers/amazon/aws/example_dags/example_ecs_ec2.py b/airflow/providers/amazon/aws/example_dags/example_ecs.py similarity index 98% rename from airflow/providers/amazon/aws/example_dags/example_ecs_ec2.py rename to airflow/providers/amazon/aws/example_dags/example_ecs.py index 5e0c38651424d..b8f1f67b8eb08 100644 --- a/airflow/providers/amazon/aws/example_dags/example_ecs_ec2.py +++ b/airflow/providers/amazon/aws/example_dags/example_ecs.py @@ -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'], diff --git a/airflow/providers/amazon/aws/operators/ec2.py b/airflow/providers/amazon/aws/operators/ec2.py index 033760be5b560..133596929e423 100644 --- a/airflow/providers/amazon/aws/operators/ec2.py +++ b/airflow/providers/amazon/aws/operators/ec2.py @@ -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 diff --git a/airflow/providers/amazon/aws/sensors/ec2.py b/airflow/providers/amazon/aws/sensors/ec2.py index f955f713f1ab4..7d4a640593fec 100644 --- a/airflow/providers/amazon/aws/sensors/ec2.py +++ b/airflow/providers/amazon/aws/sensors/ec2.py @@ -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 diff --git a/airflow/providers/amazon/provider.yaml b/airflow/providers/amazon/provider.yaml index fe939a103eba9..709667a80a577 100644 --- a/airflow/providers/amazon/provider.yaml +++ b/airflow/providers/amazon/provider.yaml @@ -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/ diff --git a/docs/apache-airflow-providers-amazon/operators/ec2.rst b/docs/apache-airflow-providers-amazon/operators/ec2.rst new file mode 100644 index 0000000000000..5b25fdf866cd5 --- /dev/null +++ b/docs/apache-airflow-providers-amazon/operators/ec2.rst @@ -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) `__ 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 `__ diff --git a/docs/apache-airflow-providers-amazon/operators/ecs.rst b/docs/apache-airflow-providers-amazon/operators/ecs.rst index df991a4a411ac..e03f25954d584 100644 --- a/docs/apache-airflow-providers-amazon/operators/ecs.rst +++ b/docs/apache-airflow-providers-amazon/operators/ecs.rst @@ -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] diff --git a/docs/spelling_wordlist.txt b/docs/spelling_wordlist.txt index bb54cf2c2b032..ae46388bb019c 100644 --- a/docs/spelling_wordlist.txt +++ b/docs/spelling_wordlist.txt @@ -1264,6 +1264,7 @@ reqs reserialize reserialized resetdb +resizable resourceVersion resultset resumable