Databricks

Terraform for Databricks: Automating Workspaces with IaC

2026-03-26
更新: 2026-03-27
NicheeLab Editorial Team

The Databricks Terraform Provider lets you declaratively manage Databricks resources — workspaces, clusters, jobs, Unity Catalog, networking, and more — using HashiCorp Terraform (HCL). Infrastructure as Code (IaC) eliminates manual operations and gives you reproducibility and auditability for every environment.

This article walks through the basic configuration of the Terraform Provider, a catalog of key resources, HCL examples, state management best practices, and how to split responsibilities between Terraform and DABs.

Databricks Terraform Provider Overview

The Databricks Terraform Provider is an officially maintained HashiCorp Terraform plugin. It is published on the Terraform Registry and is declared inside an HCL file as shown below.

terraform {
  required_providers {
    databricks = {
      source  = "databricks/databricks"
      version = "~> 1.40"
    }
  }
}

provider "databricks" {
  host  = var.databricks_host
  token = var.databricks_token
}

You can authenticate with a Personal Access Token (PAT), a service principal, an Azure Managed Identity, or an AWS IAM role. The standard for production is to use service principals and run from a CI/CD pipeline rather than relying on PATs.

Key Resources at a Glance

CategoryTerraform ResourceWhat it manages
Workspacedatabricks_mws_workspacesWorkspace creation (E2 architecture)
Clusterdatabricks_clusterAll-Purpose Cluster definitions
Cluster Policydatabricks_cluster_policyJSON definitions for cluster creation constraints
Instance Pooldatabricks_instance_poolPre-allocated instance pools
Jobdatabricks_jobWorkflow job definitions
Secretdatabricks_secret_scope / databricks_secretSecret scopes and secret values
Unity Catalogdatabricks_catalog / databricks_schema / databricks_grantsCatalogs, schemas, and grants
External Locationdatabricks_external_locationExternal storage location definitions
Storage Credentialdatabricks_storage_credentialCredentials for cloud storage
Networkdatabricks_mws_networks / databricks_mws_private_access_settingsVPC / VNet configuration, Private Link
Groups & Usersdatabricks_group / databricks_user / databricks_service_principalIdentity management and service principals

A Basic HCL Configuration Example

The example below is a baseline configuration that creates a Unity Catalog catalog and schema and defines a cluster policy.

resource "databricks_catalog" "analytics" {
  name    = "analytics"
  comment = "分析チーム用カタログ"
}

resource "databricks_schema" "bronze" {
  catalog_name = databricks_catalog.analytics.name
  name         = "bronze"
  comment      = "RAWデータ格納用スキーマ"
}

resource "databricks_grants" "bronze_grants" {
  schema = databricks_schema.bronze.id
  grant {
    principal  = "data-engineers"
    privileges = ["USE_SCHEMA", "CREATE_TABLE", "SELECT"]
  }
  grant {
    principal  = "data-analysts"
    privileges = ["USE_SCHEMA", "SELECT"]
  }
}

resource "databricks_cluster_policy" "standard" {
  name = "standard-policy"
  definition = jsonencode({
    "spark_version" : {
      "type" : "regex",
      "pattern" : "14\\.3\\.x-scala.*"
    },
    "num_workers" : {
      "type" : "range",
      "minValue" : 1,
      "maxValue" : 8,
      "defaultValue" : 2
    },
    "autotermination_minutes" : {
      "type" : "range",
      "minValue" : 10,
      "maxValue" : 60,
      "defaultValue" : 20
    },
    "custom_tags.CostCenter" : {
      "type" : "fixed",
      "value" : "analytics-team"
    }
  })
}

resource "databricks_secret_scope" "app_secrets" {
  name = "app-secrets"
}

State Management Best Practices

The Terraform state file (terraform.tfstate) records the current state of every managed resource. For team development, follow these best practices.

RequirementAWS configurationAzure configuration
State file storageS3 bucketAzure Storage Account (Blob)
Exclusive lockingDynamoDBBlob Lease
EncryptionS3 SSE (AES-256 / KMS)Azure Storage Service Encryption
VersioningS3 versioning enabledBlob Versioning enabled
Access controlRestricted via IAM policiesRestricted via Azure RBAC
# AWSの場合のバックエンド設定
terraform {
  backend "s3" {
    bucket         = "my-terraform-state"
    key            = "databricks/prod/terraform.tfstate"
    region         = "ap-northeast-1"
    dynamodb_table = "terraform-locks"
    encrypt        = true
  }
}

Managing Environments (dev / staging / prod)

