The DAG Factory is an open source tool managed by Astronomer that allows you to dynamically generate Apache Airflow® DAGs from YAML. While Airflow DAGs are traditionally written exclusively in Python, the DAG Factory makes it easy for people who don’t know Python to use Airflow.
This guide includes instructions for installing the DAG Factory package into your Astro project and a sample YAML configuration file that you can use to easily specify the details of your DAG, including its schedule, callbacks, and task names.
The DAG Factory can be used with all Astronomer products and any Apache Airflow installation. To view the source code of the project, see DAG Factory.
If you’re an Astronomer customer, you must have an Astro project with a supported version of Astro Runtime. See Astro Runtime lifecycle schedule for a list of currently supported Runtime versions.
To use the DAG Factory, install it as a Python package into your Apache Airflow environment.
If you’re an Astronomer customer:
requirements.txt file.dag-factory<=1.0.0 to the file.requirements.txt file.If you’re not an Astronomer customer, install the Python package according to the standards at your organization:
dags directory of your Astro project, create a new sub-directory called configs to store your DAG configuration files defined in YAML. Astronomer recommends keeping these separate from standard DAG files written in Python.my_dag.yaml. Copy the contents of the following example configuration file into the YAML file.Modify the example configuration file with parameters for the DAG you want to create, including replacing <your-DAG-id> with a valid dag_id. See DAG-level parameters in Airflow to learn more about each parameter.
(Optional) Delete any configurations that you don’t want to specify. For parameters that aren’t specified, your DAG will assume the default values that correspond with your current Apache Airflow or Astro Runtime version.
All YAML files in your dags directory must be parsed and converted into Python in order to run on Apache Airflow. In this step, you will create a new DAG Factory file in your Astro project that includes the conversion logic. You only need to do this once and do not need a separate DAG Factory file for each of your DAGs or YAML files.
dags directory of your Astro project, create a new Python file called dag_factory.py.In order to use DAG-level callbacks you will need to add callback parameters to your config file. The values will be the paths to the files that contain your callback functions, as well as the callback function names.
In my_dag.yml, add the following parameters:
callback_func.py in your dags directory.The example above shows how to use the DAG factory to create DAGs based on static YAML files. For use cases where you’d like to create several DAGs with a similar structure it is possible to create them dynamically based on a template YAML file to avoid code duplication. Creating a DAG dynamically with the DAG factory simply means that you use Python code to create the YAML configurations instead of writing them manually.
There are two files that you need:
The template YAML file provides the structure for all the DAGs you will generate dynamically with placeholders for values that vary in between the DAGs:
The Python script reads the template YAML file, replaces the placeholders with the actual values, and writes the resulting YAML files to the dags directory. You can run this script manually to generate your DAGs for local development or automatically as part of your CI/CD pipeline.