Sub topic 6
Variables and per-target overrides
This chapter assumes you can read YAML, you have a databricks.yml with at least one job resource, and you know what a target is. If targets are new to you, review the previous chapter first. Here you will learn how to parameterize a bundle so that one set of resource files can serve dev and prod without copy-and-paste.
Why variables exist
A bundle describes resources such as jobs and clusters. Some values in those resources differ between environments. Examples are the catalog a job writes to, the node type of a cluster, or an email address for alerts. If you hard-code these values, you end up with one file per environment, and the files drift apart.
A bundle variable is a named value that you declare once and reference from anywhere in the bundle configuration. Each target can then supply its own value. The resource files stay the same, and only the values change.
Declaring variables
Variables are declared under the top-level variables mapping in databricks.yml. Each variable can have a description, a default, and optionally a type.
variables:
catalog:
description: Unity Catalog catalog the job writes to
default: dev_catalog
node_type_id:
description: Cloud-specific node type for job clustersIn this example, catalog has a default and node_type_id does not. A variable with no default must receive a value from somewhere else before you deploy. Node type names differ between clouds, so the bundle leaves that choice to each target.
| Field | Purpose | Required |
|---|---|---|
| `description` | Explains the variable to people reading the file and to `bundle validate` error output | No, but recommended |
| `default` | The value used when nothing else sets the variable | No |
| `type` | Set to `complex` when the value is a mapping or list rather than a string | No |
Referencing variables
You reference a variable with the substitution syntax ${var.<name>}. The reference can appear in databricks.yml or in any file brought in by include.
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 CLI resolves each ${var.catalog} and ${var.node_type_id} when it reads the configuration, before it sends anything to the workspace. The deployed job contains the literal values.
Other substitutions
Variables are one kind of substitution. The same ${...} syntax also reads built-in values about the bundle. You saw one in the production target's root_path:
root_path: /Workspace/Shared/.bundle/${bundle.name}/${bundle.target}Here ${bundle.name} is the bundle name and ${bundle.target} is the name of the target being deployed. Using ${bundle.target} in paths keeps the dev and prod copies of a bundle from overwriting each other.
You can also reference the attributes of other resources, for example the ID of a job defined elsewhere in the bundle. These references follow the path of the resource in the configuration, such as ${resources.jobs.daily_etl.id}.
Per-target overrides
Each target can contain a variables mapping. Values set there replace the defaults for that target only.
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 the placeholders with real values before you deploy. The result for each target is:
| Variable | `dev` target | `prod` target |
|---|---|---|
| `catalog` | `dev_catalog` (the default) | `prod_catalog` (override) |
| `node_type_id` | Value set in the dev target | Value set in the prod target |
The dev target does not set catalog, so it falls back to the default. The prod target overrides it. Because node_type_id has no default, both targets must set it, or you must supply it another way.
How a value is chosen
A variable can receive its value from several places. When more than one applies, the most specific source wins. From highest to lowest priority, the order is:
- The
--varoption on the command line. - An environment variable named
BUNDLE_VAR_<name>. - A value set in the
variablesmapping of the selected target, or in a variables file. - The
defaultdeclared at the top level.
If none of these provides a value, the CLI reports an error and the bundle does not deploy.
Overriding at the command line
Use --var to change a value for a single invocation:
databricks bundle deploy -t prod --var="catalog=prod_catalog"You can pass the option more than once, or separate several assignments with commas, to set several variables. This is useful in CI/CD, where a pipeline step may know a value that the configuration does not, such as a build number or a branch-specific schema name.
Overriding with environment variables
Set an environment variable named BUNDLE_VAR_ followed by the variable name:
export BUNDLE_VAR_catalog=staging_catalog
databricks bundle validate -t devThis form is convenient in CI systems, because secrets and environment-specific values are often already exposed as environment variables. The variable name is case-sensitive, so BUNDLE_VAR_catalog sets catalog.
Complex variables
Not every value is a single string. A cluster definition, for example, is a mapping with several fields. Declare such a variable with type: complex and put the structure under default:
variables:
etl_cluster:
description: Shared cluster settings for ETL jobs
type: complex
default:
spark_version: 15.4.x-scala2.12
node_type_id: <a-node-type-available-in-your-cloud>
num_workers: 2You then reference it as a whole, in the place where the mapping belongs:
job_clusters:
- job_cluster_key: main
new_cluster: ${var.etl_cluster}A target can replace the full structure in its own variables mapping. This keeps cluster sizing in one place and lets prod use a larger cluster without touching the job file.
Overriding more than variables
Variables are not the only thing a target can change. A target can also override parts of the top-level configuration directly, such as workspace settings and entries under resources. Target settings are merged into the top-level settings. Mappings are merged key by key, and a value set in the target replaces the same key at the top level.
Use a variable when the same value appears in several places, or when it is something you want to change from the command line. Use a direct override when a setting only makes sense for one target, such as a schedule that should run only in prod.
| Approach | Best for | Drawback |
|---|---|---|
| Variable | A value reused in several places, or set from CLI or CI | Adds a name to track, and every consumer must use `${var.name}` |
| Direct target override | A setting that differs in one target only | The difference is hidden in the target block, away from the resource definition |
Common errors
- No value for a variable. A variable without a default is referenced, and neither the target,
--var, norBUNDLE_VAR_<name>supplies it. Rundatabricks bundle validate -t <target>to catch this before deploying. - Wrong target. You override
cataloginprodbut validate or deploydev. Thedevtarget uses the default. Check the target name with-t, or note that the target markeddefault: trueis used when you omit-t. - Typo in the reference.
${var.catlog}refers to a variable that does not exist. The CLI reports an unknown variable when it resolves the configuration. - Using
${var.x}where substitution is unsupported. Variables are not valid in a few top-level places such as the names of targets or variables themselves. Keep them in values. - Quoting. A value that YAML would read as a number or boolean, such as
trueor007, can change type. Quote it if you need a string. - Node types that do not exist. The node type name is cloud-specific. A name valid on one cloud fails on another, which is why this tutorial leaves
node_type_idwithout a default.
Inspecting the resolved configuration
To see what a target looks like after substitution, validate it and ask for the full output:
databricks bundle validate -t prod -o jsonThe JSON output shows each resource with its variable references already replaced. Compare the output for dev and prod to confirm that each target receives the values you expect. Doing this before the first production deploy is cheap and catches most mistakes in this chapter.
Summary of the workflow
- Declare each environment-dependent value under
variables, with a description and, when a safe default exists, a default. - Reference the variable with
${var.<name>}in resource files. - Override values in the
variablesmapping of each target. - Use
--varorBUNDLE_VAR_<name>for values that only the pipeline or the developer knows. - Run
databricks bundle validate -t <target> -o jsonand read the resolved result before you deploy.
Exercise
A bundle declares the variable catalog with default: dev_catalog. The prod target sets catalog: prod_catalog. The environment variable BUNDLE_VAR_catalog=staging_catalog is exported in your shell. You run databricks bundle deploy -t prod --var="catalog=release_catalog". Which catalog does the job receive, and which catalog would it receive if you ran the same command without --var?
Show answer
With --var, the job receives release_catalog, because the command-line option has the highest priority. Without --var, it receives staging_catalog, because the BUNDLE_VAR_catalog environment variable outranks the target's value of prod_catalog and the default of dev_catalog. This is a reason to check your shell for stray BUNDLE_VAR_ variables before a production deploy.
Exercise
The dev target should run the job with a small cluster of 2 workers, and the prod target with 8 workers. Name two ways to achieve this and say which you would choose if the worker count is referenced in three different jobs.
Show answer
One way is to declare a variable such as num_workers, reference it with ${var.num_workers} in each job, and set different values in each target's variables mapping. The other is to override the num_workers field directly in the prod target's resources section for each job. With three jobs, the variable is the better choice, because you change one value per target instead of editing three overrides.