Source code for airflow.providers.amazon.aws.triggers.ec2
# 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.from__future__importannotationsimportasynciofromfunctoolsimportcached_propertyfromtypingimportAnyfromairflow.providers.amazon.aws.hooks.ec2importEC2Hookfromairflow.triggers.baseimportBaseTrigger,TriggerEvent
[docs]classEC2StateSensorTrigger(BaseTrigger):""" Poll the EC2 instance and yield a TriggerEvent once the state of the instance matches the target_state. :param instance_id: id of the AWS EC2 instance :param target_state: target state of instance :param aws_conn_id: The Airflow connection used for AWS credentials. If this is None or empty then the default boto3 behaviour is used. If running Airflow in a distributed manner and aws_conn_id is None or empty, then default boto3 configuration would be used (and must be maintained on each worker node). :param region_name: (optional) aws region name associated with the client :param poll_interval: number of seconds to wait before attempting the next poll """def__init__(self,instance_id:str,target_state:str,aws_conn_id:str|None="aws_default",region_name:str|None=None,poll_interval:int=60,):self.instance_id=instance_idself.target_state=target_stateself.aws_conn_id=aws_conn_idself.region_name=region_nameself.poll_interval=poll_interval
[docs]asyncdefrun(self):whileTrue:instance_state=awaitself.hook.get_instance_state_async(instance_id=self.instance_id)self.log.info("instance state: %s",instance_state)ifinstance_state==self.target_state:yieldTriggerEvent({"status":"success","message":"target state met"})breakelse:awaitasyncio.sleep(self.poll_interval)