Using resources in projects
dg
and Dagster Components are under active development. You may encounter feature gaps, and the APIs may change. To report issues or give feedback, please join the #dg-components channel in the Dagster Community Slack.
Before following this guide, you will need to create a project with the create-dagster
CLI.
Assets, asset checks, and sensors in Dagster frequently require resources that are instantiated elsewhere in the project.
For example, if you have created a new Dagster project with dg
called my_project
, you can define the resources at src/my_project/defs/aresource.py
:
import dagster as dg
class AResource(dg.ConfigurableResource): ...
You can then make that resource available anywhere else in your project by defining a @dg.Definitions
function:
from my_project.defs.aresource import AResource
import dagster as dg
@dg.definitions
def defs() -> dg.Definitions:
return dg.Definitions(
resources={"a_resource": AResource(name="foo")},
)
You can now use the resource elsewhere in your project:
from my_project.defs.aresource import AResource
import dagster as dg
@dg.asset
def asset_one(a_resource: AResource): ...
Scaffolding resources
To create a resource dictionary like the above, you can run the following:
dg scaffold defs dagster.resources resources.py
which will create:
import dagster as dg
@dg.definitions
def resources() -> dg.Definitions:
return dg.Definitions(resources={})
and you can fill out the resource dictionary as needed.