This page hasn’t yet been updated for Airflow 3. The concepts shown are relevant, but some code may need to be updated. If you run any examples, take care to update import statements and watch for any other breaking changes.
- DAG
- Task
- Task Instance
- Pod
- Airflow Context variables
tags in your production Airflow environment.
Unlike Airflow Plugins, Cluster Policies aren’t visible in the Airflow UI. Since end users lack visibility into the installed Cluster Policies, Astronomer recommends implementing logging every time a policy modifies an Airflow object to inform users of the change.
- Enforce Task or DAG-level retries
- Verify a DAG’s
catchupparameter based on production or development environment - Limit the resources requested by a
KubernetesPodOperator - Routing critical jobs to a specific Celery
queueor Airflowpool - Add missing
tagsorowneremails
Assumed knowledge
To get the most out of this guide, you should have an understanding of:- Basic Airflow concepts. See Introduction to Apache Airflow.
- The Astro CLI. See Get started with Astro CLI
Types of cluster policies
You can use four types of cluster policies in Airflow:- DAG policy: This policy is applicable to a DAG object, and takes a DAG object
dagas a parameter. - Task policy: This policy is applicable to a Task object.
- Task Instance policy: This policy is applicable to a Task Instance, which is an instance of a Task object and is created at run time.
- Pod policy: This policy is applicable to a Kubernetes Pod launched by
KubernetesPodOperatororKubernetesExecutorat runtime.
How cluster policies work

AirflowClusterPolicyViolation exception and the DAG won’t be loaded. The Airflow web UI displays this exception as an import error.
You can also use the AirflowClusterPolicySkipDag exception to skip a DAG. For example, you may want to skip month-end DAGs from daily processing or skip any DAGs with the wrong environment tag. Another possible use case could be when you are migrating from a deprecated source system to a new source system. You might want to skip the old DAGs to avoid any failures and alerts. Note that this exception won’t be displayed on the Airflow web UI.
DAG policy
The DAG policy allows you to overwrite or reconfigure a DAG’s parameters based on the criteria you set. You can implement this usingdag_policy function. It runs at the time the DAG is loaded from the DagBag. It allows you to:
- Mutate a DAG object after it is loaded in the
DagBag. - Run code after your DAG has been fully generated.
- Enforcing a default owner for your DAGs.
- Enforcing certain tags for DAGs, either default or based on conditions.
- Ensuring development DAGs don’t run in production.
- Stopping a DAG from being executed by raising an
AirflowClusterPolicyViolationexception.
dag_policy is applied before the task_policy and after the DAG has been completely loaded. Hence, overriding the default_args parameter has no effect using dag_policy. If you want to override the default operator settings, use task policies instead.
Example
Task policy
A task policy allows you to overwrite or reconfigure a task’s parameters. You can implement this usingtask_policy function. It gets executed when the task is created during parsing of the task from DagBag at load time and mutates tasks after they have been added to a DAG. This means that the whole task definition can be altered in the task policy. It doesn’t relate to a specific task running in a DagRun. The task_policy defined is applied to all the task instances that will be executed in the future. It expects a BaseOperator as a parameter.
Some example implementations include:
- Enforcing a task timeout policy.
- Using a different environment for different operators.
- Overriding a
on_success_callbackoron_failure_callbackfor a task.
Example
Task Instance policy
If you are on Airflow version
2.9.1 or lower, you might see some inconsistencies in the application of task_instance_mutation_hook. This was fixed in Airflow 2.9.2.task_instance_mutation_hook. This is different from the task_policy function, which inspects and mutates tasks “as defined”. By contrast, task instance policies inspect and mutate task instances before execution. It takes a TaskInstance object, task_instance, as a parameter. This policy applies not to a task but to the instance of a task that relates to a particular DagRun. It is only applied to the currently executed run (in other words, instance) of that task. The policy is applied to a task instance in an Airflow worker before the task instance is executed, not in the DAG file processor.
Some example implementations include:
- Enforcing a specific queue for certain Airflow Operators.
- Modifying a task instance between retries.
Example
Pod policy
This policy is applicable to Kubernetes Pod created at runtime when using theKubernetesPodOperator or KubernetesExecutor. You can implement this policy using pod_mutation_hook function. This is a policy function that allows altering a kubernetes.client.models.V1Pod object before Airflow passes it to the Kubernetes client for scheduling. It takes a Pod object pod as a parameter. Note that this cluster policy is available only from Airflow version 2.6.
For instance, one could use this to alter the resources for a Pod or to add sidecar or init containers to every worker pod launched. Astro, however, doesn’t allow adding init or sidecar containers. Astro provides advanced logging, metrics collection, and multiple ways to manage your environment without the need to run separate containers to collect stats or apply environment settings.
Some example implementations include:
- Setting resource requests and limits.
- Increasing resources assigned to a Pod.
Example
Implementation
In this section, we describe how to usepluggy to implement cluster policies for an Airflow project using Astro CLI. pluggy is useful for plugin management, allowing you to have multiple implementations of the policy functions.
Note that the pluggy method is available only in Airflow version 2.6 and above. For versions lower than 2.6, a similar implementation is possible using the config/airflow_local_settings.py file in your $AIRFLOW_HOME. You can define your policies within this file. There is no need to build or install any package when you use the airflow_local_settings.py file. However, on Astro, you can only implement policies using the pluggy interface.
Step 1: Create a package for your policies
The simplest way to implement cluster policies is to build a package for them that you apply to your Airflow environment. You can add this package to theplugins folder of your Astro project and install it by customizing your Dockerfile. This method uses setuptools entrypoint for your project. You can read more about Python packaging here.
For example, you can create a package plugins with the following structure:
-
In the
pyproject.tomlfile, add the following: -
Define the policies in
policy.py: -
(Optional) Build the Python package:
Step 2: Setup your Astro project
- Initialize your Astro project using the Astro CLI or reopen your Astro project.
-
Copy over your plugin package to the
pluginsdirectory of your Astro project. -
Add the following line to your
Dockerfile: -
Run
astro dev restartto refresh your local Airflow instance. Runastro deployto build and deploy to your Astro Deployment.
See also
- Airflow docs on Cluster policies
- Airflow summit session on Cluster policies