แชร์ผ่าน


Define environment variables in a Databricks app

Azure 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 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:

env:
  - name: LOG_LEVEL
    value: 'debug'

Only hardcode values when they're static, non-sensitive, and consistent across environments. 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 Azure 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:

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")

JavaScript

const warehouseId = process.env.WAREHOUSE_ID;
const secretValue = process.env.SECRET_KEY;

valueFrom reference

The following table shows the value that valueFrom resolves to for each resource type:

Resource type Resolved value Example
Databricks app App name my-app
Genie space Space ID 01ef1fa2b3c45678
Lakebase Autoscaling database Endpoint path projects/my-project/branches/main/endpoints/ep123
Lakebase Provisioned database Host postgres-host.example.com
Lakeflow job Job ID 123456789
MLflow experiment Experiment ID 456789012
Model serving endpoint Endpoint name my-serving-endpoint
Secret Decrypted secret value (the secret value)
SQL warehouse Warehouse ID a1b2c3d4e5f67890
Unity Catalog connection Connection name my_connection
Unity Catalog table Table full name catalog.schema.table
Unity Catalog volume Volume path /Volumes/catalog/schema/volume
User-defined function Function full name catalog.schema.my_function
Vector search index Index full name catalog.schema.my_index

Next steps