Sub topic 12
Defining resources with Asset Bundles
This chapter assumes you can use a shell, read YAML and JSON, and know what a Databricks job, task, and cluster are. You should also have a working CLI profile from the earlier chapters. Here you will learn how a Databricks Asset Bundle describes resources in files, so that deployments come from version control and not from clicks in the UI.
What a bundle is
A Databricks Asset Bundle is a project directory with a databricks.yml file at its root. The file declares your resources, such as jobs, and the environments they deploy to. The CLI reads the file and creates or updates those resources in a workspace.
The calls in the previous chapters were imperative: you told the workspace what to do, one step at a time. A bundle is declarative. You describe the end state, and databricks bundle deploy works out what must change to reach it.
This gives you three practical benefits:
- The job definition lives in Git, so every change has an author, a diff, and a review.
- The same definition deploys to several targets, such as dev and prod, without copy-pasting.
- A CI pipeline can run the same commands you run locally.
The top-level structure
The main file has a small set of top-level keys.
| Key | Purpose |
|---|---|
| `bundle` | Names the bundle. The name is required. |
| `variables` | Declares values that can change between deployments. |
| `resources` | Declares the jobs, pipelines, and other objects to manage. |
| `targets` | Defines named environments, each of which can override settings. |
| `include` | Lists extra YAML files to merge into the configuration. |
Other keys exist, such as workspace and artifacts, but these five cover most bundles.
A minimal bundle
This is the file used throughout this tutorial. It declares one notebook job and one development target.
bundle:
name: demo_bundle
variables:
cluster_id:
description: ID of an existing cluster to run the job on
resources:
jobs:
demo_job:
name: demo_job
tasks:
- task_key: main
existing_cluster_id: ${var.cluster_id}
notebook_task:
notebook_path: ./demo_notebook.ipynb
targets:
dev:
mode: development
default: true
workspace:
host: https://<workspace-host>Replace <workspace-host> with your workspace URL. The notebook file demo_notebook.ipynb must sit next to databricks.yml, because the path is relative to the file that mentions it.
How resources are described
Under resources, each resource type has its own key, such as jobs. Beneath it, each resource gets a resource key. In the example the key is demo_job. The key is the identity the bundle uses to track the object. It is not the display name.
This distinction matters. The name field (demo_job) is what people see in the workspace. The resource key is what the bundle uses to remember which workspace object it created. If you rename the key, the bundle treats it as a new resource, and the next deploy can create a second job and remove the first.
The fields inside a job mirror the Jobs API you called with curl earlier. tasks, task_key, and notebook_task have the same meaning as in the JSON body of a job definition. If you know the API payload, you already know most of the YAML. The main difference is that the bundle supports references and merging that raw JSON does not.
Variables
A variable makes a value configurable. The example declares cluster_id with a description but no default. You reference a variable with ${var.cluster_id}.
A variable without a default must receive a value at deploy time. You can pass it on the command line with --var cluster_id=<cluster-id>. You can also set it as an environment variable named BUNDLE_VAR_cluster_id, which is convenient in CI. A target can also set it under a variables key of its own, which is how you give dev and prod different clusters.
If you forget the value, bundle validate reports the missing variable instead of failing halfway through a deploy.
Targets and modes
A target is a named deployment environment. It can override most settings in the top-level file. The example has one target, dev, marked default: true so that commands work without -t.
The mode: development setting changes how the bundle deploys:
- Deployed resources get a
[dev <your-user-name>]prefix, so your copy does not collide with a teammate's. - Job schedules and triggers are paused, so a test deploy does not start runs on its own.
- Files are placed under your own user folder.
A production target usually sets mode: production and a root_path that is not tied to one person. In production mode the CLI checks more settings, for example that the deploying identity is explicit. You should confirm the exact checks in the current documentation, because they change between CLI releases.
Each target names the workspace it deploys to through workspace.host. This is why two targets can point at different workspaces from a single file.
Where files go
On deploy, the CLI uploads your notebooks and other files to a folder in the workspace. By default this is under your user directory, in a path built from the bundle name and the target name. It also stores deployment state there, which is how the next deploy knows what already exists.
That state is why you should not edit a bundle-managed job in the UI. The next deploy overwrites the change, because the YAML is the source of truth. This is the most common surprise for people who move from UI work to bundles.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `bundle validate` says a variable has no value | A variable with no default was not supplied | Pass `--var name=value` or set `BUNDLE_VAR_name` |
| A second job appears after a rename | The resource key changed | Keep the key stable and change only `name` |
| Notebook not found | The path is relative to the wrong file | Make the path relative to the YAML file that declares it |
| Wrong workspace is targeted | The default target or `workspace.host` is not what you expected | Pass `-t` explicitly and check `host` |
| UI edits disappear | The bundle redeployed and overwrote them | Make the change in YAML instead |
Splitting a large bundle
A single file gets long once you add several jobs. The include key merges other files in.
bundle:
name: demo_bundle
include:
- resources/*.ymlEach included file holds a resources block. The CLI combines them into one configuration, so you can keep one file per job. Paths in include are relative to the main file.
Checking your work
You do not need a workspace to catch most mistakes. Running databricks bundle validate -t dev parses the files, resolves variables, and reports schema errors. The next chapter covers validate, deploy, and run in detail. For now, treat validate as the quick check you run after every edit to databricks.yml.
Exercise
You rename the resource key demo_job to nightly_job in databricks.yml and leave the name field unchanged, then deploy. What does the bundle most likely do, and how should you have made this change?
Show answer
The bundle tracks resources by key, so it sees nightly_job as a new resource. It creates a new job and removes the one tracked under demo_job, so job history and the job ID are lost. To change only the visible label, edit the name field and keep the resource key stable.
Exercise
A teammate fixes a job's timeout in the workspace UI. The next day your CI pipeline deploys the bundle and the timeout reverts. Why, and what is the correct way to make the change last?
Show answer
The bundle treats the YAML as the source of truth and overwrites the job on deploy, so the UI edit is lost. Make the change in databricks.yml, commit it, and deploy.