Apache Airflow: From Basics to Mastery
So far, you’ve learned what DAGs are and how tasks and operators work. Now it’s time to put that knowledge into practice by writing your first Python DAG in Apache Airflow.
By the end of this lesson, you will be able to:
Define a basic DAG in Python.
Add a simple task using the
PythonOperator.Run the DAG from the Airflow Web UI.
Understand the structure of a DAG file.
1. Structure of a DAG File
Every Airflow DAG is defined in a Python file. A typical DAG file contains:
Imports:Â DAG, operators, and datetime.
DAG definition:Â ID, schedule, start date, settings.
Tasks:Â Created using operators.
Dependencies : Define task order.
2. Writing Your First Python DAG​
from airflow import DAG
from airflow.operators.python import PythonOperator
from datetime import datetime
# Step 1: Define the Python function
def greet():
print("Hello, Airflow! This is my first DAG.")
# Step 2: Define the DAG
with DAG(
dag_id="first_python_dag",
description="A simple Python DAG example",
start_date=datetime(2025, 1, 1),
schedule_interval="@daily", # runs once per day
catchup=False
) as dag:
# Step 3: Define a task
greet_task = PythonOperator(
task_id="greet_task",
python_callable=greet
)
3. Explanation of Key Parts
dag_id="first_python_dag": Unique identifier for your DAG.start_date: Date the DAG starts scheduling runs.schedule="@daily": DAG runs once a day.catchup=False: Prevents backfilling old runs.PythonOperator: Runs a Python function as a task.greet_task: The single task in this DAG.
4. Running the DAG
Save the file as
first_python_dag.pyin your Airflowdags/folder.Start Airflow (scheduler + webserver).
Open the Web UI → DAGs list.
Enable
first_python_dag.Trigger it manually or wait for the scheduler.
Open Logs to see:
Hello, Airflow! This is my first DAG.
5. Adding Dependencies (Optional)
If you had multiple tasks, you’d define their execution order:
task1 >> task2
👉 For now, we only have one task, so no dependencies are needed.
Lesson Summary
- A DAG file contains imports, DAG definition, tasks, and dependencies.
The PythonOperator lets you run Python functions as tasks.
Saving the DAG in the
dags/folder makes it available in the Airflow UI.You successfully created and ran your first DAG! 🎉
Finish Course Early?
You have not completed all required lessons and assessments.