Sub topic 5
Targets: dev and prod from one bundle
A bundle describes one project. A target describes one place that project gets deployed. This chapter covers how targets let one set of YAML files produce a safe development copy and a locked-down production copy, and which settings each target can change.
This chapter assumes you can read YAML, you have a databricks.yml like the one from the earlier chapter on anatomy, and you can run databricks bundle validate against a workspace.
What a target is
A target is a named block under the top-level targets mapping in databricks.yml. The name is yours to choose, and dev and prod are common. Each target overrides or extends the settings declared at the top level of the bundle. Anything a target doesn't mention keeps its top-level value.
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>The job definition lives in resources/daily_etl.yml and appears only once. Both targets deploy the same job. The differences are where it goes, what values it receives, and who it runs as.
Selecting a target
Every lifecycle command accepts -t (or --target). If you leave it out, the CLI uses the target marked default: true. If no target is marked and you omit -t, the CLI can't decide for you, so give it a target explicitly.
databricks bundle validate -t dev
databricks bundle deploy -t prodMark only one target as default: true, and make it the least dangerous one. A typo-free databricks bundle deploy should never reach production.
What a target can override
The settings you will override most often:
| Setting | Purpose |
|---|---|
| `mode` | Applies a bundle of behaviors: `development` or `production`. |
| `default` | Makes this the target used when `-t` is omitted. |
| `workspace` | Sets `host`, `root_path`, and related deployment locations. |
| `variables` | Supplies values for variables declared at the top level. |
| `run_as` | Sets the identity that jobs and pipelines run as. |
| `resources` | Overrides fields of resources defined at the top level or in included files. |
| `permissions` | Grants access to the deployed resources. |
You can also use git settings and artifacts overrides, but the table above covers most real projects. Permissions, run_as, and artifacts get their own chapters.
How merging works
Target settings are merged into the top-level configuration, not substituted wholesale. The rules matter when a resource is overridden.
- Scalar values such as strings and numbers in the target replace the top-level value.
- Mappings are merged key by key, so you can change one field without repeating its siblings.
- Some lists merge by key. For jobs,
tasksmerge bytask_keyandjob_clustersmerge byjob_cluster_key. A target can change one task's field without restating the whole task list.
Here is a target that gives only the production copy of daily_etl a larger cluster and a schedule. The job itself still comes from resources/daily_etl.yml.
targets:
prod:
mode: production
workspace:
host: https://<your-prod-workspace-url>
root_path: /Workspace/Shared/.bundle/${bundle.name}/${bundle.target}
run_as:
service_principal_name: <service-principal-application-id>
resources:
jobs:
daily_etl:
job_clusters:
- job_cluster_key: main
new_cluster:
num_workers: 8
schedule:
quartz_cron_expression: "0 0 2 * * ?"
timezone_id: UTCThe job_cluster_key: main entry matches the cluster declared in the resource file, so only num_workers changes. spark_version and node_type_id remain as originally defined.
Run databricks bundle validate -t prod after writing an override like this. If the override doesn't show up, the key probably didn't match. Keys are case-sensitive, and a mismatched job_cluster_key adds a second cluster instead of editing the first.
Deployment modes
The mode setting changes how the CLI treats everything the target deploys.
Development mode
With mode: development, the CLI assumes you are iterating and making a personal copy:
- Deployed resource names get a prefix such as
[dev your_name], so your copy ofdaily_etldoesn't collide with a teammate's copy. - Resources are tagged as development resources.
- Job schedules and triggers are paused, so a dev deployment doesn't start running on its own.
- The default
root_pathis under your own user folder in the workspace, which keeps your files separate from other users'.
This is why many people can deploy the same bundle to one workspace without stepping on each other.
Production mode
With mode: production, the CLI doesn't add the name prefix, and it doesn't pause schedules. It also runs extra validation so you don't ship a personal deployment by accident. Typical checks cover whether the deployment path and identity settings look like a shared production setup. The exact set of checks depends on your CLI version, so read the errors validate prints and follow them.
Production mode expects you to make these choices explicitly:
- Set
workspace.root_pathto a shared location, not a user's home folder. - Set
run_asto a service principal so the jobs don't depend on one person's account. - Set
permissionsso the right groups can view or manage the deployed jobs.
Using neither mode
mode is optional. A target without it deploys resources exactly as declared. Teams sometimes use this for a staging target that should behave like prod but runs under a different host or variable set. Be aware that without mode: development, nothing prefixes names or pauses schedules for you.
Targets and workspaces
A target usually points at a workspace through workspace.host. Two layouts are common.
- One workspace per environment.
devandprodhave different hosts, as in the example above. Isolation is strong, and the CLI needs a profile or login for each host. - One workspace, several targets. Targets share a host and differ in
mode, variables, and paths. This is simpler and cheaper, but production and development share one workspace's access controls.
Where you deploy is separate from where data lives. The catalog variable controls which Unity Catalog catalog the job writes to, and a dev workspace could in principle write to a prod catalog if you let it. Keep dev permissions in Unity Catalog narrow, and don't rely on the bundle alone.
Authentication per target
Each host needs credentials. After databricks auth login --host <url>, the CLI stores a profile. A target can name that profile:
targets:
dev:
workspace:
host: https://<your-dev-workspace-url>
profile: devIf you set host and no profile, the CLI finds a matching profile by host. When two profiles match the same host, specify profile so the CLI doesn't have to guess.
Combining targets with variables
Targets and variables work together. Declare each variable once at the top level, give it a default if one makes sense, and override it per target. Values for a variable are resolved in this order, from highest priority to lowest:
- The
--varflag on the command line. - The
BUNDLE_VAR_<name>environment variable. - A value under the target's
variablesblock. - The variable's
defaultat the top level.
In the example, catalog defaults to dev_catalog, so the dev target doesn't mention it. The prod target sets prod_catalog. The node_type_id variable has no default, so every target must provide a value or the command fails with an error about an unset variable.
A one-off override looks like this:
databricks bundle deploy -t prod --var="catalog=prod_catalog"Use this form for experiments or CI parameters. For values that should be permanent, edit the target.
Common errors
- Deploying to the wrong target. Always type
-tin scripts and CI. Don't rely on the default. - A missing variable value. If a variable has no default and the chosen target doesn't set it,
validatefails. Add a value to the target or pass--var. - An override that doesn't apply. A resource key or list key that doesn't match the original creates a new entry or is ignored. Compare against the output of
validate --output json. - A production deploy rejected by validation. A user-specific
root_pathor a missingrun_asis the usual cause. Set both for the production target. - Unexpected paused schedules. A development-mode deployment pauses schedules on purpose. Trigger the job with
databricks bundle run -t dev daily_etl, or unpause it in the UI when you want to test the schedule. - Stale state after renaming a target. Deployment state is stored per target, under the target's
root_path. Renaming a target creates a fresh deployment and leaves the old resources in the workspace. Rundatabricks bundle destroy -t <old-name>first, or the two copies coexist.
Exercise
Your bundle has dev (with default: true) and prod targets. A teammate runs databricks bundle deploy with no flags on a laptop already authenticated to the prod workspace. Which target is deployed, and which two practices would prevent a surprise?
Show answer
The dev target is used, because it is marked default: true. The CLI then uses the host from the dev target, so if no credentials exist for that host the command fails rather than touching prod. To avoid surprises, pass -t explicitly in every script and CI step, and keep the default pointing at the least dangerous target (dev).
Exercise
In the prod target, you want daily_etl to run on 8 workers while the shared resource file defines the cluster with job_cluster_key: main and 2 workers. What must the override contain, and what happens if you write job_cluster_key: primary instead?
Show answer
The override goes under targets.prod.resources.jobs.daily_etl.job_clusters with an entry whose job_cluster_key is main and new_cluster.num_workers is 8. Because job clusters merge by key, the other cluster settings stay as defined. With primary, the key doesn't match, so the CLI adds a second cluster instead of editing main. The tasks would still use main with 2 workers.