When dbt runs in dbt Cloud, Airflow only has to start the job and wait for the result. The dbt Cloud provider does both with DbtCloudRunJobOperator: it calls the dbt Cloud API, follows the run until it finishes and fails the task if the job fails.

Copy and paste

1. Install the provider:

pip install apache-airflow-providers-dbt-cloud

2. Create the connection. Account ID in login, a service token in password, and your account’s host in host:

export AIRFLOW_CONN_DBT_CLOUD_DEFAULT='{"conn_type": "dbt_cloud", "login": "12345", "password": "<service token>", "host": "cloud.getdbt.com"}'

cloud.getdbt.com is the default host. Accounts on another region or on a dedicated cell use the host shown in their dbt Cloud account settings.

3. Run the job:

from airflow.providers.dbt.cloud.operators.dbt import DbtCloudRunJobOperator

run_dbt_job = DbtCloudRunJobOperator(
    task_id="run_dbt_job",
    dbt_cloud_conn_id="dbt_cloud_default",
    job_id=123456,
    check_interval=60,
    timeout=2 * 60 * 60,
    deferrable=True,
)

Pass the run date, or replace the job’s commands for this run:

run_dbt_job = DbtCloudRunJobOperator(
    task_id="run_dbt_job",
    job_id=123456,
    steps_override=["dbt build --select tag:daily --vars '{run_date: \"{{ ds }}\"}'"],
    deferrable=True,
)

Find the job by name instead of ID:

run_dbt_job = DbtCloudRunJobOperator(
    task_id="run_dbt_job",
    project_name="Shop",
    environment_name="Production",
    job_name="Daily build",
)

Waiting for the job

Other options

Checked against the code of the dbt Cloud provider 4.9 on Airflow 3.3.2; not run against a live dbt Cloud account.

References