Skip to main content

Define environment variables in a Databricks app

Databricks automatically sets certain environment variables in the app runtime environment. These variables provide essential information about the app and workspace, and are accessible to all Databricks apps by default. For a list of default variables, see Databricks Apps system environment.

If your app requires additional environment variables, define them in the app.yaml configuration file in the env section. Each variable requires a name and a value. Variables can use a hardcoded value or reference an external source.

For example:

YAML
env:
- name: LOG_LEVEL
value: 'debug'

Hardcoding values like this is safe when the value is static, non-sensitive, and consistent across environments. Other safe examples include value: "true" for feature toggles, value: "us-west" for fixed regions, or value: "UTC" for default timezones.

important

To keep your app secure and portable, never reference secret keys or other sensitive values directly in your app configuration. For example, avoid embedding secret values in the value field of an environment variable or directly in your source code. Instead, use the valueFrom field to securely reference secrets and other managed resources defined in your resources block. This ensures secrets are retrieved from Databricks at runtime and are never exposed in plaintext in your configuration files.

Use environment variables to access resources

If you define app resources, such as SQL warehouses or secrets, reference these resources in the env section of your app.yaml file using the valueFrom field. This connects environment variables in your app to the resource keys defined in resources.

Example app.yaml snippet:

YAML
env:
- name: WAREHOUSE_ID
valueFrom: sql_warehouse

- name: SECRET_KEY
valueFrom: secret

Then, in your app code, access them as environment variables:

Python
import os

warehouse_id = os.getenv("WAREHOUSE_ID")
secret_value = os.getenv("SECRET_KEY")

To learn more about managing Databricks secrets, see Manage secrets.