Sub topic 12
Defining resources with Asset Bundles
This chapter assumes you can run Databricks CLI commands, are authenticated to a workspace, and know what a job, a task, and a cluster are. It covers how to describe a job as code in a bundle. The next chapter covers validating, deploying, and running it.
What a bundle is
A Databricks Asset Bundle is a project directory with a databricks.yml file at its root. The file declares the resources you want in a workspace, such as jobs and pipelines, and the targets where they should be deployed. Because the definition is plain text, you can keep it in Git, review changes, and deploy the same definition to several workspaces.
The CLI reads databricks.yml and turns it into API calls. You describe the desired state, and the CLI creates or updates the resources to match it.
The top-level sections
The example below declares one notebook job and a 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>| Section | Purpose |
|---|---|
| `bundle` | Names the bundle. The name is required. |
| `variables` | Declares values that differ between targets or runs. |
| `resources` | Declares the jobs, pipelines, and other objects to manage. |
| `targets` | Declares deployment environments, such as `dev` and `prod`. |
Resources
Under resources, each resource type has its own key, such as jobs. Below that, each resource has a key of your choice. Here the key is demo_job. The bundle uses this key to track the resource between deployments, so renaming it makes the CLI treat the job as a new one.
The fields inside a job mirror the Jobs API. A job has a name and a list of tasks. Each task needs a unique task_key and a task type, here notebook_task. The notebook_path is relative to the file that contains it, so the notebook travels with the bundle.
Variables
${var.cluster_id} is a reference to the variable declared in variables. The variable has no default, so you must supply a value when you deploy, for example with --var cluster_id=<cluster-id>. This keeps environment-specific IDs out of the file.
Targets
A target selects where and how the bundle is deployed. In dev, mode: development marks the deployment as a development copy. It prefixes resource names with your user name and pauses schedules, which prevents accidental clashes with production. default: true means commands use this target when you omit -t.
A diagram of the structure
Common errors
- A missing
bundle.nameor a mistyped key makes the configuration invalid. - An unset variable with no default causes an error at validation or deployment.
- A wrong indentation level in YAML moves a field to the wrong parent, so check nesting first.
Exercise
In the example bundle, what happens if you rename the job's resource key from demo_job to report_job and deploy again, and why is cluster_id not hard-coded in the file?
Show answer
The bundle tracks resources by key, so the CLI treats report_job as a new resource rather than renaming the existing job. The cluster_id is a variable so the file stays free of environment-specific values and you can supply a different ID for each target or deployment.