Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Airflow + Astronomer Cosmos Deployment

This repository adds an additional Airflow orchestration option for the existing DBT SQL-Based Data Warehouse project by using Astronomer Cosmos.

The dbt SQL models are kept unchanged. Cosmos is used only to improve Airflow orchestration, model-level visibility, debugging, and rerun control.


Overview

The previous Airflow implementation used BashOperator tasks grouped by layer:

run_bronze_layer
run_silver_layer
run_gold_layer

This works, but each layer appears as one large task in the Airflow UI.

With Astronomer Cosmos, dbt models and tests are rendered as individual Airflow tasks. This makes the pipeline easier to monitor and debug.

Example task structure:

br_crm_cust_info
├── run
└── test

sl_crm_prd_info
├── run
└── test

dim_customers
├── run
└── test

Why Use Astronomer Cosmos?

Cosmos improves the Airflow experience for dbt projects by providing:

  • Model-level task visibility
  • Separate run and test tasks
  • Better Graph view in Airflow
  • Easier debugging when a single model fails
  • More flexible rerun control
  • No changes to the existing dbt SQL logic

In short, Cosmos makes the dbt pipeline easier to observe and operate inside Airflow.


DAG Files

The Airflow DAGs are located in:

airflow/dags/
├── dbt_sql_based_dwh_dag.py
├── dbt_sql_based_dwh_cosmos_dag.py
└── dbt_sql_based_dwh_cosmos_layered_dag.py

Main Cosmos DAG

dbt_sql_based_dwh_cosmos_dag.py

This file contains the main Cosmos-based DAG definitions.

Optional Layered DAG

dbt_sql_based_dwh_cosmos_layered_dag.py

This DAG is mainly for learning and demonstration purposes. It explicitly shows the pipeline flow:

Bronze Layer -> Silver Layer -> Gold Layer

Available Cosmos DAGs

1. Full Refresh DAG

dbt_sql_based_dwh_cosmos_full_refresh

Use this DAG for the first run or when a full rebuild is required.

It runs dbt with:

full_refresh=True

This is equivalent to a first full load.


2. Incremental DAG

dbt_sql_based_dwh_cosmos_incremental

Use this DAG for normal scheduled runs.

It runs dbt with:

full_refresh=False

Bronze and Silver models use incremental logic. Gold models remain as views.


3. Bronze-Only DAG

dbt_sql_based_dwh_cosmos_bronze_only

Use this DAG when only the Bronze layer needs to be rerun.


4. Silver-Only DAG

dbt_sql_based_dwh_cosmos_silver_only

Use this DAG when a Silver transformation has been fixed and Bronze does not need to be rerun.


5. Gold-Only DAG

dbt_sql_based_dwh_cosmos_gold_only

Use this DAG when only the Gold views need to be rebuilt.


6. Optional Layered Pipeline DAG

dbt_sql_based_dwh_cosmos_layered_pipeline

This DAG shows the pipeline in a more traditional layer-by-layer structure:

Bronze -> Silver -> Gold

It is useful for learning, documentation, and visualizing the warehouse flow clearly.


Recommended Execution Flow

First Run

For the initial run, trigger the full refresh DAG:

dbt_sql_based_dwh_cosmos_full_refresh

Recommended steps:

  1. Open the Airflow UI.
  2. Trigger the full refresh DAG.
  3. Open the Graph view.
  4. Expand the dbt_project_graph task group.
  5. Monitor the dbt model-level tasks.
  6. Check the preview_gold_outputs task log after the run finishes.

Normal Daily Run

For regular scheduled runs, use:

dbt_sql_based_dwh_cosmos_incremental

This runs the same dbt project using incremental logic.


Rerun a Failed Model

One benefit of Cosmos is that a failed dbt model can be rerun without restarting the whole pipeline.

Steps:

  1. Open the failed DAG run in Airflow.
  2. Go to the Graph tab.
  3. Expand the Cosmos task group.
  4. Select the failed model task.
  5. Click Clear.
  6. Choose whether to clear only that task or include downstream tasks.

This is useful when only one model or test fails.


Rerun a Specific Layer

To rerun only one warehouse layer, use one of the layer-specific DAGs:

dbt_sql_based_dwh_cosmos_bronze_only
dbt_sql_based_dwh_cosmos_silver_only
dbt_sql_based_dwh_cosmos_gold_only

Example:

If the model silver.crm_prd_info fails, fix the dbt model and trigger:

dbt_sql_based_dwh_cosmos_silver_only

This avoids rerunning Bronze when it is not needed.


Docker Image Requirements

The Docker image includes the following main components:

Apache Airflow 2.10.5
Python 3.12
dbt-core 1.9.10
dbt-sqlserver 1.9.0
Astronomer Cosmos 1.10.1
Microsoft ODBC Driver 18 for SQL Server
git

git is included because dbt and Cosmos may use it for commands such as:

dbt deps
dbt debug
graph rendering

Rebuild the Docker Image

After changing the Dockerfile or DAG files, rebuild the environment:

docker compose --env-file .env down
docker compose --env-file .env build --no-cache
docker compose --env-file .env up airflow-init
docker compose --env-file .env up -d

Open the Airflow UI:

http://127.0.0.1:8081

Validate the Setup

Check Cosmos Installation

Run:

docker compose --env-file .env exec airflow-webserver python -c "import cosmos; print('Cosmos is installed')"

Expected output:

Cosmos is installed

Check dbt Connection

Run:

docker compose --env-file .env exec airflow-webserver dbt debug --project-dir /opt/airflow/dbt_project --profiles-dir /opt/airflow/dbt_profiles

Expected result:

Connection test: [OK connection ok]
All checks passed!

BashOperator vs Astronomer Cosmos

Approach Airflow Visibility Rerun Control
BashOperator One task per layer Rerun the whole layer
Astronomer Cosmos One task per dbt model/test Rerun a failed model or selected layer

Summary

Astronomer Cosmos improves the way Airflow displays and manages the dbt project.

Instead of showing only large Bronze, Silver, and Gold tasks, Cosmos exposes each dbt model and test as separate Airflow tasks. This makes the pipeline easier to monitor, debug, and rerun while keeping the existing dbt SQL logic unchanged.

About

DBT for SQL Based DWH + Airflow + Astronomer Cosmos Deployment

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages