Airflow File Structure & Best Practices
Designing a Scalable Platform ποΈπβ
The Story: When Airflow Turns into a Junk Drawerβ
At first, everything is clean.
One DAG.
One folder.
Life is good.
Six months later:
- 200 DAGs in one directory
- Random helper scripts
- Hardcoded configs
- No naming standards
- New engineers afraid to touch anything
Airflow didnβt fail β structure did.
A well-designed file structure turns Airflow from a scheduler into a maintainable data platform.
Why Airflow File Structure Mattersβ
Airflow continuously:
- Scans files
- Imports Python modules
- Parses DAGs
Poor structure leads to:
- Slow DAG parsing
- Scheduler overload
- Hard-to-debug failures
- Unmaintainable pipelines
π File structure is performance, reliability, and governance combined.
Default Airflow Directory Layoutβ
A typical Airflow home looks like this:
$AIRFLOW_HOME/
βββ dags/
βββ logs/
βββ plugins/
βββ airflow.cfg
βββ webserver_config.py
Each directory has a specific responsibility.
The dags/ Directory (Most Important)β
This is where Airflow looks for DAG definitions.
Basic Exampleβ
dags/
βββ example_dag.py
βββ sales_etl.py
Production-Ready Structure (Recommended)β
dags/
βββ sales/
β βββ sales_etl_dag.py
β βββ tasks.py
β βββ helpers.py
βββ marketing/
β βββ campaign_dag.py
β βββ transformations.py
βββ common/
βββ constants.py
βββ utils.py
π Rule:
Only DAG definitions should be parsed at top-level imports.
DAG Parsing Best Practicesβ
Airflow parses DAGs every few seconds.
Do β β
- Keep imports lightweight
- Move heavy logic inside tasks
- Use helper modules
Avoid ββ
- Database connections at import time
- API calls at import time
- Large computations in global scope
β Bad Exampleβ
data = load_big_dataset() # runs on every parse
β Good Exampleβ
@task
def load_data():
return load_big_dataset()
The plugins/ Directoryβ
Used for custom Airflow extensions.
What Belongs Here?β
- Custom Operators
- Hooks
- Sensors
- Macros
- UI plugins
Example Structureβ
plugins/
βββ operators/
β βββ custom_api_operator.py
βββ hooks/
β βββ custom_db_hook.py
βββ macros/
βββ date_macros.py
π Plugins are loaded globally β keep them clean and minimal.
The logs/ Directoryβ
Stores:
- Task logs
- Scheduler logs
- Webserver logs
Local Logging Structureβ
logs/
βββ dag_id=example_dag/
βββ task_id=task1/
βββ execution_date=2024-01-10/
π In production, logs usually live in remote storage.
Configuration Filesβ
airflow.cfgβ
Main Airflow configuration file.
Controls:
- Executor type
- Logging backend
- Database connection
- Scheduler behavior
π Avoid editing directly in containers β use env vars.
webserver_config.pyβ
Controls:
- Authentication
- UI behavior
- RBAC settings
Used mostly in enterprise setups.
Environment-Based Structure (Best Practice)β
Separate environments cleanly:
airflow/
βββ dags/
βββ plugins/
βββ config/
β βββ dev.env
β βββ staging.env
β βββ prod.env
π One codebase, multiple environments.
Naming Conventions (Highly Recommended)β
| Item | Convention |
|---|---|
| DAG ID | lowercase_with_underscores |
| Task ID | verb_noun |
| File names | snake_case.py |
| Folders | domain-based |
π Consistency = maintainability.
Input & Output Example (Realistic)β
Inputβ
Team with:
- 10 data domains
- 20 DAGs per domain
- Shared utilities
Outputβ
- Faster DAG parsing
- Clear ownership
- Fewer production incidents
- Easier onboarding
Common File Structure Mistakesβ
β All DAGs in one folder
β Business logic at import time
β Random scripts inside dags/
β No naming conventions
β Environment-specific logic in DAG code
Best Practices Summaryβ
β
Organize DAGs by domain
β
Keep DAG files thin
β
Move logic into tasks and helpers
β
Use plugins for reusable components
β
Separate config from code
β
Structure for scale, not convenience
Summary π§ β
- Airflow file structure defines platform quality
- Clean DAG folders improve performance
- Plugins enable extensibility
- Configuration should be environment-driven
- Structure is critical as DAG count grows
Key Takeawaysβ
- β DAG parsing performance depends on structure
- β Clean folders reduce cognitive load
- β Plugins prevent code duplication
- β Naming standards improve collaboration
- β Structure is a long-term investment
Whatβs Next? πβ
β‘οΈ Secrets Management in Airflow
Environment Variables, HashiCorp Vault, AWS Secrets Manager, and best practices for secure pipelines