Sub topic 10
Controlling configuration with cluster policies
A cluster policy is a set of rules that limits how users can configure classic compute. Policies keep cluster sizes, runtime versions, and idle shutdown times inside bounds that an administrator chose. They are the main cost and governance control for all-purpose and job clusters.
This chapter assumes you have read the earlier chapters on all-purpose clusters, job clusters, access modes, and autoscaling and auto termination. It also assumes you can read JSON.
What a policy does
A policy is a JSON document. It maps cluster attributes, such as node_type_id or autotermination_minutes, to rules. When someone creates or edits a cluster and picks that policy, the workspace checks the settings against the rules. It then does one of three things:
- Rejects values that break a rule.
- Fills in fixed values and defaults.
- Hides attributes the user should not see.
Policies serve three goals:
- Cost control. Cap worker counts, require auto termination, and restrict node types.
- Governance. Force an access mode that works with Unity Catalog, and force cost-allocation tags.
- Simplicity. Show users a short form instead of the full cluster page.
Policies apply to classic compute only. Serverless compute has no node type, runtime, or worker count to constrain, so cluster policies do not govern it. Serverless cost control uses other mechanisms, such as budget policies and usage monitoring.
Anatomy of a policy definition
Each key in the definition is an attribute path. A top-level attribute uses its API name, for example spark_version. A nested attribute uses dots, for example autoscale.max_workers. Spark settings and tags use prefixes: spark_conf.<key> and custom_tags.<name>.
Each value is an object with a type and the parameters for that type.
| Type | Meaning | Parameters |
|---|---|---|
| `fixed` | The attribute must have exactly this value. | `value` |
| `forbidden` | The attribute cannot be set. | none |
| `allowlist` | The value must be one of a list. | `values`, optional `defaultValue` |
| `blocklist` | The value must not be in a list. | `values` |
| `regex` | The value must match a regular expression. | `pattern`, optional `defaultValue` |
| `range` | A number must fall between bounds. | `minValue`, `maxValue`, optional `defaultValue` |
| `unlimited` | Any value is allowed. Use it to make an attribute required or to set a default. | optional `defaultValue` |
Three optional properties work with most types:
hidden: trueremoves the attribute from the cluster creation form. It works withfixedrules.defaultValuepre-fills the form for the user.isOptional: truelets the user leave the attribute unset. Without it, an attribute that has a rule is required.
Policies also support virtual attributes. dbus_per_hour limits the DBU rate a cluster can reach, which is a cost ceiling that holds even when the user changes node types. cluster_type restricts a policy to all-purpose, job, or dlt clusters.
An example policy
This policy fits a data engineering team. It pins Unity Catalog-compatible access mode, limits runtimes to the 15.x line, restricts node types, caps autoscaling, requires auto termination between 10 and 120 minutes, and adds a cost tag.
{
"cluster_type": {
"type": "fixed",
"value": "all-purpose"
},
"spark_version": {
"type": "regex",
"pattern": "15\\.[0-9]+\\.x-scala2\\.12",
"defaultValue": "15.4.x-scala2.12"
},
"node_type_id": {
"type": "allowlist",
"values": ["i3.xlarge", "i3.2xlarge"],
"defaultValue": "i3.xlarge"
},
"autoscale.min_workers": {
"type": "range",
"minValue": 1,
"maxValue": 2,
"defaultValue": 1
},
"autoscale.max_workers": {
"type": "range",
"minValue": 1,
"maxValue": 8,
"defaultValue": 4
},
"autotermination_minutes": {
"type": "range",
"minValue": 10,
"maxValue": 120,
"defaultValue": 60
},
"data_security_mode": {
"type": "fixed",
"value": "USER_ISOLATION",
"hidden": true
},
"custom_tags.team": {
"type": "fixed",
"value": "data-eng"
},
"dbus_per_hour": {
"type": "range",
"maxValue": 20
}
}The node types are AWS examples. On Azure or GCP, use that cloud's instance names in node_type_id.
Because data_security_mode is fixed and hidden, users cannot pick a mode that bypasses Unity Catalog. The custom_tags.team rule means every cluster carries a tag that cost reports can group by.
How a request is evaluated
The same checks run whether the request comes from the UI, the API, or a job definition.
Two behaviors surprise people:
- Policies are checked when a cluster is created or edited. Changing a policy does not stop or reconfigure running clusters. A cluster that no longer matches its policy is out of compliance. It is checked again the next time someone edits it, and the edit fails until the settings comply.
- A job cluster that references a policy is validated when the job is saved and again when the run starts.
Creating a policy through the API
In the REST API, the definition field is a JSON string, not a nested object. Serialize it with json.dumps. The script below reads your workspace URL and a personal access token from environment variables and creates the policy from a file.
import json
import os
import requests
host = os.environ["DATABRICKS_HOST"].rstrip("/") # e.g. https://my-workspace.cloud.databricks.com
token = os.environ["DATABRICKS_TOKEN"]
with open("data-eng-policy.json") as f:
definition = json.load(f)
response = requests.post(
f"{host}/api/2.0/policies/clusters/create",
headers={"Authorization": f"Bearer {token}"},
json={
"name": "data-eng-interactive",
"definition": json.dumps(definition),
"description": "Interactive compute for the data engineering team",
},
timeout=30,
)
response.raise_for_status()
print("policy_id:", response.json()["policy_id"])Save the policy JSON from the previous section as data-eng-policy.json first. The response contains a policy_id.
Permissions
Creating policies is an admin task. Users get access in one of two ways:
- CAN_USE on a policy. The user can create clusters that follow that policy. Grant it to groups, not individuals.
- The unrestricted cluster creation entitlement. The user can create clusters with no policy. Remove it from non-admin users so policies cannot be bypassed.
Without the entitlement, a user who has CAN_USE on exactly one policy sees only that policy in the cluster form. The form pre-selects it.
Policies for jobs
A job cluster can reference a policy through policy_id in its new_cluster block. The run service checks the settings against the policy. A common problem is a job definition that omits attributes the policy fixes. Set apply_policy_default_values to true so the policy fills omitted fields. Without it, the request can fail on missing required attributes.
{
"name": "nightly-etl-with-policy",
"job_clusters": [
{
"job_cluster_key": "etl_cluster",
"new_cluster": {
"policy_id": "ABC123DEF456",
"apply_policy_default_values": true,
"num_workers": 2
}
}
],
"tasks": [
{
"task_key": "ingest",
"job_cluster_key": "etl_cluster",
"notebook_task": {
"notebook_path": "/Workspace/Users/[email protected]/ingest"
}
}
]
}Replace ABC123DEF456 with a real policy ID. This only works with a policy that does not fix autoscale.* attributes, because the example sets a fixed num_workers. A policy meant for jobs normally sets cluster_type to job and does not rely on autotermination_minutes, which does not apply to job clusters.
Policy families
Databricks provides built-in policy families, which are templates for common patterns such as personal compute, shared compute, power user compute, and job compute. You create a policy from a family and add overrides. The family part stays maintained by Databricks, so your policy picks up new attribute rules when the family is updated. Start from a family when your needs match one. Write a policy from scratch only when you need rules the families do not cover.
Common mistakes
- Fixing an attribute but also setting it elsewhere. If a policy fixes
autotermination_minutesat 60 and a job definition sets 30, the request fails. Drop the setting or useapply_policy_default_values. - Forgetting
isOptional. An attribute with a rule is required. Mark rarely used attributes optional. - Over-restricting
node_type_id. If the list excludes the only node type available in a region, nobody can create a cluster. Check availability first. - Escaped regex. In JSON, backslashes must be doubled, as in
15\\.[0-9]+. - Assuming serverless is covered. A policy has no effect on serverless compute.
Practical guidance
- Start with one policy per team or workload class, such as interactive, ETL job, and ML.
- Fix access mode and cost tags. Range-limit size and idle time.
- Add
dbus_per_houras a backstop so a node type change cannot raise spend without limit. - Store policy JSON in version control and create it through the API or your infrastructure tool, as covered in the chapter on defining compute with the Jobs and Clusters APIs.
- Test by creating a cluster as a non-admin member of the target group.
Exercise
A policy sets "autotermination_minutes" to {"type": "range", "minValue": 10, "maxValue": 120, "defaultValue": 60}. A user creates a cluster with the policy and types 180 minutes in the form. A different user edits a running cluster that has no auto termination setting. What happens in each case, and which control makes sure users cannot skip the policy entirely?
Show answer
The first request is rejected, because 180 is above maxValue 120. The second edit also fails, because the policy checks the cluster on edit and the attribute is required (no isOptional) with no value in range. Policy defaults or a value between 10 and 120 are needed. Users skip policies only if they hold the unrestricted cluster creation entitlement, so remove it from non-admin users and grant CAN_USE on the policy to their group.