Create Databricks workspace using Terraform & Service Account based authentication and impersonation.
This documentation outlines the steps to create and configure two Google Cloud Platform (GCP) service accounts, namely caller-sa and privileged-sa. The caller-sa service account is granted the “Service Account Token Creator” role, while the privileged-sa service account is given the required permissions to create a Databricks workspace. Please note that databricks terraform provider only support GCP Service Account based authentication.
Network viewer role on the host or shared VPC project.Make sure to replace following variables with actual values while running the commands.
gcloud iam service-accounts create caller-sa --display-name="Caller Service Account"
gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
--member="serviceAccount:caller-sa@YOUR_PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/iam.serviceAccountTokenCreator"
gcloud iam service-accounts create privileged-sa --display-name="Privileged Service Account"
gcloud iam roles create DatabricksAdmin –project=YOUR_PROJECT_ID –file=databricks-admin-role.yaml
cat << EOF > databricks-admin-role.yaml
title: "Databricks Admin"
description: "Custom role with permissions required for Databricks workspace creation"
stage: "GA"
includedPermissions:
- iam.roles.get
- iam.roles.create
- iam.roles.delete
- iam.roles.update
- iam.serviceAccounts.getIamPolicy
- iam.serviceAccounts.setIamPolicy
- resourcemanager.projects.get
- resourcemanager.projects.getIamPolicy
- resourcemanager.projects.setIamPolicy
- serviceusage.services.enable
- serviceusage.services.get
- serviceusage.services.list
- compute.networks.get
- compute.subnetworks.get
- compute.projects.get
- compute.forwardingRules.get
- compute.forwardingRules.list
EOF
gcloud iam roles describe DatabricksAdmin --project=YOUR_PROJECT_ID
gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
--member="serviceAccount:privileged-sa@YOUR_PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/DatabricksAdmin"
gcloud iam service-accounts keys create /path/to/caller-sa-key.json \
--iam-account=caller-sa@YOUR_PROJECT_ID.iam.gserviceaccount.com
Replace /path/to/caller-sa-key.json with the desired local path and filename for the downloaded key file, and replace YOUR_PROJECT_ID with your actual GCP project ID. This command generates and downloads a JSON key file for the specified service account. The key file contains the private key and other information needed to authenticate as the service account.
gcloud auth activate-service-account \
--key-file=/path/to/caller-sa-key.json
This command activates authentication with Google Cloud using the provided service account key file. Replace /path/to/caller-sa-key.json with the actual path to your caller-sa service account key file.
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/caller-sa-key.json"
gcloud config set auth/impersonate_service_account privileged-sa@YOUR_PROJECT_ID.iam.gserviceaccount.com
export GOOGLE_OAUTH_ACCESS_TOKEN=$(gcloud auth print-access-token)
Follow these steps to add privileged-sa to the Databricks account console:
privileged-sa as an accounts user.privileged-sa.You are now ready to run Terraform scripts.