Bird
Raised Fist0
Apache Airflowdevops~15 mins

Debugging with Airflow CLI - Mini Project: Build & Apply

Choose your learning style10 modes available
Debugging with Airflow CLI
📖 Scenario: You are a data engineer managing workflows using Apache Airflow. Sometimes, tasks in your workflows fail, and you need to quickly find out what went wrong. Airflow provides a command-line tool (CLI) to help you debug these issues by checking task logs and statuses.
🎯 Goal: Learn how to use the Airflow CLI commands to check the status of a task instance and view its logs for debugging.
📋 What You'll Learn
Use the Airflow CLI commands exactly as specified
Work with a DAG named example_dag
Check the status and logs of a task named example_task
Use the execution date 2024-04-01T00:00:00+00:00
💡 Why This Matters
🌍 Real World
Data engineers and DevOps professionals use Airflow CLI commands daily to monitor and debug workflows without needing to open the Airflow web interface.
💼 Career
Knowing how to quickly check task statuses and logs using the Airflow CLI helps you troubleshoot workflow issues efficiently, a key skill for roles managing data pipelines and automation.
Progress0 / 4 steps
1
Set up the DAG and task names
Create two variables: dag_id with the value 'example_dag' and task_id with the value 'example_task'.
Apache Airflow
Hint

Use simple assignment statements to create the variables with the exact string values.

2
Set the execution date variable
Create a variable called execution_date and set it to the string '2024-04-01T00:00:00+00:00'.
Apache Airflow
Hint

Assign the exact string to the variable execution_date.

3
Write the Airflow CLI command to check task state
Create a variable called check_state_cmd that holds the exact Airflow CLI command string to check the state of the task instance. Use the format: airflow tasks state {dag_id} {task_id} {execution_date} with the variables inserted using an f-string.
Apache Airflow
Hint

Use an f-string to insert the variables into the command string exactly as shown.

4
Print the command to view task logs
Print the exact Airflow CLI command to view the logs of the task instance using the format: airflow tasks logs example_dag example_task 2024-04-01T00:00:00+00:00.
Apache Airflow
Hint

Use a print statement with an f-string to output the exact command string.

Practice

(1/5)
1. What is the primary purpose of the Airflow CLI command airflow tasks list DAG_ID?
easy
A. To show logs of a specific task
B. To run all tasks in a DAG immediately
C. To clear all task instances in a DAG
D. To display all tasks in a specific DAG

Solution

  1. Step 1: Understand the command structure

    The command airflow tasks list DAG_ID is used to list tasks related to the DAG identified by DAG_ID.
  2. Step 2: Identify the command's function

    This command shows all tasks defined in the DAG, helping users see what tasks exist.
  3. Final Answer:

    To display all tasks in a specific DAG -> Option D
  4. Quick Check:

    List tasks = Display tasks [OK]
Hint: List tasks to see all steps in a DAG quickly [OK]
Common Mistakes:
  • Confusing 'list' with 'run' to execute tasks
  • Thinking it clears tasks instead of listing
  • Assuming it shows logs instead of task names
2. Which of the following is the correct Airflow CLI command to view logs for a task named task1 in DAG example_dag for execution date 2023-01-01T00:00:00?
easy
A. airflow tasks logs example_dag task1 2023-01-01T00:00:00
B. airflow logs example_dag task1 2023-01-01T00:00:00
C. airflow task logs example_dag task1 2023-01-01T00:00:00
D. airflow tasks log example_dag task1 2023-01-01T00:00:00

Solution

  1. Step 1: Recall the correct Airflow CLI syntax for logs

    The correct command to view logs is airflow tasks logs DAG_ID TASK_ID EXECUTION_DATE.
  2. Step 2: Match the command with the options

    airflow tasks logs example_dag task1 2023-01-01T00:00:00 matches the exact syntax with correct keywords and parameters.
  3. Final Answer:

    airflow tasks logs example_dag task1 2023-01-01T00:00:00 -> Option A
  4. Quick Check:

    Correct syntax for logs = airflow tasks logs example_dag task1 2023-01-01T00:00:00 [OK]
Hint: Use 'tasks logs' to view task logs with DAG and date [OK]
Common Mistakes:
  • Using 'logs' without 'tasks' keyword
  • Misspelling 'tasks' as 'task'
  • Using 'log' instead of 'logs'
