Skip to content

Instantly share code, notes, and snippets.

@olliefr
Created April 22, 2025 01:10
Show Gist options
  • Save olliefr/150bf46686efabb02a3e184b58e3bdfa to your computer and use it in GitHub Desktop.
Save olliefr/150bf46686efabb02a3e184b58e3bdfa to your computer and use it in GitHub Desktop.
Databricks: assign external location to multiple workspaces
terraform {
required_providers {
databricks = {
source = "databricks/databricks"
version = "1.74.0"
}
google = {
source = "hashicorp/google"
version = "6.30.0"
}
google-beta = {
source = "hashicorp/google-beta"
version = "6.30.0"
}
}
required_version = "~> 1.11.3"
}
variable "gcp_bucket" {
type = string
default = "dml-learning-datasets"
nullable = false
}
variable "databricks_workspace_1" {
type = number
description = "Workspace ID (number)"
default = 9999487995419999
nullable = false
}
variable "databricks_workspace_2" {
type = number
description = "Workspace ID (number)"
default = 9999826097309999
nullable = false
}
provider "databricks" {
host = "https://9999487995419999.1.gcp.databricks.com"
}
provider "google" {}
provider "google-beta" {}
data "databricks_current_config" "main" {}
data "databricks_current_metastore" "main" {}
data "databricks_current_user" "me" {}
resource "databricks_storage_credential" "external" {
name = "the-creds"
databricks_gcp_service_account {}
isolation_mode = "ISOLATION_MODE_ISOLATED"
}
data "google_storage_bucket" "main" {
name = var.gcp_bucket
}
resource "google_storage_bucket_iam_member" "databricks_admin" {
bucket = data.google_storage_bucket.main.name
member = "serviceAccount:${databricks_storage_credential.external.databricks_gcp_service_account[0].email}"
role = "roles/storage.admin"
}
resource "databricks_external_location" "main" {
name = "the-ext-location"
url = "gs://${data.google_storage_bucket.main.name}"
isolation_mode = "ISOLATION_MODE_ISOLATED"
credential_name = databricks_storage_credential.external.id
}
# Created the group "Data Engineers" in DB Account Console
# Granted "User" role to the group on the DB Workspace in DB Account Console
# Wait 5 minutes
data "databricks_group" "data_engineers" {
display_name = "Data Engineers"
recursive = false
}
resource "databricks_grants" "storage_credential" {
storage_credential = databricks_storage_credential.external.id
grant {
principal = data.databricks_group.data_engineers.display_name
privileges = ["CREATE_EXTERNAL_TABLE", "READ_FILES"]
}
}
resource "databricks_grants" "external_location" {
external_location = databricks_external_location.main.id
grant {
principal = data.databricks_group.data_engineers.display_name
privileges = ["CREATE_EXTERNAL_TABLE", "READ_FILES"]
}
}
resource "databricks_workspace_binding" "first" {
securable_name = databricks_external_location.main.name
securable_type = "external_location"
workspace_id = var.databricks_workspace_1
}
resource "databricks_workspace_binding" "second" {
securable_name = databricks_external_location.main.name
securable_type = "external_location"
workspace_id = var.databricks_workspace_2
}
output "databricks_current_config" {
value = data.databricks_current_config.main
}
output "current_metastore_info" {
value = data.databricks_current_metastore.main.metastore_info
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment