TECHNOLOGIESTieInn Technologies

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.get at 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
5 questions

    XComs, variables, and connections

  1. Question 1easy

    What should you pass between tasks with XCom?

  2. XComs, variables, and connections

  3. Question 2easy

    Where should a database password used by a DAG be stored?

  4. XComs, variables, and connections

  5. Question 3medium

    Why avoid Variable.get() at the top level of a DAG file?

  6. XComs, variables, and connections

  7. Question 4medium

    What does the report task print?

    @task
    def count_rows() -> int:
        return 42
    
    @task
    def report(n: int):
        print(n * 2)
    
    report(count_rows())
  8. XComs, variables, and connections

  9. Question 5medium

    Task load sets a module-level variable count = 80. The next task prints count. What does it print?

8 more questions on this topic, with hints, and tests

Sign up free for 21 days of every question and test.