Hi, I'd like to pass dbt variables to dbt_cli_reso...
# ask-community
n
Hi, I'd like to pass dbt variables to dbt_cli_resource based on Dagster run_id. Run_id is a runtime variable and context is not available at declaration time. What is the best way to accomplish it? Code mockup for my definitions: vars = get_dbt_vars(context.run_id) DBT_CONFIG = {"project_dir": DBT_PROJECT_DIR, "profiles_dir": DBT_PROFILES_DIR, "target": DBT_TARGET, vars: vars} resource_def = { "dbt": dbt_cli_resource(DBT_CONFIG) }
r
What command are you trying to run using the dbt resource?
When invoking the resource from inside the context of either an asset or an op, the methods take in
kwargs
that can be used to override the default parameters of the resource.
so you could do something like this:
Copy code
@op(required_resource_keys={"dbt})
def my_dbt_op(context):
    context.dbt.cli("run", vars=get_dbt_vars(context.run_id))
n
I don't define ops explicitly, they are created by
load_assets_from_dbt_project()
afterwards materialization is triggered by freshness sensor, so I have no explicit declaration of assets and ops
So far I just made my own resource
custom_dbt_cli_resource(context)
which is exact copy of
dbt_cli_resource
with additional call to
vars = get_dbt_vars(context.run_id)
since context is available when resource is initialized. It is not pretty solution, thus looking for better way
r
ah i see. I think your solution is fine for the time being. to support arbitrary kwargs to the underlying
op
would require altering the existing interface for
load_assets_from_dbt_project
to accept some sort of mapping function from
context
to your desired
kwargs
, which also doesn’t seem pretty
n
Thank you!