Sub topic 8
Starting from a template with bundle init
Writing a bundle from scratch means remembering the file layout, the target settings, and the resource syntax. databricks bundle init skips that. It asks a few questions, renders a template, and writes a working bundle into a new directory. This chapter assumes you have installed and authenticated the Databricks CLI and can read a databricks.yml with targets and variables. It covers the built-in templates, how to run init interactively and non-interactively, and how to read the generated project. It also shows how to write a small custom template for your team.
What bundle init does
A bundle template is a directory of files with placeholders, plus a schema file that lists the questions to ask. When you run bundle init, the CLI does four things:
- Locates the template: a built-in name, a local path, or a Git URL.
- Reads the template's schema and prompts you for each property, or reads the answers from a config file.
- Renders every file in the template, substituting your answers.
- Writes the result into a new project directory.
init only writes files on your machine. It does not contact the workspace to create resources and it does not deploy anything. Deployment happens later, when you run bundle deploy.
Running init interactively
Run the command with no arguments to pick from the built-in templates:
databricks bundle initThe CLI lists the available templates and then asks for the answers that template needs. Run it from the directory where you want the new project folder to appear, which is usually the parent of your repository. The CLI creates a subdirectory named after the project.
The set of built-in templates depends on your CLI version. These have been available for some time:
| Template | What it generates | Typical use |
|---|---|---|
| `default-python` | A job and optionally a pipeline, a Python package, notebooks, and tests | Python ETL projects |
| `default-sql` | A job that runs SQL files against a SQL warehouse | SQL-only workflows |
| `dbt-sql` | A dbt project wired to a job | Teams that already use dbt |
| `mlops-stacks` | A multi-resource machine learning project with CI/CD files | Production ML workflows |
You can also name a template directly and skip the menu:
databricks bundle init default-pythonRun databricks bundle init --help to see the templates your installed version supports. Newer releases add templates, and a template's prompts also change between versions.
Running init without prompts
Prompts do not work in scripts or CI. For those cases, put the answers in a JSON file whose keys match the property names in the template's schema:
{
"project_name": "sales_etl",
"include_notebook": "yes",
"include_dlt": "no",
"include_python": "yes"
}Save it as init-config.json and pass it with --config-file:
databricks bundle init default-python \
--config-file init-config.json \
--output-dir ./projects--output-dir sets the directory in which the project folder is created. If you omit a property from the config file, the CLI falls back to prompting for it, which will fail in a non-interactive shell. Include every property the template asks for. The exact property names come from the template, so run the template once interactively, or read its schema file, to find them.
The flags you will use most:
| Flag | Purpose |
|---|---|
| `--config-file` | JSON file with answers, so no prompts appear |
| `--output-dir` | Directory where the project folder is created |
| `--template-dir` | Subdirectory of a Git repository that holds the template |
| `--branch` | Git branch to read the template from |
| `--tag` | Git tag to read the template from |
Reading what was generated
The default-python template produces a layout like this. File names vary slightly across CLI versions, so treat this as a guide:
sales_etl/
databricks.yml
pyproject.toml
README.md
resources/
sales_etl.job.yml
sales_etl.pipeline.yml
src/
sales_etl/
main.py
notebook.ipynb
tests/
main_test.py
scratch/Map each part to the concepts from earlier chapters:
databricks.ymlholds the bundle name, anincludelist that pulls inresources/*.yml, and thedevandprodtargets.resources/holds one file per resource, which is the include pattern from the chapter on organizing configuration.src/holds the code the job runs. Resource files refer to it with paths relative to the resource file, such as../src/notebook.ipynb.tests/holds unit tests you can run locally.
After generating, read the files before deploying. The template makes choices for you, such as the cluster definition, the schedule, and the notification settings. Check each against your environment.
cd sales_etl
databricks bundle validate -t dev
databricks bundle deploy -t dev
databricks bundle run -t dev sales_etl_jobThe job's resource key comes from the generated resource file. Open resources/sales_etl.job.yml and use the key under jobs: in the bundle run command. Templates often append a suffix such as _job.
Common first-run problems
- Validation fails on a node type or catalog. Templates fill defaults using values from your workspace at init time. If you init in one workspace and deploy to another, update
node_type_id,catalog, and theworkspace.hostof each target. - The wrong workspace host.
initreads your CLI profile to fill inworkspace.host. If you have several profiles, setDATABRICKS_CONFIG_PROFILEor pass--profileso the template picks up the host you intend. - A re-run overwrites nothing and fails. If the output folder already exists,
initstops rather than merging. Pick a new project name or a new--output-dir. - Missing prompts in CI. Without
--config-file, the command waits for input. Always supply a config file in pipelines.
Writing your own template
Built-in templates fit generic projects. A team usually wants its own conventions: a naming scheme, a fixed cluster policy, a standard prod target. A custom template lets every new project start from those rules.
A template directory has two required parts: a databricks_template_schema.json file at the root, and a template/ directory that holds the files to render.
team_template/
databricks_template_schema.json
template/
{{.project_name}}/
databricks.yml.tmpl
resources/
{{.project_name}}_job.yml.tmpl
src/
main.pyDirectory and file names can contain placeholders, as in {{.project_name}}. Files ending in .tmpl have the suffix removed after rendering. The templating language is Go's text/template.
The schema file
The schema declares the questions. Each property has a type, a prompt description, and optional validation:
{
"welcome_message": "Creating a new team project",
"properties": {
"project_name": {
"type": "string",
"default": "my_project",
"description": "Project name (lowercase letters, digits, underscores)",
"order": 1,
"pattern": "^[a-z][a-z0-9_]*$",
"pattern_match_failure_message": "Use lowercase letters, digits, and underscores, starting with a letter."
},
"schedule_cron": {
"type": "string",
"default": "0 0 6 * * ?",
"description": "Quartz cron expression for the daily run",
"order": 2
}
},
"success_message": "Project created. Run 'databricks bundle validate' next."
}order controls the sequence of prompts. pattern rejects input that does not match the regular expression. Properties with type of string can also use an enum list to restrict the choices.
The template files
Refer to answers with {{.property_name}}. The CLI also provides helper functions, such as {{workspace_host}}, {{user_name}}, and {{short_name}}, that read from your current authentication. The available helpers can change between CLI versions, so check the documentation for yours.
template/{{.project_name}}/databricks.yml.tmpl:
bundle:
name: {{.project_name}}
include:
- resources/*.yml
targets:
dev:
mode: development
default: true
workspace:
host: {{workspace_host}}
prod:
mode: production
workspace:
host: {{workspace_host}}
root_path: /Workspace/Shared/.bundle/${bundle.name}/${bundle.target}template/{{.project_name}}/resources/{{.project_name}}_job.yml.tmpl:
resources:
jobs:
{{.project_name}}_job:
name: {{.project_name}}_job
schedule:
quartz_cron_expression: "{{.schedule_cron}}"
timezone_id: UTC
tasks:
- task_key: main
spark_python_task:
python_file: ../src/main.py
environment_key: default
environments:
- environment_key: default
spec:
client: "1"The ${bundle.name} text is a bundle substitution, not a template placeholder. Go templates only act on {{ }}, so ${...} passes through unchanged.
Using the template
Point init at the local directory:
databricks bundle init ./team_templateFor repeatable runs, combine it with a config file:
databricks bundle init ./team_template \
--config-file init-config.json \
--output-dir ./projectsTo share the template, push it to a Git repository and pass the URL. If the template lives in a subdirectory, add --template-dir:
databricks bundle init https://github.com/<your-org>/<your-repo> \
--template-dir templates/team_template \
--tag v1.0.0Pinning a tag or branch keeps projects reproducible. Without one, init reads the repository's default branch, so a template change can alter what new projects look like.
Edge cases and trade-offs
- Templates are a one-time copy. Once rendered, the project no longer follows the template. If you improve the template, existing projects do not update. Keep the shared settings you want to change later in variables, or in a central include, instead of relying on re-running
init. - Generated defaults are not your standards. Treat the built-in output as a starting point. Review cluster size, schedules, and the
prodtarget'srun_asbefore the first production deploy. - Version drift. Prompts, file names, and generated settings change as the CLI changes. A config file written for one CLI version can fail on another when a property is added or renamed. Pin the CLI version in CI.
- Secrets stay out. Do not put tokens or passwords into a template or config file. Both usually end up in source control.
Exercise
Your CI pipeline runs databricks bundle init ./team_template --output-dir ./projects and hangs. What is the cause, and what two changes make the command work in a pipeline?
Show answer
The command is waiting for answers to the template's prompts, and a pipeline has no terminal to provide them. Create a JSON config file whose keys match the property names in databricks_template_schema.json, and pass it with --config-file. Include every property the template asks for, because any missing one still triggers a prompt. Also pin the CLI version in the pipeline so property names do not change underneath the config file.
Exercise
You change a custom template to add a new notification_email property. Three existing projects were created from the old version. Do they get the new property the next time someone runs bundle deploy?
Show answer
No. bundle init renders the template once and writes plain files. The existing projects keep the files they were created with, and bundle deploy does not consult the template. To add the property to them you must edit each project, or generate a new project and copy the changes across.