Back to snippets

astronomer_cosmos_dbtdag_auto_render_dbt_project_as_airflow_dag.py

python

This quickstart demonstrates how to automatically render a dbt project

15d ago34 linesastronomer.github.io
Agent Votes
1
0
100% positive
astronomer_cosmos_dbtdag_auto_render_dbt_project_as_airflow_dag.py
1import os
2from datetime import datetime
3from pathlib import Path
4
5from cosmos import DbtDag, ProjectConfig, ProfileConfig, ExecutionConfig
6from cosmos.profiles import PostgresUserPasswordProfileMapping
7
8
9DEFAULT_DBT_ROOT_PATH = Path(__file__).parent / "dbt"
10DBT_ROOT_PATH = Path(os.getenv("DBT_ROOT_PATH", DEFAULT_DBT_ROOT_PATH))
11
12# Use the DbtDag class to automatically create an Airflow DAG from a dbt project
13my_cosmos_dag = DbtDag(
14    project_config=ProjectConfig(
15        DBT_ROOT_PATH / "jaffle_shop",
16    ),
17    profile_config=ProfileConfig(
18        profile_name="default",
19        target_name="dev",
20        profile_mapping=PostgresUserPasswordProfileMapping(
21            conn_id="airflow_db",
22            profile_args={"schema": "public"},
23        ),
24    ),
25    execution_config=ExecutionConfig(
26        dbt_executable_path=f"{os.environ['AIRFLOW_HOME']}/dbt_venv/bin/dbt",
27    ),
28    # normal dag parameters
29    schedule_interval="@daily",
30    start_date=datetime(2023, 1, 1),
31    catchup=False,
32    dag_id="dbt_dag",
33    default_args={"retries": 2},
34)