Sub topic 14
Deploying pipelines as code with Databricks Asset Bundles
This chapter assumes you can write a pipeline in SQL or Python, know the difference between development and production mode, and have the Databricks CLI installed and authenticated to a workspace. You do not need prior experience with bundles.
Why deploy as code
Creating a pipeline in the UI is fine for learning. For a team, it has problems. Settings live only in the workspace, changes are hard to review, and rebuilding the pipeline in another workspace means clicking through the same screens again.
A Databricks Asset Bundle is a project folder that describes your pipeline, jobs and other resources in YAML files. You keep it in Git, review changes in pull requests, and deploy it with the Databricks CLI. The same bundle can target several workspaces or environments.
A minimal bundle
A bundle has a databricks.yml file at its root. This file names the bundle and defines deployment targets. Resources can be declared in the same file or included from other files.
bundle:
name: orders_bundle
include:
- resources/*.yml
targets:
dev:
mode: development
default: true
workspace:
host: https://<your-workspace>.cloud.databricks.com
prod:
mode: production
workspace:
host: https://<your-workspace>.cloud.databricks.com
root_path: /Workspace/Shared/.bundle/${bundle.name}/${bundle.target}Replace the host values with your own workspace URL. The dev target uses development mode, which prefixes deployed resource names with your user name so they do not collide with other people's copies. The prod target uses production mode and a fixed root_path.
Declaring the pipeline
Put the pipeline definition in resources/orders_pipeline.yml. It points at the SQL file from the earlier chapters.
resources:
pipelines:
orders_pipeline:
name: orders_pipeline
catalog: main
schema: demo
serverless: true
continuous: false
libraries:
- file:
path: ../src/orders.sqlEach field maps to a setting you already know:
| Field | Meaning |
|---|---|
| `catalog` and `schema` | Where the pipeline publishes its datasets in Unity Catalog |
| `serverless` | Use serverless compute instead of a configured cluster |
| `continuous` | `false` means triggered updates, `true` means continuous |
| `libraries` | The source files that define the datasets |
The path is relative to the YAML file that contains it. Here, ../src/orders.sql means the src folder next to resources.
The deployment workflow
The CLI has three commands you use most.
databricks bundle validatechecks the configuration for errors.databricks bundle deploy -t devuploads the source files and creates or updates the pipeline in the dev target.databricks bundle run -t dev orders_pipelinestarts an update of the pipeline.
Deploying does not run the pipeline. It only makes the workspace match your files. Running is a separate step.
Common errors
- Editing the deployed pipeline in the UI. The next
bundle deployoverwrites those changes with what is in your files. Make changes in the YAML instead. - Wrong relative paths. If
librariespoints to a file that does not exist, deployment fails. Check paths against the location of the YAML file, not the bundle root. - Forgetting the target. Without
-t, the CLI uses the target markeddefault: true. Be explicit when deploying to production. - Missing permissions. The identity that deploys needs permission to create pipelines and to use the target catalog and schema.
Exercise
You changed continuous: false to continuous: true in the YAML file and ran databricks bundle deploy -t dev. Is the pipeline now running continuously? What else is needed?
Show answer
Deploying updates the pipeline's configuration to continuous mode, but it does not start an update. You still need to start one, for example with databricks bundle run -t dev orders_pipeline. A continuous pipeline then keeps running until it is stopped.
Exercise
A colleague fixed a pipeline setting directly in the workspace UI. Two days later the old value came back. What most likely happened, and how do you prevent it?
Show answer
Someone ran databricks bundle deploy, which made the workspace match the YAML files and overwrote the UI edit. To prevent it, make configuration changes in the bundle files and deploy them.