Using environment variables and secrets in Dagster code
Environment variables, which are key-value pairs configured outside your source code, allow you to dynamically modify application behavior depending on environment.
Using environment variables, you can define various configuration options for your Dagster application and securely set up secrets. For example, instead of hard-coding database credentials - which is bad practice and cumbersome for development - you can use environment variables to supply user details. This allows you to parameterize your pipeline without modifying code or insecurely storing sensitive data.
Declaring environment variables
How environment variables are declared depends on whether you're developing locally or have already deployed your Dagster project.
- Local development
- Dagster open source
Local development
As of Dagster 1.1.0, using .env
files is supported for loading environment variables into local environments. A .env
file is a text file containing key-value pairs that is used locally, but not checked into source control. Using a .env
file allows you to develop and test locally without putting sensitive info at risk. For example:
# .env
DATABASE_NAME=staging
DATABASE_SCHEMA=sales
DATABASE_USERNAME=salesteam
DATABASE_PASSWORD=supersecretstagingpassword
If Dagster detects a .env
file in the same folder where dagster-webserver
or dagster-daemon
is launched, it will automatically load the environment variables in the file. This also applies to variables exported from Dagster+
When using a .env
file, keep the following in mind:
- The
.env
file must be in the same folder wheredagster-webserver
ordagster-daemon
is launched - Any time the
.env
file is modified, the workspace must be re-loaded to make the Dagster webserver/UI aware of the changes
Dagster+
Environment variables can be set a variety of ways in Dagster+:
- Directly in the UI
- Via agent configuration (Hybrid deployments only)
If using the UI, you can also export locally-scoped variables to a .env
file, which you can then use to develop locally.
Refer to the Dagster+ environment variables guide for more info.
Dagster open source
How environment variables are set for Dagster projects deployed on your infrastructure depends on where Dagster is deployed. Refer to the deployment guide for your platform for more info:
Accessing environment variables
In this section, we'll demonstrate how to access environment variables once they've been declared. There are two ways to do this:
- In Python code, which isn't specific to Dagster
- From Dagster configuration, which incorporates environment variables into the Dagster config system
In Python code
To access environment variables in your Dagster code, you can use os.getenv
:
import os
database_name = os.getenv("DATABASE_NAME")
This approach also works for accessing built-in environment variables for Dagster+:
import os
deployment_name = os.getenv("DAGSTER_CLOUD_DEPLOYMENT_NAME")
For a real-world example, see the Dagster+ branch deployments example.
You can also call the get_value()
method on the EnvVar
:
from dagster import EnvVar
database_name = EnvVar('DATABASE_NAME').get_value()