Skip to content

Commit 42c0006

Browse files
committed
feat: added databricks runtime module
1 parent 209f7d9 commit 42c0006

File tree

6 files changed

+241
-0
lines changed

6 files changed

+241
-0
lines changed

main.tf

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
data "azurerm_key_vault_secret" "sp_client_id" {
2+
name = var.sp_client_id_secret_name
3+
key_vault_id = var.key_vault_id
4+
}
5+
6+
data "azurerm_key_vault_secret" "sp_key" {
7+
name = var.sp_key_secret_name
8+
key_vault_id = var.key_vault_id
9+
}
10+
11+
data "azurerm_key_vault_secret" "tenant_id" {
12+
name = var.tenant_id_secret_name
13+
key_vault_id = var.key_vault_id
14+
}
15+
16+
locals {
17+
secrets = merge(var.secrets, {
18+
(var.sp_client_id_secret_name) = { value = data.azurerm_key_vault_secret.sp_client_id.value }
19+
(var.sp_key_secret_name) = { value = data.azurerm_key_vault_secret.sp_key.value }
20+
})
21+
secret_scope_name = var.use_local_secret_scope ? databricks_secret_scope.this[0].name : "main"
22+
mount_secret_name = var.use_local_secret_scope ? databricks_secret.this[var.sp_key_secret_name].key : data.azurerm_key_vault_secret.sp_key.name
23+
}
24+
25+
resource "databricks_token" "pat" {
26+
comment = "Terraform Provisioning"
27+
lifetime_seconds = var.pat_token_lifetime_seconds
28+
}
29+
30+
resource "databricks_user" "this" {
31+
for_each = var.sku == "standard" ? toset(var.users) : []
32+
user_name = each.value
33+
lifecycle { ignore_changes = [external_id] }
34+
}
35+
36+
resource "azurerm_role_assignment" "this" {
37+
for_each = {
38+
for permision in var.permissions : "${permision.object_id}-${permision.role}" => permision
39+
if permision.role != null
40+
}
41+
scope = var.workspace_id
42+
role_definition_name = each.value.role
43+
principal_id = each.value.object_id
44+
}
45+
46+
resource "databricks_cluster" "this" {
47+
cluster_name = "shared autoscaling"
48+
spark_version = var.spark_version
49+
50+
node_type_id = var.node_type
51+
autotermination_minutes = var.autotermination_minutes
52+
53+
autoscale {
54+
min_workers = var.min_workers
55+
max_workers = var.max_workers
56+
}
57+
58+
azure_attributes {
59+
availability = var.cluster_nodes_availability
60+
first_on_demand = var.first_on_demand
61+
spot_bid_max_price = var.spot_bid_max_price
62+
}
63+
64+
lifecycle {
65+
ignore_changes = [
66+
state
67+
]
68+
}
69+
}

mount.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
resource "databricks_mount" "adls" {
2+
for_each = var.mountpoints
3+
4+
cluster_id = databricks_cluster.this.id
5+
name = each.key
6+
uri = "abfss://${each.value["container_name"]}@${each.value["storage_account_name"]}.dfs.core.windows.net/${each.value["root_path"]}"
7+
extra_configs = {
8+
"fs.azure.account.auth.type" : "OAuth",
9+
"fs.azure.account.oauth.provider.type" : "org.apache.hadoop.fs.azurebfs.oauth2.ClientCredsTokenProvider",
10+
"fs.azure.account.oauth2.client.id" : data.azurerm_key_vault_secret.sp_client_id.value,
11+
"fs.azure.account.oauth2.client.secret" : "{{secrets/${local.secret_scope_name}/${data.azurerm_key_vault_secret.sp_key.name}}}",
12+
"fs.azure.account.oauth2.client.secret" : "{{secrets/${local.secret_scope_name}/${local.mount_secret_name}}}",
13+
"fs.azure.account.oauth2.client.endpoint" : "https://login.microsoftonline.com/${data.azurerm_key_vault_secret.tenant_id.value}/oauth2/token",
14+
"fs.azure.createRemoteFileSystemDuringInitialization" : "false",
15+
"spark.databricks.sqldw.jdbc.service.principal.client.id" : data.azurerm_key_vault_secret.sp_client_id.value,
16+
"spark.databricks.sqldw.jdbc.service.principal.client.secret" : "{{secrets/${local.secret_scope_name}/${data.azurerm_key_vault_secret.sp_key.name}}}",
17+
"spark.databricks.sqldw.jdbc.service.principal.client.secret" : "{{secrets/${local.secret_scope_name}/${local.mount_secret_name}}}",
18+
}
19+
}

