Sub topic 4
Defining a job as a bundle resource
This chapter assumes you can read YAML, know what a Databricks job, task and job cluster are, and have a bundle with a working databricks.yml from the previous chapter. You should also be able to run the Databricks CLI against a workspace. The chapter shows how to describe a job in a bundle, how the pieces connect, and which mistakes cause most validation failures.
Where resources live
A bundle is more than a name and some targets. The resources mapping holds the Databricks objects the bundle creates and manages. Jobs are one kind. Pipelines, models and dashboards live in the same mapping under their own keys.
The path to one job has three levels:
resources:
jobs:
daily_etl:
name: daily_etlresourcesis the top-level key.jobsis the resource type.daily_etlis the resource key. It is the identifier you use in the CLI and in references. It is not necessarily the job's display name.
The resource key and the name field are different things. Running databricks bundle run daily_etl looks up the resource key. The Jobs UI shows the name. If you leave out name, Databricks falls back to a default, so set it explicitly.
The job definition is the Jobs API payload in YAML
The fields under a job follow the Jobs API create request, written as YAML instead of JSON. If you can build a job in the UI, you can already describe it in a bundle. Open the job in the UI, choose the option to view it as code or JSON, and translate the fields.
This is the job from the topic plan. Save it as resources/daily_etl.yml:
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 sections below take it apart.
Job clusters
job_clusters is a list of cluster definitions that belong to this job. Each entry has a job_cluster_key, a label that tasks use to refer to it. Tasks that share a key share the same cluster during a run, so you pay for startup once.
Inside new_cluster, spark_version pins the Databricks Runtime and num_workers sets a fixed size. The node_type_id is not hard-coded. It is ${var.node_type_id}, a variable defined in databricks.yml. Node type names differ between clouds, so this keeps cloud-specific values out of the job definition.
Tasks
tasks is a list. Each task needs a unique task_key. The task here has three parts:
job_cluster_key: mainattaches it to the cluster above.notebook_tasksays what to run.base_parameterspasses values to the notebook as widgets.
Other task types exist, including spark_python_task, python_wheel_task, sql_task, pipeline_task and run_job_task. They follow the same pattern: a task key, compute, and one type-specific block.
Paths are relative to the file that declares them
The line notebook_path: ../src/ingest.py resolves relative to resources/daily_etl.yml, not to the project root and not to your working directory. The project layout is:
my_project/
databricks.yml
resources/
daily_etl.yml
src/
ingest.pyFrom resources/, .. goes up to the project root, and src/ingest.py goes back down. If you moved the job definition into databricks.yml itself, the path would become ./src/ingest.py.
During deployment the CLI uploads the files from your project to the workspace under the target's root path. It rewrites the notebook path in the job so it points at the uploaded copy. You never write a workspace path by hand.
A local .py file is treated as a notebook if its first line is a notebook marker. The matching notebook looks like this:
# Databricks notebook source
catalog = dbutils.widgets.get("catalog")
spark.sql(f"CREATE SCHEMA IF NOT EXISTS {catalog}.bronze")
print(f"Ingesting into {catalog}.bronze")The widget named catalog receives the value from base_parameters. In the dev target that is dev_catalog. In prod it is prod_catalog. The notebook code does not change between targets.
Variables and references
Two kinds of substitution appear in bundle files, and they are easy to confuse:
| Syntax | What it refers to | Example |
|---|---|---|
| `${var.name}` | A variable declared under `variables` | `${var.catalog}` |
| `${bundle.name}`, `${bundle.target}` | Built-in bundle properties | `${bundle.target}` |
| `${resources.jobs.key.id}` | A property of another resource in the bundle | `${resources.jobs.daily_etl.id}` |
The CLI resolves these when it loads the configuration. References to a deployed resource's id resolve at deploy time, after the object exists. That lets one resource point at another without you knowing its ID in advance.
Adding a second task and a schedule
Most real jobs have several tasks. Add a downstream task with depends_on, and give the job a schedule:
resources:
jobs:
daily_etl:
name: daily_etl
schedule:
quartz_cron_expression: "0 0 6 * * ?"
timezone_id: UTC
pause_status: PAUSED
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}
- task_key: transform
depends_on:
- task_key: ingest
job_cluster_key: main
notebook_task:
notebook_path: ../src/transform.py
base_parameters:
catalog: ${var.catalog}The schedule uses a Quartz cron expression with seconds first, so 0 0 6 * * ? means 06:00:00 every day. Setting pause_status: PAUSED deploys the schedule without enabling it. You can switch it to UNPAUSED in the prod target only.
transform waits for ingest to succeed. Both tasks use the main cluster.
Making the job differ by target
The job definition should stay the same across targets. Differences come from variables, which you set per target in databricks.yml. You can also override part of a job under a target when a difference cannot be expressed as a variable:
targets:
prod:
resources:
jobs:
daily_etl:
schedule:
pause_status: UNPAUSEDThe CLI merges this override into the base definition. Lists of tasks merge by task_key, and lists of job clusters merge by job_cluster_key. That is why those keys must be unique and stable. If you rename a task_key in only one place, you get a second task instead of an edit.
What changes in development mode
A target with mode: development changes how the job is deployed:
- The job name gets a prefix such as
[dev your_name], so several developers can deploy the same bundle without colliding. - Schedules and triggers are paused.
- The bundle files go to a per-user folder.
You do not need to edit the YAML for any of this. It is one reason to keep resource definitions free of dev-specific values.
Setting permissions on the job
A job you deploy is owned by the identity that deploys it. To share it, add a permissions list on the job:
resources:
jobs:
daily_etl:
name: daily_etl
permissions:
- group_name: data-engineers
level: CAN_MANAGE_RUN
- user_name: [email protected]
level: CAN_VIEWEach entry names one principal (user_name, group_name or service_principal_name) and a level. Common levels are CAN_VIEW, CAN_MANAGE_RUN and CAN_MANAGE. A later chapter covers permissions and run_as for production in more detail.
Checking your work
Use the CLI to check the definition before you deploy it:
databricks bundle validate -t dev
databricks bundle deploy -t dev
databricks bundle run -t dev daily_etlvalidate loads every included file, resolves variables and paths, and reports schema errors. deploy creates or updates the job. run starts it using the resource key.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `bundle run` cannot find `daily_etl` | You used the display `name` where the resource key is needed, or the file is not under `include` | Use the key under `resources.jobs`, and check the `include` globs |
| Notebook not found at deploy or run time | Path written relative to the project root instead of the declaring file | Count `..` segments from the YAML file's own folder |
| `variable node_type_id is not defined` or an empty value | Variable declared with no default and no target value | Add a value under the active target, or pass `--var` |
| Two tasks appear after an override | `task_key` spelled differently in the override | Keep keys identical so the merge matches them |
| Unknown field errors from `validate` | Wrong indentation or a field from a different task type | Check the nesting against the Jobs API field names |
| Job runs the old code | Edits were not deployed | Run `bundle deploy` again before `bundle run` |
Summary of the working model
- A job lives at
resources.jobs.<key>. The key is what the CLI and references use. - The fields mirror the Jobs API.
- Tasks and job clusters have keys, and those keys control both dependency wiring and merging.
- File paths resolve from the file that contains them.
- Anything that differs between environments should come from a variable or a target override.
Exercise
A teammate adds a target override under targets.prod.resources.jobs.daily_etl.tasks with a task whose task_key is Ingest (capital I), meaning to change its notebook parameters. After deploying, the prod job has three tasks instead of two. Why, and how do you fix it?
Show answer
Tasks in an override merge with the base definition by task_key, and keys are case-sensitive. Ingest does not match ingest, so the CLI adds a new task instead of editing the existing one. Change the override's key to exactly ingest, run databricks bundle validate -t prod and then databricks bundle deploy -t prod. The extra task is removed from the deployed job because the bundle now no longer defines it.
Exercise
In resources/daily_etl.yml, a task has notebook_path: ./src/ingest.py, and the notebook is at src/ingest.py in the project root. Validation or deployment fails to find it. What is wrong?
Show answer
The path resolves relative to the YAML file, which is in resources/. From there, ./src/ingest.py points to resources/src/ingest.py, which does not exist. Use ../src/ingest.py.