You are currently viewing [SOLVED] Apache Airflow How To Create dynamic DAG – 5 min solution!
Could You Please Share This Post? I Appreciate It And Thank YOU! :) Have A Nice Day!

Apache Airflow gives us possibility to create dynamic DAG. This feature is very useful when we would like [ Apache Airflow How To Create dynamic DAG ] to achieve flexibility in Airflow, to do not create many DAGs for each case but have only on DAG where we will have power to change the tasks and relationships between them dynamically.

Concept -> Apache Airflow How To Create dynamic DAG

Our dynamic DAG will be built based on JSON file which could be created by another process. But for this tutorial purpose we will create static JSON file by own.

Let’s imagine that we would like to load tables from one source to target database, but do not load all set of tables but only these ones which contains new data and this list of tables will be included in our JSON configuration file. Additionally, our loading process will have two steps: Init and Clear.

Apache Airflow: How To Create dynamic DAG

Configuration File

Below you can find the structure of JSON configuration file where I specified the:

  • name – of DAG
  • schedule – of DAG
  • tables – list of tables. It indicates the amount of tasks between Init and Clear phases
  "name": "Dynamic_DAG",
  "schedule": "@hourly",
  "tables": [

DAG Code

Create new python file in the $AIRFLOW_HOME/dags folder. It will be our new DAG. Apache Airflow How To Create dynamic DAG

Please find the DAG source code below. 

from airflow.models import DAG
from datetime import datetime, timedelta
from airflow.operators.dummy_operator import DummyOperator

import json

def create_dag(dag_id,
    dag = DAG(dag_id, default_args=default_args, schedule_interval=schedule)

    with dag:
        init = DummyOperator(

        clear = DummyOperator(

        for table in conf['tables']:
            tab = DummyOperator(
            init >> tab >> clear

        return dag

with open("/home/pawel/airflow-dynamic-dag-conf/process_configuration.json") as json_data:
    conf = json.load(json_data)
    schedule = conf['schedule']
    dag_id = conf['name']

    args = {
        'owner': 'BigDataETL',
        'depends_on_past': False,
        'email': [''],
        'email_on_failure': False,
        'email_on_retry': False,
        'retries': 1,
        'retry_delay': timedelta(minutes=5),
        'concurrency': 1,
        'max_active_runs': 1
    globals()[dag_id] = create_dag(dag_id, schedule, args, conf)

Airflow UI -> Apache Airflow: How To Create dynamic DAG

Your new DAG now is available from Airflow UI. When you click on it and go to Graph View you will see that our DAG consists of:

  • one Init task
  • four “Table” task
  • one Clear task
Apache Airflow: How To Create dynamic DAG


Now we will remove two tables from configuration file to like below:

  "name": "Dynamic_DAG",
  "schedule": "@hourly",
  "tables": [

And after that let’s refresh you DAG from Airflow UI. You will see that our new DAG has only two table tasks in Graph View.

Apache Airflow Create dynamic DAG- 5 min solution!

Summary -> Apache Airflow: How To Create dynamic DAG

After this tutorial you should be able to generate dynamic DAG in Apache Airflow based on external configuration. This approach gives you ability to achieve flexibility to create of complex and dynamic DAG in Airflow. (Apache Airflow How To Create dynamic DAG)

Could You Please Share This Post? 
I appreciate It And Thank YOU! :)
Have A Nice Day!


How useful was this post?

Click on a star to rate it!

Average rating 4.8 / 5. Vote count: 891

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?