Sub topic 4
Defining a job as a bundle resource
This chapter assumes you can read YAML, know what a Databricks job and a job cluster are, and have a databricks.yml like the one in the previous chapter. You will define one job in its own file and see how it connects to the bundle.
Where resources live
A bundle describes workspace objects under the top-level resources mapping. Each resource type has its own key, such as jobs or pipelines. Under a type, each resource gets a name you choose. That name is the resource key. The bundle uses it to track the object between deployments.
The root databricks.yml pulls in other files with include:
include:
- resources/*.ymlEvery file matched by the glob is merged into the bundle configuration. A job file therefore only needs to contain a resources block.
A complete job definition
Save this 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 fields follow the Jobs API. If you know the job settings in the UI or the REST API, the names will look familiar.
daily_etl(the key underjobs) is the bundle's identifier for the job. You use it indatabricks bundle run.nameis the display name in the workspace. It can differ from the key.job_clustersdefines clusters that exist only for this job. A task selects one withjob_cluster_key.tasksis a list. Each task needs a uniquetask_key.notebook_taskpoints at the code to run and passes parameters.
Paths are relative to the file
The notebook_path is ../src/ingest.py. Bundle resolves relative paths from the location of the configuration file that contains them, not from the project root. Because the job file sits in resources/, .. moves up to the project root and src/ingest.py is found there. When you deploy, the CLI uploads the file to the workspace and rewrites the path to the uploaded location.
If you move the YAML file to another folder, update the relative path to match.
Variables inside the job
${var.node_type_id} and ${var.catalog} are substitutions. The CLI replaces them with the value for the target you deploy to. The job file stays the same across targets while the values change. Node types differ by cloud, so keeping node_type_id as a variable avoids editing the job when you change workspaces.
You can also reference built-in values such as ${bundle.target} in names. This is useful for telling deployed copies apart:
resources:
jobs:
daily_etl:
name: daily_etl_${bundle.target}In development mode the CLI already prefixes job names with [dev <your-user>] and pauses schedules, so you rarely need to do this by hand for dev.
Check what you wrote
Run validation before you deploy:
databricks bundle validate -t devValidation loads all included files, resolves variables, and reports schema errors. Common failures at this stage:
- Unresolved variable. A variable with no
defaultand no value for the target. Set it undertargets.<name>.variablesor pass--var. - Duplicate resource key. Two files define
jobs.daily_etl. The bundle merges them instead of failing, which can produce surprising settings. Keep keys unique. - Wrong relative path. The notebook path is correct from the project root but not from the YAML file's folder.
- Missing
task_keyor compute. Each task needs a key, and a task needs a cluster reference or serverless compute.
Exercise
Exercise
A job file at resources/dailyetl.yml uses notebookpath: src/ingest.py, but the notebook is at src/ingest.py in the project root. Validation or deployment cannot find the notebook. What is the fix, and why?
Show answer
Change the path to ../src/ingest.py. Relative paths are resolved from the folder of the YAML file that contains them, which is resources/, so ".." is needed to reach the project root.