Sub topic 3
Anatomy of databricks.yml
This chapter explains each top-level section of databricks.yml, the file that defines a Databricks Asset Bundle. It assumes you have read the earlier chapters, so you know what a bundle is and have authenticated the Databricks CLI. It also assumes you can read YAML and know what a Databricks job and a Unity Catalog catalog are.
The file is the bundle root
Every bundle has exactly one databricks.yml file in its root directory. The CLI looks for this file in the current directory, so you run bundle commands from the folder that contains it. Paths in the file are resolved relative to the file they are written in, not relative to where you run the command.
The file holds configuration, not code. Notebooks, Python files, and wheels stay in their own folders. The YAML only points at them.
The top-level keys
A databricks.yml is a YAML mapping. These top-level keys are the ones you will meet most often:
| Key | Purpose | Required |
|---|---|---|
| `bundle` | Names the bundle and holds bundle-wide settings | Yes (`name`) |
| `variables` | Declares named values that you can reuse and override | No |
| `include` | Lists other YAML files to merge into the configuration | No |
| `resources` | Declares jobs, pipelines, and other workspace objects | No |
| `targets` | Declares deployment environments such as dev and prod | No, but practically always used |
| `workspace` | Sets the host, root path, and related workspace settings | No |
| `artifacts` | Describes build steps, such as building a wheel | No |
| `permissions` | Grants access to the deployed resources | No |
| `run_as` | Sets the identity that jobs and pipelines run as | No |
Only bundle.name is strictly required. In practice, a bundle that deploys anything needs resources, and a bundle that serves more than one environment needs targets.
A complete example
This is the root file from a small project. It declares the bundle, two variables, an include pattern, and two targets.
bundle:
name: my_project
variables:
catalog:
description: Unity Catalog catalog the job writes to
default: dev_catalog
node_type_id:
description: Cloud-specific node type for job clusters
include:
- resources/*.yml
targets:
dev:
mode: development
default: true
workspace:
host: https://<your-dev-workspace-url>
variables:
node_type_id: <a-node-type-available-in-your-cloud>
prod:
mode: production
workspace:
host: https://<your-prod-workspace-url>
root_path: /Workspace/Shared/.bundle/${bundle.name}/${bundle.target}
variables:
catalog: prod_catalog
node_type_id: <a-node-type-available-in-your-cloud>
run_as:
service_principal_name: <service-principal-application-id>Replace each angle-bracket placeholder with a real value before you validate. The CLI does not fill them in for you.
The bundle section
The bundle mapping identifies the project. The name value becomes part of the deployment path in the workspace and the prefix of deployed resource names in development mode. Pick a name that is stable. Renaming a bundle later means the CLI treats it as a new project, and the old deployment stays behind until you destroy it.
Other optional keys under bundle include databricks_cli_version, which constrains the CLI version that may deploy the bundle. This helps a team avoid surprises when one person has an older CLI.
The variables section
Each entry under variables declares a name and, optionally, a description and a default. You refer to a variable elsewhere as ${var.catalog}.
In the example, catalog has a default, and node_type_id does not. A variable without a default must receive a value from somewhere, or the bundle is incomplete. The dev and prod targets both supply node_type_id, so the bundle resolves in either one.
A variable can get its value from several places. In order of precedence, from highest to lowest:
- The
--varoption on the command line, such as--var="catalog=prod_catalog". - An environment variable named
BUNDLE_VAR_plus the variable name, such asBUNDLE_VAR_catalog. - A value set in a target's
variablesmapping. - The
defaultin the top-level declaration.
This ordering lets you keep safe defaults in the file and still override a value for one run.
Substitutions
Anything written as ${...} is a substitution. The CLI resolves substitutions when it loads the configuration. The ones you will use most often are:
${var.<name>}for a variable value.${bundle.name}and${bundle.target}for the bundle name and the selected target.${workspace.file_path}and${workspace.root_path}for the paths of the deployed files.${resources.jobs.<key>.id}for a reference to another resource in the same bundle.
The root_path in the prod target above uses two of these. It produces /Workspace/Shared/.bundle/my_project/prod, a path that depends on the target and not on a person's user folder.
The include section
include lists files or glob patterns, relative to the root file. The CLI merges the contents of each matched file into the configuration as if you had written it in databricks.yml. The pattern resources/*.yml pulls in every YAML file in the resources folder.
Splitting files this way keeps the root file short. A common layout puts one resource per file:
my_project/
databricks.yml
resources/
daily_etl.yml
src/
ingest.pyTwo details matter here. First, a pattern that matches no files does not by itself make the bundle valid or useful, so check that your folder name and extension match. Second, paths inside an included file are relative to that file. A notebook at src/ingest.py is written as ../src/ingest.py from inside resources/daily_etl.yml.
The resources section
resources is where the deployable objects live. Each resource type has its own mapping, such as jobs or pipelines, and each resource has a key you choose. This file, saved as resources/daily_etl.yml, defines one job:
resources:
jobs:
daily_etl:
name: daily_etl
job_clusters:
- job_cluster_key: main
new_cluster:
spark_version: 15.4.x-scala2.12
node_type_id: ${var.node_type_id}
num_workers: 2
tasks:
- task_key: ingest
job_cluster_key: main
notebook_task:
notebook_path: ../src/ingest.py
base_parameters:
catalog: ${var.catalog}The key daily_etl is the bundle's own handle for the job. You use it in databricks bundle run daily_etl and in references like ${resources.jobs.daily_etl.id}. The name field is the display name in the workspace, and it can differ from the key. Changing the key later makes the CLI see a different resource.
The settings under a resource follow the corresponding Databricks API for that object. Because the job above takes node_type_id and catalog from variables, the same file works in every target.
The targets section
A target is a named environment. Each key under targets (here dev and prod) is a name you pass with -t, as in databricks bundle deploy -t prod.
A target can override or add:
workspace, including thehostof the workspace to deploy to.variables, to give variables a different value.mode, set todevelopmentorproduction.run_as, the identity that runs the deployed jobs.default: true, which marks the target used when you omit-t.
Keys set at the top level apply to all targets. Keys set inside a target are merged over them. For lists of named items, such as tasks and job_clusters, the merge matches items by their key (task_key or job_cluster_key), so a target can change one task without repeating the others.
What the modes do
In development mode, the CLI marks deployed resources as belonging to a developer. It prefixes resource names with [dev <your user name>] and pauses job schedules, so two developers can deploy the same bundle without colliding. In production mode, the CLI applies stricter checks to the configuration. For example, it expects a workspace root_path that is not tied to one person, and it expects you to be deliberate about the identity the jobs use. That is why the prod target above sets both root_path and run_as.
Checking what you wrote
The command databricks bundle validate loads the root file, merges the included files, applies the chosen target, resolves substitutions, and reports problems. Add -o json to see the fully resolved configuration, which is the best way to learn what the merge produced:
databricks bundle validate -t prod -o jsonLook for the final node_type_id, the final catalog, and the resolved root_path. If the output shows a value you did not expect, trace it back through the precedence list above.
Common errors
- A variable has no value. A declaration with no
defaultand no target override leaves the bundle incomplete. Supply a value in the target, with--var, or in aBUNDLE_VAR_environment variable. - A notebook path is wrong. Paths in included files are relative to that file. A missing
../is the usual cause. - The wrong target is used. If no target has
default: trueand you omit-t, the command may fail or pick an unexpected target. Pass-texplicitly in scripts. - Placeholders left in place. Strings such as
<your-dev-workspace-url>are not valid values. Replace them before validating. - Renamed keys. Changing a resource key or the bundle name creates a new deployment identity. Rename deliberately, and clean up the old one.
Exercise
A bundle declares a variable catalog with default: dev_catalog. The prod target sets catalog: prod_catalog. You run databricks bundle deploy -t prod --var="catalog=staging_catalog". Which catalog value does the deployed job receive, and why? Then, in a file resources/daily_etl.yml, how do you write the path to a notebook at src/ingest.py?
Show answer
The job receives staging_catalog. The --var option has the highest precedence, above the target's variables value and the declared default. The notebook path is written relative to the included file, so it is ../src/ingest.py.
Exercise
Name the only strictly required key in databricks.yml, and name the top-level key you would use to split a long file into several smaller ones.
Show answer
The required key is bundle.name, the name under the bundle mapping. The key for splitting the configuration is include, which lists files or glob patterns such as resources/*.yml.