Documentation Index
Fetch the complete documentation index at: https://astronomer-preview.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
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.
Prerequisites
- Python version 3.8.0 or greater
- Apache Airflow version 2.0 or greater
Step 1: Install DAG Factory
To use the DAG Factory, install it as a Python package into your Apache Airflow environment. If you’re an Astronomer customer:- In your Astro project, open your
requirements.txtfile. - Add
dag-factory<=1.0.0to the file. - Save the changes to your
requirements.txtfile.
Step 2: Create a sub-directory for your DAG configuration files and create a YAML file for your DAG
- In the
dagsdirectory of your Astro project, create a new sub-directory calledconfigsto store your DAG configuration files defined in YAML. Astronomer recommends keeping these separate from standard DAG files written in Python. - Within the new sub-directory, create a new YAML file for your DAG called for example
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 validdag_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.
Step 3: Create a DAG Factory file
All YAML files in yourdags 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.
- In the
dagsdirectory of your Astro project, create a new Python file calleddag_factory.py. - Copy the following contents into your empty Python file. This file represents an Apache Airflow DAG and includes two commands that convert each of your YAML file(s) into DAGs.
Step 4: (Optional) Add a DAG-level callback
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. Inmy_dag.yml, add the following parameters:
- Create a new file
callback_func.pyin yourdagsdirectory. - Copy the contents of the following placeholder callback into the file:
- Save the file.
Generating YAML files dynamically
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:- A template YAML file that contains the structure of the DAGs you want to create with placeholders for the values that will change.
- A Python script that creates the DAG Factory YAML file by replacing the placeholders in the template YAML file with the actual values.
dags directory. You can run this script manually to generate your DAGs for local development or automatically as part of your CI/CD pipeline.