3. What will be the output of the command airflow tasks clear example_dag -t task1 -s 2023-01-01 -e 2023-01-02?
medium
A. It runs task1 in example_dag for the specified dates
B. It lists all tasks in example_dag between the given dates
C. It clears all task instances of task1 in example_dag between 2023-01-01 and 2023-01-02
D. It shows logs of task1 for the specified dates

Solution

  1. Step 1: Understand the 'tasks clear' command

    The command airflow tasks clear removes task instance states, allowing rerun.
  2. Step 2: Analyze the options -t, -s, -e

    -t specifies the task, -s and -e specify start and end dates for clearing task instances.
  3. Final Answer:

    It clears all task instances of task1 in example_dag between 2023-01-01 and 2023-01-02 -> Option C
  4. Quick Check:

    Clear task instances = It clears all task instances of task1 in example_dag between 2023-01-01 and 2023-01-02 [OK]
Hint: Clear tasks with -t for task and -s/-e for date range [OK]
Common Mistakes:
  • Confusing clear with run or list commands
  • Ignoring date range flags -s and -e
  • Assuming it shows logs instead of clearing
4. You run airflow tasks run example_dag task1 2023-01-01 but get an error saying the task instance does not exist. What is the most likely cause?
medium
A. The command syntax is incorrect and missing required flags
B. The DAG is not loaded or paused, so the task instance is missing
C. The execution date format is wrong and not recognized
D. The task1 does not exist in any DAG

Solution

  1. Step 1: Understand task instance creation

    Task instances exist only if the DAG is loaded and active (not paused).
  2. Step 2: Identify why instance might be missing

    If the DAG is paused or not loaded, the task instance for the date won't exist, causing the error.
  3. Final Answer:

    The DAG is not loaded or paused, so the task instance is missing -> Option B
  4. Quick Check:

    Missing instance = DAG paused/not loaded [OK]
Hint: Check DAG status; paused DAGs block task instances [OK]
Common Mistakes:
  • Assuming syntax error without checking DAG state
  • Blaming date format without verifying
  • Thinking task1 is missing from DAG
5. You want to debug a failed task task2 in DAG data_pipeline for execution date 2023-06-15T12:00:00. Which sequence of Airflow CLI commands helps you clear the failed task, rerun it, and then view its logs?
hard
A. airflow tasks clear data_pipeline -t task2 -s 2023-06-15T12:00:00 -e 2023-06-15T12:00:00 && airflow tasks run data_pipeline task2 2023-06-15T12:00:00 && airflow tasks logs data_pipeline task2 2023-06-15T12:00:00
B. airflow tasks run data_pipeline task2 2023-06-15T12:00:00 && airflow tasks clear data_pipeline -t task2 -s 2023-06-15T12:00:00 -e 2023-06-15T12:00:00 && airflow tasks logs data_pipeline task2 2023-06-15T12:00:00
C. airflow tasks logs data_pipeline task2 2023-06-15T12:00:00 && airflow tasks clear data_pipeline -t task2 -s 2023-06-15T12:00:00 -e 2023-06-15T12:00:00 && airflow tasks run data_pipeline task2 2023-06-15T12:00:00
D. airflow tasks clear data_pipeline -t task2 && airflow tasks logs data_pipeline task2 2023-06-15T12:00:00 && airflow tasks run data_pipeline task2 2023-06-15T12:00:00

Solution

  1. Step 1: Clear the failed task instance for the exact execution date

    Use airflow tasks clear with task name and exact start/end date to reset the failed task.
  2. Step 2: Run the cleared task instance

    Use airflow tasks run with DAG, task, and execution date to rerun the task.
  3. Step 3: View the logs of the rerun task

    Use airflow tasks logs to check the output and debug information.
  4. Final Answer:

    Clear, then run, then logs in sequence with correct dates -> Option A
  5. Quick Check:

    Clear -> Run -> Logs = airflow tasks clear data_pipeline -t task2 -s 2023-06-15T12:00:00 -e 2023-06-15T12:00:00 && airflow tasks run data_pipeline task2 2023-06-15T12:00:00 && airflow tasks logs data_pipeline task2 2023-06-15T12:00:00 [OK]
Hint: Clear failed task first, then run, then check logs [OK]
Common Mistakes:
  • Running task before clearing causes rerun failure
  • Checking logs before rerun shows old failure logs
  • Omitting date range in clear command
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard