Creating a Dagster resource to run dbt
Our next step is to define a Dagster resource as the entry point used to run dbt commands and configure its execution.
The DbtCliResource is the main resource that you’ll be working with. In later sections, we’ll walk through some of the resource’s methods and how to customize what Dagster does when dbt runs.
💡 Resource refresher: Resources are Dagster’s recommended way of connecting to other services and tools, such as dbt, your data warehouse, or a BI tool.
Navigate to the defs/resources.py, which is where other resources are defined. Copy and paste the following code to their respective locations:
# src/dagster_and_dbt/defs/resources.py
from dagster_dbt import DbtCliResource
from dagster_and_dbt.defs.project import dbt_project
# the import lines go at the top of the file
# this can be defined anywhere below the imports
dbt_resource = DbtCliResource(
project_dir=dbt_project,
)
defs = dg.Definitions(
resources={
"database": database_resource,
"dbt": dbt_resource,
},
)
The code above:
- Imports the
DbtCliResourcefrom thedagster_dbtpackage that we installed earlier - Imports the
dbt_projectrepresentation we just defined - Instantiates a new
DbtCliResourceunder the variable namedbt_resource - Tells the resource that the dbt project to execute is the
dbt_project - Imports the resource mappings so it can be automatically loaded by the
Definitions