There are two main approaches for managing per-environment differences in Terraform.

  • Terraform Workspaces: reuse the same HCL files and switch environments with terraform workspace select dev. The state file is separated per workspace
  • Directory separation + tfvars: split into directories like environments/dev/ and environments/prod/, and override variables with a tfvars file per environment. This is the recommended approach for large-scale configurations

DABs vs Terraform: Comparison Table

Comparison itemTerraformDatabricks Asset Bundles
Sweet spotInfrastructure base (workspaces, networking, UC, policies)Application assets (jobs, DLT, notebooks, ML)
Definition languageHCLYAML (databricks.yml)
State managementExplicit (tfstate + remote backend)Implicit, managed on the Databricks workspace side
plan/previewPreview diffs with terraform planValidate with databricks bundle validate
Multi-cloudCan manage AWS / Azure / GCP resources alongside DatabricksDatabricks assets only
Learning curveMedium-to-high (HCL syntax + understanding state management)Low (YAML + Databricks CLI)
Best fit forInfrastructure / platform engineersData engineers / ML engineers

What the Exam Asks About

  • "How do I manage workspace configuration reproducibly as code?" → Terraform (Databricks Provider)
  • "How do I define cluster policies and Secret Scopes as code?" → Terraform
  • "How do I deploy jobs and DLT pipelines across environments?" → DABs (Terraform also works, but DABs is more Databricks-native)
  • "When should I use Terraform vs DABs?" → Terraform for the infrastructure base, DABs for application assets

Check Your Understanding

Platform Admin / Data Engineer Professional

問題 1

A platform team wants to manage workspace creation, Unity Catalog metastore configuration, cluster policy definitions, and network setup (VPC Peering) as code, all in one pipeline. Which tool is the most appropriate?

  1. Define everything in databricks.yml using Databricks Asset Bundles (DABs)
  2. Build it manually with the Databricks CLI workspace commands
  3. Define the resources in HCL files with Terraform (Databricks Provider) and run terraform apply from CI/CD
  4. Create the workspace in the Databricks UI and back up the settings with a JSON export

正解: C

Workspace creation, UC metastore configuration, cluster policies, and network setup are all infrastructure-base concerns — Terraform's sweet spot. DABs specializes in application assets like jobs, DLT pipelines, and notebooks, and does not handle workspace creation or VPC Peering. Manual builds and JSON exports lack reproducibility and auditability.

Frequently Asked Questions

How do I configure authentication for the Databricks Terraform Provider?

There are three main approaches: (1) set a Personal Access Token (PAT) via the DATABRICKS_TOKEN environment variable, (2) authenticate via an Azure AD / AWS IAM / GCP service account, or (3) use Databricks CLI profile authentication. For production, the recommended approach is service principals running from a CI/CD pipeline. Limit PATs to individual development use and always set a token expiration.

Where should the Terraform state file be stored?

Do not keep state on the local filesystem. Use a remote backend (AWS S3 + DynamoDB, Azure Storage Account + Blob, GCS, or Terraform Cloud). For team development, exclusive state locking is critical — enable it through DynamoDB (AWS) or Blob Lease (Azure). State files can contain secrets, so encryption is also mandatory.

Should I use Terraform or Databricks Asset Bundles (DABs)?

A two-layer split is recommended: manage the infrastructure base layer (workspace creation, UC metastore, cluster policies, network configuration, Secret Scopes) with Terraform, and the application layer (jobs, DLT pipelines, notebooks, ML models) with DABs. Terraform can manage jobs too, but DABs lets you describe them natively in databricks.yml and offers cleaner environment-target management.

Check what you learned with practice questions

Practice with certification-focused question sets

無料で問題を解いてみる
Author

NicheeLab Editorial Team

NicheeLab editorial team focused on data engineering and cloud certification learning. Content is structured around practical study needs and official exam domains.


Related articles
Databricks

Databricks Certifications: All 7 Exams, Difficulty & Study Plan (2026)

Complete guide to all 7 Databricks certifications — Data Eng...

Databricks

Databricks Exam Difficulty Ranking: All 7 Certs Compared (2026)

Every Databricks certification ranked by difficulty, with st...

Databricks

Databricks Study Guide: Fastest Pass Route & Time Estimates (2026)

How to pass Databricks certifications efficiently. Official ...

Databricks

Databricks Data Engineer Associate: Complete Guide (2026)

Domain-by-domain breakdown of the Databricks Certified Data ...

Databricks

Databricks Data Engineer Professional: Complete Guide (2026)

Tactics for the Databricks Certified Data Engineer Professio...

Browse all Databricks articles (110)
© 2026 NicheeLab All rights reserved.