Subchapter 2.104
references/env-vars.mdMarkdown3 KBView on GitHub
Environment variables provide a clean separation between configuration and code, allowing the same Dagster project to work across different environments (development, staging, production) without code changes.
The dg CLI automatically loads environment variables from .env files in your project root before executing commands. This means:
export or source commands neededdg launch, dg dev, dg check defs automatically use .env valuesUse the dg list envs command to see what environment variables your Dagster definitions reference:
dg list envsThis shows all EnvVar references in your code, helping you identify what needs to be configured.
# .env (at project root, never commit to git)
DATABASE_URL=postgresql://localhost:5432/mydb
API_KEY=secret_key_here
SNOWFLAKE_ACCOUNT=myaccount
SNOWFLAKE_USER=myuser
SNOWFLAKE_PASSWORD=secretImportant: Add .env to your .gitignore to prevent committing secrets.
import dagster as dg
class DatabaseResource(dg.ConfigurableResource):
connection_string: str = dg.EnvVar("DATABASE_URL")
timeout: int = dg.EnvVar.int("DB_TIMEOUT")
# Dagster automatically loads DATABASE_URL and DB_TIMEOUT from .env# defs/my_component/defs.yaml
component: dagster_sling.SlingReplicationComponent
params:
sling:
connections:
- name: MY_POSTGRES
type: postgres
host: "{{ env.DB_HOST }}"
port: "{{ env.DB_PORT }}"
database: "{{ env.DB_NAME }}"For multiple environments, use separate .env files:
my_project/
├── .env # Local development (gitignored)
├── .env.example # Template (committed to git)
├── .env.staging # Staging config (gitignored)
└── .env.prod # Production config (gitignored)# .env.example (committed to git)
DATABASE_URL=postgresql://localhost:5432/mydb
API_KEY=your_api_key_here
SNOWFLAKE_ACCOUNT=your_account
SNOWFLAKE_USER=your_user
SNOWFLAKE_PASSWORD=your_passwordDevelopers copy .env.example to .env and fill in real values.
# Development (uses .env by default)
dg dev
# Staging
dg launch --env-file .env.staging --assets my_asset
# Production
dg launch --env-file .env.prod --assets my_asset.env files - Always add to .gitignore.env.example - Template for required variablesSNOWFLAKE_ACCOUNT not ACCTSNOWFLAKE_*, S3_*)dg list envs - Verify all environment variables are configureddg list envs command