Sub topic 5
Targets: dev and prod from one bundle
This chapter assumes you can read YAML, have the Databricks CLI installed and authenticated, and know the basic layout of a databricks.yml file. Here you will learn how targets let one bundle deploy to different environments, and what the development and production modes change.
What a target is
A target is a named set of overrides for a bundle. The top-level bundle, variables, include, and resources sections describe what the project is. Each entry under targets describes where and how to deploy it. You pick one with the -t flag, so the same code can go to a dev workspace on Monday and a prod workspace on Friday.
bundle:
name: my_project
targets:
dev:
mode: development
default: true
workspace:
host: https://<your-dev-workspace-url>
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>Settings in a target are merged over the top-level configuration. Scalars are replaced and maps are merged key by key. This is why the shared job definition stays in one place while only the differences live under each target.
Choosing a target
Run any bundle command with -t:
databricks bundle deploy -t dev
databricks bundle deploy -t prodIf you omit -t, the CLI uses the target marked default: true. If no target is marked default and there is more than one, the CLI cannot choose, so it reports an error. Mark only one target as default, and make it the safe one (dev).
Development mode
Setting mode: development makes the CLI treat the deployment as a personal sandbox. It changes several defaults so that several developers can deploy the same bundle without colliding:
- Deployed resources get a
[dev <your_user_name>]prefix in their names. - Resources are tagged with
dev. - Job schedules and triggers are paused, so a deployed job does not run by itself.
- The workspace files go under your own user folder, not a shared one.
You can still run a job on demand with databricks bundle run -t dev daily_etl.
Production mode
Setting mode: production makes the CLI check that the deployment is safe to share. databricks bundle validate and deploy fail if the configuration does not meet these checks. They include:
- Deploying to a shared location instead of a personal folder. Setting
root_pathexplicitly, as in the example above, satisfies this. - Setting
run_asto an identity that is not an individual user, usually a service principal, so jobs do not depend on one person's account.
Production mode does not add name prefixes and does not pause schedules. A scheduled job in prod will run on its schedule after deploy.
| Behavior | `development` | `production` |
|---|---|---|
| Resource name prefix | `[dev <user>]` | None |
| Schedules and triggers | Paused | Active |
| Default workspace path | Your user folder | Must be a shared path you set |
| `run_as` | Optional | Expected to be a service principal |
The exact set of checks changes between CLI versions, so confirm details with databricks bundle validate -t prod and the current documentation.
Overriding resources per target
A target can also override parts of a resource. For example, give the prod job a larger cluster:
targets:
prod:
resources:
jobs:
daily_etl:
job_clusters:
- job_cluster_key: main
new_cluster:
num_workers: 8Lists of items that have a key, such as job_clusters (keyed by job_cluster_key) and tasks (keyed by task_key), are merged by that key. Here, the main cluster keeps its other settings and only num_workers changes. For simple per-environment values such as a catalog name, variables (next chapter) are usually cleaner than overriding whole resources.
Common errors
- Host not set. If a target has no
workspace.hostand your CLI profile does not supply one, authentication fails. Set the host in the target. - Production validation fails. The usual causes are a missing
run_asor the default per-userroot_path. Set both. - Unexpected prefix in the job name. You deployed with a development-mode target. Names without prefixes mean production mode or no
modeset. - Wrong environment. Without
-t, the default target is used. Always pass-tin scripts.
Exercise
A bundle has targets dev (mode: development, default: true) and prod (mode: production). You run databricks bundle deploy with no flags. Which target is used, and will its scheduled jobs run automatically?
Show answer
The dev target is used because it is marked default: true. In development mode, schedules and triggers are paused, so jobs do not run automatically until you start them with databricks bundle run.
Exercise
databricks bundle validate -t prod fails on a target that uses mode: production. Name two configuration settings you should check first.
Show answer
Check that run_as is set to a service principal rather than an individual user, and that workspace.root_path points to a shared location instead of the default per-user folder.