outputs.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
output "token" {
2+
value = databricks_token.pat.token_value
3+
description = "Databricks Personal Authorization Token"
4+
}
5+
6+
output "cluster_id" {
7+
value = databricks_cluster.this.id
8+
description = "Databricks Cluster Id"
9+
}

secrets.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
resource "databricks_secret_scope" "this" {
2+
count = var.use_local_secret_scope ? 1 : 0
3+
4+
name = "main"
5+
initial_manage_principal = "users"
6+
}
7+
8+
resource "databricks_secret" "this" {
9+
for_each = var.use_local_secret_scope ? local.secrets : {}
10+
11+
key = each.key
12+
string_value = each.value["value"]
13+
scope = databricks_secret_scope.this[0].id
14+
}

variables.tf

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
variable "workspace_id" {
2+
type = string
3+
description = "Databricks Workspace ID"
4+
}
5+
6+
variable "sp_client_id_secret_name" {
7+
type = string
8+
description = "The name of Azure Key Vault secret that contains ClientID of Service Principal to access in Azure Key Vault"
9+
}
10+
11+
variable "sp_key_secret_name" {
12+
type = string
13+
description = "The name of Azure Key Vault secret that contains client secret of Service Principal to access in Azure Key Vault"
14+
}
15+
16+
variable "tenant_id_secret_name" {
17+
type = string
18+
description = "The name of Azure Key Vault secret that contains tenant ID secret of Service Principal to access in Azure Key Vault"
19+
}
20+
21+
variable "key_vault_id" {
22+
type = string
23+
description = "ID of the Key Vault instance where the Secret resides"
24+
}
25+
26+
# Optional
27+
variable "sku" {
28+
type = string
29+
description = "The sku to use for the Databricks Workspace: [standard|premium|trial]"
30+
default = "standard"
31+
}
32+
33+
variable "pat_token_lifetime_seconds" {
34+
type = number
35+
description = "The lifetime of the token, in seconds. If no lifetime is specified, the token remains valid indefinitely"
36+
default = 315569520
37+
}
38+
39+
variable "cluster_nodes_availability" {
40+
type = string
41+
description = "Availability type used for all subsequent nodes past the first_on_demand ones: [SPOT_AZURE|SPOT_WITH_FALLBACK_AZURE|ON_DEMAND_AZURE]"
42+
default = null
43+
}
44+
45+
variable "first_on_demand" {
46+
type = number
47+
description = "The first first_on_demand nodes of the cluster will be placed on on-demand instances: [[:number]]"
48+
default = 0
49+
}
50+
51+
variable "spot_bid_max_price" {
52+
type = number
53+
description = "The max price for Azure spot instances. Use -1 to specify lowest price."
54+
default = -1
55+
}
56+
57+
variable "autotermination_minutes" {
58+
type = number
59+
description = "Automatically terminate the cluster after being inactive for this time in minutes. If not set, Databricks won't automatically terminate an inactive cluster. If specified, the threshold must be between 10 and 10000 minutes. You can also set this value to 0 to explicitly disable automatic termination."
60+
default = 15
61+
}
62+
63+
variable "min_workers" {
64+
type = number
65+
description = "The minimum number of workers to which the cluster can scale down when underutilized. It is also the initial number of workers the cluster will have after creation."
66+
default = 0
67+
}
68+
69+
variable "max_workers" {
70+
type = number
71+
description = "The maximum number of workers to which the cluster can scale up when overloaded. max_workers must be strictly greater than min_workers."
72+
default = 1
73+
}
74+
75+
variable "users" {
76+
type = list(string)
77+
description = "List of users to access Databricks"
78+
default = []
79+
}
80+
81+
variable "secrets" {
82+
type = map(any)
83+
description = "Map of secrets to create in Databricks"
84+
default = {}
85+
}
86+
87+
variable "use_local_secret_scope" {
88+
type = bool
89+
description = "Create databricks secret scope and create secrets"
90+
default = false
91+
}
92+
93+
variable "permissions" {
94+
type = list(map(string))
95+
description = "Databricks Workspace permission maps"
96+
default = [
97+
{
98+
object_id = null
99+
role = null
100+
}
101+
]
102+
}
103+
104+
variable "spark_version" {
105+
type = string
106+
description = "Runtime version"
107+
default = "9.1.x-scala2.12"
108+
}
109+
110+
variable "node_type" {
111+
type = string
112+
description = "databricks_node_type id"
113+
default = "Standard_D3_v2"
114+
}
115+
116+
variable "mountpoints" {
117+
type = map(any)
118+
description = "mountpoints for databricks"
119+
default = null
120+
}

versions.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_version = ">=1.0.0"
3+
4+
required_providers {
5+
databricks = {
6+
source = "databricks/databricks"
7+
version = "=1.4.0"
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)