Skip to main content

Using resources in projects

info

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.

Prerequisites

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:

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:

src/my_project/defs/resources.py
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:

src/my_project/defs/assets.py
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:

src/<project_name>/defs/resources.py
import dagster as dg


@dg.definitions
def resources() -> dg.Definitions:
return dg.Definitions(resources={})

and you can fill out the resource dictionary as needed.