Airflow Params and Run Config
Params and run configuration let you pass values into a DAG when you start it. A single DAG then handles many situations without code edits. You can choose a region, a date range, or a file name at the moment you press the Trigger button.
The Restaurant Order Form Picture
A kitchen cooks the same dish with different choices: spicy or mild, with rice or bread. The order form carries those choices to the chef. Params act as the order form for a DAG run.
Trigger form ┌──────────────────────┐ │ region: [ south ] │ ──► DAG run uses region = south │ limit: [ 500 ] │ and limit = 500 └──────────────────────┘
Define Params in a DAG
from datetime import datetime
from airflow import DAG
from airflow.models.param import Param
from airflow.operators.python import PythonOperator
def export_sales(**context):
region = context["params"]["region"]
limit = context["params"]["limit"]
print(f"Exporting {limit} rows for region {region}")
with DAG(
dag_id="sales_export",
start_date=datetime(2024, 1, 1),
schedule=None,
catchup=False,
params={
"region": Param("north", type="string", enum=["north", "south", "east"]),
"limit": Param(100, type="integer", minimum=1, maximum=1000),
},
) as dag:
export = PythonOperator(task_id="export", python_callable=export_sales)
What Each Part Does
| Piece | Meaning |
|---|---|
Param("north") | Default value used when nobody changes it |
type | Allowed data type, such as string or integer |
enum | List of allowed choices, shown as a dropdown |
minimum, maximum | Number limits that Airflow checks |
schedule=None | The DAG runs only when triggered by hand or by another system |
The Trigger Form
Click Trigger DAG with Config in the UI. Airflow builds a form from your params. Dropdowns appear for enum values and number boxes appear for integers. Airflow rejects values that break the rules, so bad input never reaches your task code.
Use Params in Templates
from airflow.operators.bash import BashOperator
report = BashOperator(
task_id="report",
bash_command="python make_report.py --region {{ params.region }} --limit {{ params.limit }}",
)
Trigger with Config from the CLI
airflow dags trigger sales_export --conf '{"region": "south", "limit": 250}'
The same JSON works through the REST API. Other systems start your DAG with custom values this way.
Reading dag_run.conf
The conf dictionary holds the raw values sent with the trigger.
def show_conf(**context):
conf = context["dag_run"].conf or {}
print(conf.get("region", "no region sent"))
By default, values sent in conf override matching params. Params add validation and defaults on top. For clean DAGs, prefer params.
Params versus Variables
| Feature | Params | Variables |
|---|---|---|
| Scope | One DAG run | Whole Airflow installation |
| Changed by | Person or system that triggers the run | Admin through UI or CLI |
| Validation | Yes, through the Param class | No |
| Best for | Per-run choices | Shared settings |
Practical Uses
- Reprocess one customer by passing a customer id.
- Choose a target environment such as test or production.
- Limit a test run to a small number of rows.
- Pass a file path from an external system.
Good Habits
- Give every param a safe default.
- Restrict choices with
enumwhenever the list is short. - Keep secrets out of params, since run configuration appears in the UI and logs.
Key Takeaways
- Params carry per-run values into a DAG.
- The trigger form validates input before the run starts.
- Templates and Python code both read params.
- The CLI and API send the same values as JSON.
