Skip to content

Commit 067c93b

Browse files
authored
Merge pull request #43 from MicrosoftCloudEssentials-LearningHub/databricks
databricks template
2 parents 1a7d6d9 + e97bc9d commit 067c93b

File tree

8 files changed

+319
-2
lines changed

8 files changed

+319
-2
lines changed

5_analytics-bigdata/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Costa Rica
55
[![GitHub](https://img.shields.io/badge/--181717?logo=github&logoColor=ffffff)](https://github.com/)
66
[brown9804](https://github.com/brown9804)
77

8-
Last updated: 2026-02-09
8+
Last updated: 2026-02-12
99

1010
------------------------------------------
1111

@@ -15,6 +15,7 @@ Last updated: 2026-02-09
1515
## Templates available
1616

1717
- [Azure Data Factory](./data-factory)
18+
- [Azure Databricks (Workspace)](./databricks)
1819
- [Azure Synapse Analytics (Workspace)](./synapse-analytics)
1920

2021
<!-- START BADGE -->
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Terraform Template - Azure Databricks (Workspace)
2+
3+
Costa Rica
4+
5+
[![GitHub](https://img.shields.io/badge/--181717?logo=github&logoColor=ffffff)](https://github.com/)
6+
[brown9804](https://github.com/brown9804)
7+
8+
Last updated: 2026-02-12
9+
10+
------------------------------------------
11+
12+
> This template contains Terraform configurations to create an Azure Databricks workspace.
13+
14+
> [!IMPORTANT]
15+
>
16+
> - Azure Databricks creates a **managed resource group** for service-managed infrastructure. You will typically see **two resource groups**: your main RG plus the Databricks-managed RG. This is expected behavior.
17+
> - If you disable `append_random_suffix`, you may hit name collisions.
18+
19+
<div align="center">
20+
<img width="650" alt="image" src="https://github.com/user-attachments/assets/5f4cf7a6-9317-4d34-adcf-c493297ab814" style="border: 2px solid #4CAF50; border-radius: 5px; padding: 5px;"/>
21+
</div>
22+
23+
<div align="center">
24+
<img width="650" alt="image" src="https://github.com/user-attachments/assets/90778a82-a56f-4b56-8673-66339073c746" style="border: 2px solid #4CAF50; border-radius: 5px; padding: 5px;"/>
25+
</div>
26+
27+
<div align="center">
28+
<img width="650" alt="image" src="https://github.com/user-attachments/assets/eb7c2c5c-2f1b-4bb1-8266-5e3093f28371" style="border: 2px solid #4CAF50; border-radius: 5px; padding: 5px;"/>
29+
</div>
30+
31+
## File Descriptions
32+
33+
- **main.tf**: Creates/updates the Resource Group (idempotent ARM PUT) and the Databricks workspace.
34+
- **variables.tf**: Defines input variables used in the Terraform configuration.
35+
- **provider.tf**: Configures the AzureRM + AzAPI + Random providers.
36+
- **terraform.tfvars**: Example values for variables.
37+
- **outputs.tf**: Outputs such as the workspace ID and URL.
38+
39+
## Variables
40+
41+
| Variable Name | Description | Type | Example Value |
42+
| --- | --- | --- | --- |
43+
| `resource_group_name` | Resource Group name to deploy into (created if missing). | string | `"rg-analytics-dev"` |
44+
| `location` | Azure region for the deployment. | string | `"eastus"` |
45+
| `databricks_workspace_name` | Base workspace name. If suffix enabled, final is `<base>-<suffix>`. | string | `"dbw-analytics-dev"` |
46+
| `sku` | Workspace SKU (`standard`, `premium`, `trial`). | string | `"standard"` |
47+
| `managed_resource_group_name` | Optional base managed RG name. If omitted, Databricks auto-generates it. | string | `null` |
48+
| `append_random_suffix` | Append random suffix to reduce name collisions. | bool | `true` |
49+
| `random_suffix_length` | Length of the suffix when enabled. | number | `6` |
50+
| `tags` | Tags applied to resources. | map(string) | `{ "env": "dev" }` |
51+
52+
## Usage
53+
54+
1. Authenticate:
55+
56+
```sh
57+
az login
58+
az account show
59+
# If needed:
60+
az account set --subscription "<subscription-id-or-name>"
61+
```
62+
63+
2. Initialize:
64+
65+
```sh
66+
terraform init -upgrade
67+
```
68+
69+
3. Validate and plan:
70+
71+
```sh
72+
terraform validate
73+
terraform plan
74+
```
75+
76+
4. Apply:
77+
78+
```sh
79+
terraform apply -auto-approve
80+
```
81+
82+
<!-- START BADGE -->
83+
<div align="center">
84+
<img src="https://img.shields.io/badge/Total%20views-1790-limegreen" alt="Total views">
85+
<p>Refresh Date: 2026-02-12</p>
86+
</div>
87+
<!-- END BADGE -->
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# main.tf
2+
# Creates an Azure Databricks workspace.
3+
4+
data "azurerm_client_config" "current" {}
5+
6+
# Resource group creation is idempotent in ARM (PUT). This will create the RG if it doesn't exist,
7+
# or update tags if it already exists.
8+
resource "azapi_resource" "resource_group" {
9+
type = "Microsoft.Resources/resourceGroups@2022-09-01"
10+
name = var.resource_group_name
11+
location = var.location
12+
parent_id = "/subscriptions/${data.azurerm_client_config.current.subscription_id}"
13+
14+
body = jsonencode({
15+
tags = var.tags
16+
})
17+
18+
response_export_values = [
19+
"id",
20+
"name"
21+
]
22+
}
23+
24+
resource "random_string" "suffix" {
25+
length = var.random_suffix_length
26+
upper = false
27+
special = false
28+
numeric = true
29+
30+
keepers = {
31+
resource_group_name = var.resource_group_name
32+
location = var.location
33+
workspace_base = var.databricks_workspace_name
34+
sku = lower(var.sku)
35+
managed_rg_base = coalesce(var.managed_resource_group_name, "rg-databricks-managed-${var.databricks_workspace_name}")
36+
}
37+
}
38+
39+
locals {
40+
suffix = var.append_random_suffix ? random_string.suffix.result : ""
41+
42+
workspace_name = var.append_random_suffix ? "${var.databricks_workspace_name}-${local.suffix}" : var.databricks_workspace_name
43+
44+
managed_rg_name = (
45+
var.managed_resource_group_name == null
46+
? null
47+
: (var.append_random_suffix ? "${var.managed_resource_group_name}-${local.suffix}" : var.managed_resource_group_name)
48+
)
49+
}
50+
51+
resource "azurerm_databricks_workspace" "ws" {
52+
name = local.workspace_name
53+
resource_group_name = var.resource_group_name
54+
location = var.location
55+
sku = lower(var.sku)
56+
57+
managed_resource_group_name = local.managed_rg_name
58+
59+
tags = var.tags
60+
61+
depends_on = [
62+
azapi_resource.resource_group
63+
]
64+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# outputs.tf
2+
3+
output "resource_group_id" {
4+
description = "The ID of the resource group."
5+
value = azapi_resource.resource_group.id
6+
}
7+
8+
output "databricks_workspace_id" {
9+
description = "The resource ID of the Databricks workspace."
10+
value = azurerm_databricks_workspace.ws.id
11+
}
12+
13+
output "databricks_workspace_name" {
14+
description = "The name of the Databricks workspace."
15+
value = azurerm_databricks_workspace.ws.name
16+
}
17+
18+
output "databricks_workspace_url" {
19+
description = "The workspace URL of the Databricks workspace."
20+
value = azurerm_databricks_workspace.ws.workspace_url
21+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# provider.tf
2+
# Provider configuration for Azure Databricks workspace deployment.
3+
4+
terraform {
5+
required_version = ">= 1.8, < 2.0"
6+
7+
required_providers {
8+
azurerm = {
9+
source = "hashicorp/azurerm"
10+
version = "~> 3.116"
11+
}
12+
13+
azapi = {
14+
source = "Azure/azapi"
15+
version = "~> 1.13"
16+
}
17+
18+
random = {
19+
source = "hashicorp/random"
20+
version = "~> 3.6"
21+
}
22+
}
23+
}
24+
25+
provider "azurerm" {
26+
features {
27+
resource_group {
28+
prevent_deletion_if_contains_resources = false
29+
}
30+
}
31+
32+
# Uses the current Azure CLI context (az login + az account set)
33+
skip_provider_registration = false
34+
}
35+
36+
provider "azapi" {
37+
# Uses the current Azure CLI context (az login + az account set)
38+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
resource_group_name = "rg-analytics-dev"
2+
location = "eastus"
3+
4+
# Databricks workspace names are unique within your subscription.
5+
# This template appends a random suffix by default to reduce collisions.
6+
databricks_workspace_name = "dbw-analytics-dev"
7+
8+
# Optional. If omitted, Databricks auto-generates the managed resource group name.
9+
# managed_resource_group_name = "rg-databricks-managed-analytics-dev"
10+
11+
# SKU: standard | premium | trial
12+
sku = "standard"
13+
14+
append_random_suffix = true
15+
random_suffix_length = 6
16+
17+
tags = {
18+
env = "dev"
19+
area = "analytics-bigdata"
20+
iac = "terraform"
21+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# variables.tf
2+
3+
variable "resource_group_name" {
4+
description = "The name of the Azure Resource Group to deploy into. This template will create the RG if it does not exist (idempotent ARM PUT)."
5+
type = string
6+
7+
validation {
8+
condition = length(trimspace(var.resource_group_name)) > 0
9+
error_message = "resource_group_name must not be empty."
10+
}
11+
}
12+
13+
variable "location" {
14+
description = "The Azure region where the Resource Group and Databricks workspace will be created."
15+
type = string
16+
17+
validation {
18+
condition = length(trimspace(var.location)) > 0
19+
error_message = "location must not be empty."
20+
}
21+
}
22+
23+
variable "databricks_workspace_name" {
24+
description = "Base name of the Azure Databricks workspace. If append_random_suffix is true, the final name will be '<base>-<suffix>'."
25+
type = string
26+
27+
validation {
28+
condition = (
29+
length(trimspace(var.databricks_workspace_name)) > 0
30+
&& can(regex("^[a-zA-Z0-9][a-zA-Z0-9-]*$", var.databricks_workspace_name))
31+
)
32+
error_message = "databricks_workspace_name must not be empty, start with alphanumeric, and contain only alphanumeric or '-'."
33+
}
34+
}
35+
36+
variable "sku" {
37+
description = "Databricks workspace SKU."
38+
type = string
39+
default = "standard"
40+
41+
validation {
42+
condition = contains(["standard", "premium", "trial"], lower(var.sku))
43+
error_message = "sku must be one of: standard, premium, trial."
44+
}
45+
}
46+
47+
variable "managed_resource_group_name" {
48+
description = "Optional base name of the Databricks managed resource group. If null/omitted, Databricks auto-generates it. If append_random_suffix is true, the final name will be '<base>-<suffix>'."
49+
type = string
50+
default = null
51+
52+
validation {
53+
condition = (
54+
var.managed_resource_group_name == null ? true : (
55+
length(try(trimspace(var.managed_resource_group_name), "")) > 0
56+
&& length(try(var.managed_resource_group_name, "")) <= (var.append_random_suffix ? (90 - 1 - var.random_suffix_length) : 90)
57+
)
58+
)
59+
error_message = "managed_resource_group_name must be 1-90 chars (or null to auto-generate) and leave room for '-<suffix>' when append_random_suffix is true."
60+
}
61+
}
62+
63+
variable "append_random_suffix" {
64+
description = "Whether to append a random suffix to the workspace name (and managed RG name, if provided) to avoid collisions."
65+
type = bool
66+
default = true
67+
}
68+
69+
variable "random_suffix_length" {
70+
description = "Length of the random suffix appended when append_random_suffix is true."
71+
type = number
72+
default = 6
73+
74+
validation {
75+
condition = var.random_suffix_length >= 4 && var.random_suffix_length <= 16
76+
error_message = "random_suffix_length must be between 4 and 16."
77+
}
78+
}
79+
80+
variable "tags" {
81+
description = "A map of tags to assign to the resources."
82+
type = map(string)
83+
default = {}
84+
}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Costa Rica
55
[![GitHub](https://img.shields.io/badge/--181717?logo=github&logoColor=ffffff)](https://github.com/)
66
[brown9804](https://github.com/brown9804)
77

8-
Last updated: 2026-02-11
8+
Last updated: 2026-02-12
99

1010
----------
1111

@@ -106,6 +106,7 @@ Last updated: 2026-02-11
106106

107107
- [Analytics and Big Data](./5_analytics-bigdata)
108108
- [Azure Data Factory](./5_analytics-bigdata/data-factory)
109+
- [Azure Databricks (Workspace)](./5_analytics-bigdata/databricks)
109110
- [Azure Synapse Analytics (Workspace)](./5_analytics-bigdata/synapse-analytics)
110111

111112
</details>

0 commit comments

Comments
 (0)