XComs, variables, and connections
Airflow (Cloud Composer) · 13 questions
Short notes
Key idea
XComs pass small values between tasks. Variables store settings. Connections store credentials and hosts for outside systems, so secrets stay out of code.
Syntax
@task
def count(): return 42 # pushed to XCom automatically
@task
def report(n): print(n) # pulled automatically
report(count())
from airflow.models import Variable
bucket = Variable.get("raw_bucket")
# hook from a connection id defined in the UI or env
from airflow.providers.postgres.hooks.postgres import PostgresHook
PostgresHook(postgres_conn_id="shop_db").get_records("SELECT 1")
Example
extract writes a day's file to Cloud Storage and returns the path; load receives the path through XCom and loads it into BigQuery. The data travels via storage, only the path via XCom.
Traps
- XCom is for small values (IDs, paths, counts), not DataFrames or files; it lives in Airflow's database.
Variable.getat the top of a DAG file queries the database on every parse; call it inside tasks or use templates.- Never hard-code passwords; use Connections or a secrets backend.
- Connection IDs differ per environment; keep the names the same and change values.
Want the full lesson with step-by-step examples, a project and a certificate? Airflow (Cloud Composer) course, ₹120 →
Questions
5 free questions- Question 1easy
What should you pass between tasks with XCom?
- Question 2easy
Where should a database password used by a DAG be stored?
- Question 3medium
Why avoid
Variable.get()at the top level of a DAG file? - Question 4medium
What does the
reporttask print?@task def count_rows() -> int: return 42 @task def report(n: int): print(n * 2) report(count_rows()) - Question 5medium
Task
loadsets a module-level variablecount = 80. The next task printscount. What does it print?
XComs, variables, and connections
XComs, variables, and connections
XComs, variables, and connections
XComs, variables, and connections
XComs, variables, and connections
8 more questions on this topic, with hints, and tests
Sign up free for 21 days of every question and test.