📖 Scenario: You are setting up Apache Airflow to connect to a cloud storage service. To do this, you need to create and manage a connection in Airflow that stores the credentials and endpoint details securely. This project will guide you through creating a connection, configuring it, using it in a DAG, and finally printing the connection details.
🎯 Goal: Build an Airflow setup where you create a cloud service connection, configure it with necessary details, use it in a DAG to retrieve connection info, and print the connection details.
📋 What You'll Learn
Create an Airflow connection with exact ID and parameters
Add configuration variables for the connection
Write a DAG that uses the connection to get connection info
Print the connection details in the DAG task
💡 Why This Matters
🌍 Real World
Managing connections in Airflow is essential for securely accessing cloud services like storage, databases, or APIs without hardcoding credentials.
💼 Career
DevOps and Data Engineering roles often require setting up and managing Airflow connections to automate workflows that interact with cloud platforms.
Progress0 / 4 steps
1
Create an Airflow connection for cloud storage
Create an Airflow connection with the connection ID cloud_storage_conn. Set the connection type to Google Cloud, host to https://storage.googleapis.com, and login to user123. Use the password pass123.
Apache Airflow
Hint
Use the Connection class from airflow.models to create the connection object with the exact parameters.
2
Add configuration variables for the connection
Create a variable called project_id and set it to my-gcp-project. Also create a variable called location and set it to us-central1.
Apache Airflow
Hint
Simply assign the exact string values to the variables project_id and location.
3
Write a DAG that uses the connection to get connection info
Write a DAG named cloud_storage_dag with a single PythonOperator task called print_conn_info. In the task's Python function, use Airflow's BaseHook.get_connection to get the connection with ID cloud_storage_conn. Extract the host, login, and password from the connection object and store them in variables named conn_host, conn_login, and conn_password respectively.
Apache Airflow
Hint
Define a Python function that calls BaseHook.get_connection('cloud_storage_conn') and extracts the host, login, and password. Then create a DAG and a PythonOperator that runs this function.
4
Print the connection details in the DAG task
In the print_connection_info function, add print statements to display the connection host, login, and password. The output should be exactly: Host: https://storage.googleapis.com Login: user123 Password: pass123
Apache Airflow
Hint
Use print(f"Host: {conn_host}") and similar print statements for login and password inside the function.
Practice
(1/5)
1. What is the main purpose of managing connections in Airflow for cloud services?
easy
A. To manually enter credentials every time a task runs
B. To write custom code for each cloud service login
C. To store cloud login details securely and reuse them in tasks
D. To disable cloud service access in Airflow
Solution
Step 1: Understand connection management role
Connection management stores credentials securely so tasks can access cloud services without exposing secrets.
Step 2: Identify reuse benefit
Reusing connections avoids repeating credentials in multiple tasks, making workflows cleaner and safer.
Final Answer:
To store cloud login details securely and reuse them in tasks -> Option C
Quick Check:
Secure storage + reuse = A [OK]
Hint: Connections store credentials once for many tasks [OK]
Common Mistakes:
Thinking credentials must be typed in each task
Assuming connections are only for UI use
Believing connections disable cloud access
2. Which command correctly adds a Google Cloud connection named 'gcp_default' in Airflow CLI?
easy
A. airflow connections add 'gcp_default' --conn-type google_cloud_platform --conn-extra '{"extra__google_cloud_platform__key_path":"/path/key.json"}'
B. airflow add connection gcp_default --type gcp --extra '{"key_path":"/path/key.json"}'
C. airflow connections create gcp_default --type google --extra '{"key":"/path/key.json"}'
D. airflow connection add gcp_default --conn-type gcp --conn-extra '{"key_path":"/path/key.json"}'
Solution
Step 1: Recall correct Airflow CLI syntax
The command to add connections is 'airflow connections add' followed by connection id and parameters.
Step 2: Match correct flags and JSON format
airflow connections add 'gcp_default' --conn-type google_cloud_platform --conn-extra '{"extra__google_cloud_platform__key_path":"/path/key.json"}' uses '--conn-type google_cloud_platform' and properly formatted JSON for extra parameters, matching Airflow docs.