Add resources to a Databricks app
Your Databricks apps can integrate with various Databricks platform features, such as Databricks SQL for querying data, Databricks Jobs for data ingestion and processing, Mosaic AI Model Serving to access generative AI models, and Databricks secrets for managing sensitive information. In the context of apps, these platform features are referred to as resources.
To keep apps portable and secure, avoid hardcoding resource IDs. For example, instead of embedding a fixed SQL warehouse ID in your app.yaml
file, configure the SQL warehouse as a resource through the Databricks Apps UI or in databricks.yaml
.
Apps run with least privilege and rely on existing resources within the Databricks platform. When deployed, the app’s service principal accesses these resources and must have the necessary permissions, such as table-level access for SQL queries or read access to secret. See Configure authorization in a Databricks app.
Configure resources for your app
Resources allow your app to securely connect to services that it depends on, without hardcoding sensitive or environment-specific values.
- Databricks UI
- Databricks.yml
Add most resources directly in the Apps UI when you create or edit your app.
In the App resources section, click + Add resource and select the resource type you want to use. You assign a key to each resource, which you reference in your app.yaml
file.
Manually add resources in the resources
section of your databricks.yml
configuration file. This method gives you more flexibility for advanced configurations.
resources:
sql_warehouses:
sql_warehouse: # resource key
name: my-warehouse
secrets:
secret: # resource key
scope: my-scope
key: my-key
You can then reference these resources in the app configuration using the valueFrom
field.
Use environment variables to access resources
After you define your app resources, 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:
import os
warehouse_id = os.getenv["WAREHOUSE_ID"]
secret_value = os.getenv["SECRET_KEY"]
To learn more about managing Databricks secrets, see Manage secrets.