AIRFLOW Airflow: Dynamic Task Mapping and Dynamic DAGs
Real workloads don't always have a fixed number of tasks. A file processing pipeline might handle 3 files today and 300 tomorrow. Airflow's dynamic task mapping lets your DAG adapt at runtime. We'll compare two approaches: dynamic DAGs built with Python loops, and task mapping over the output of an upstream task.
Dynamic Task Mapping
Dynamic task mapping lets Airflow generate tasks at runtime based on the output of another task. Instead of writing a fixed number of tasks, you write a single task and tell Airflow to run it once per item in a list.
Two things make this different from a Python for loop:
- Each mapped instance is a separate Airflow task with its own logs, status, and retry behavior.
- The list can come from an upstream task's return value. It does not need to be known when the DAG file is written.
Creating the DAG File
In VS Code, navigate to the Airflow directory, then to the DAGs folder. Create a file called mapped_dag.py.
Only two imports are required:
from airflow.decorators import DAG, task
from datetime import datetime
DAGandtaskcome from the TaskFlow API for decorator-based DAGs.datetimesets thestart_date.
Defining the First Task
The first task returns a list of file names:
@task
def get_files():
files = ["sales.csv", "users.csv", "events.csv", "logs.csv"]
print(f"Number of files: {len(files)}")
return files
Defining the Mapped Task
The second task is written as if it only handles a single item:
@task
def process_file(file_name):
print(f"Processing {file_name}")
return f"{file_name} done"
Building the DAG
@DAG(
dag_id="mapped_dag",
start_date=datetime(2023, 1, 1),
schedule=None,
catchup=False
)
def my_dag():
files = get_files()
process_file.expand(file_name=files)
mapped_dag = my_dag()
Key fields:
schedule=None– the DAG runs only when manually triggered.catchup=False– no backfilling.process_file.expand(file_name=files)– tells Airflow to mapprocess_fileover thefileslist. One task instance is created per element.
The line mapped_dag = my_dag() registers the DAG with Airflow.
Running the DAG
Open the Airflow UI, search for mapped_dag, and trigger a single run.
Expected behavior:
get_filesruns once and returns a list of four file names.process_filegenerates four task instances, one for each file.
Click on process_file in the graph view, then open Task Instances. Each instance is indexed:
- Index 0 – processes
sales.csv - Index 1 – processes
users.csv - Index 2 – processes
events.csv - Index 3 – processes
logs.csv
What's next
Start Airflow Suggested tutorials
Keep the momentum going, pick a related topic.
AIRFLOWAirflow: Connections, Hooks, and Providers
Real pipelines move data between systems and Airflow handles that through three concepts: connections store credentials securely, hooks give you a working client to talk to an external system, and providers ship the code that makes it all work. We'll connect a DAG to a Postgres database and pull data through a hook.
Open tutorial
AIRFLOWAirflow: Branching and Trigger Rules
Not every pipeline runs the same path every time. Sometimes the next step depends on a condition and Airflow handles this with branching. We'll build a DAG that picks between two paths using a branch operator and see how trigger rules control what happens when paths merge back together.
Open tutorial