Skip to content

Commit 5e65f16

Browse files
committed
both RGs explained
1 parent 6beffe2 commit 5e65f16

5 files changed

Lines changed: 45 additions & 19 deletions

File tree

5_analytics-bigdata/synapse-analytics/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ Last updated: 2026-02-11
1111

1212
> This template contains Terraform configurations to create an Azure Synapse Analytics workspace backed by an ADLS Gen2 filesystem.
1313
14+
> [!IMPORTANT]
15+
> Azure Synapse always uses a **managed resource group** (configured by `managed_resource_group_name`). This is created and managed by the Synapse service itself and is required for the workspace to operate. You will see **two resource groups** in Azure: your main RG plus the Synapse-managed RG.
16+
1417
> [!IMPORTANT]
1518
> This template creates the Storage Account and filesystem via the AzAPI provider (management plane) to avoid key-based Storage data-plane operations (common in environments where shared keys are disabled by policy).
1619
@@ -32,7 +35,7 @@ Last updated: 2026-02-11
3235
| `resource_group_name` | Resource Group name to create/deploy into. | string | `"rg-analytics-dev"` |
3336
| `location` | Azure region for the deployment. | string | `"eastus"` |
3437
| `synapse_workspace_name` | Base Synapse workspace name. If suffix enabled, final is `<base>-<suffix>`. | string | `"synw-analytics-dev"` |
35-
| `managed_resource_group_name` | Base managed RG name for Synapse. If suffix enabled, final is `<base>-<suffix>`. | string | `"rg-synapse-managed-analytics-dev"` |
38+
| `managed_resource_group_name` | Optional base managed RG name for Synapse. If omitted, auto-generated. | string | `null` |
3639
| `storage_account_name` | Base storage account name. If suffix enabled, final is `<base><suffix>` (no dash). | string | `"stadlsanalyticsdev"` |
3740
| `filesystem_name` | ADLS Gen2 filesystem name (container). | string | `"synapse"` |
3841
| `sql_administrator_login` | Synapse SQL admin login. | string | `"sqladminuser"` |

5_analytics-bigdata/synapse-analytics/main.tf

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,24 @@
22
# Creates an Azure Synapse Analytics workspace backed by an ADLS Gen2 filesystem.
33
# Storage resources are created via AzAPI (management plane) to avoid key-based data-plane operations.
44

5-
resource "azurerm_resource_group" "rg" {
6-
name = var.resource_group_name
7-
location = var.location
5+
data "azurerm_client_config" "current" {}
86

9-
tags = var.tags
7+
# Resource group creation is idempotent in ARM (PUT). This will create the RG if it doesn't exist,
8+
# or update tags if it already exists.
9+
resource "azapi_resource" "resource_group" {
10+
type = "Microsoft.Resources/resourceGroups@2022-09-01"
11+
name = var.resource_group_name
12+
location = var.location
13+
parent_id = "/subscriptions/${data.azurerm_client_config.current.subscription_id}"
14+
15+
body = jsonencode({
16+
tags = var.tags
17+
})
18+
19+
response_export_values = [
20+
"id",
21+
"name"
22+
]
1023
}
1124

1225
resource "random_string" "suffix" {
@@ -20,14 +33,19 @@ resource "random_string" "suffix" {
2033
location = var.location
2134
workspace_base = var.synapse_workspace_name
2235
storage_base = var.storage_account_name
23-
managed_rg_base = var.managed_resource_group_name
36+
managed_rg_base = coalesce(var.managed_resource_group_name, "rg-synapse-managed-${var.synapse_workspace_name}")
2437
}
2538
}
2639

2740
locals {
41+
rg_id = azapi_resource.resource_group.id
42+
rg_name = var.resource_group_name
43+
location = var.location
44+
2845
suffix = var.append_random_suffix ? random_string.suffix.result : ""
2946
synapse_workspace_name = var.append_random_suffix ? "${var.synapse_workspace_name}-${local.suffix}" : var.synapse_workspace_name
30-
managed_rg_name = var.append_random_suffix ? "${var.managed_resource_group_name}-${local.suffix}" : var.managed_resource_group_name
47+
managed_rg_base = coalesce(var.managed_resource_group_name, "rg-synapse-managed-${var.synapse_workspace_name}")
48+
managed_rg_name = var.append_random_suffix ? "${local.managed_rg_base}-${local.suffix}" : local.managed_rg_base
3149

3250
# Storage Account names must be lowercase alphanumeric and cannot contain dashes.
3351
storage_account_name = var.append_random_suffix ? "${var.storage_account_name}${local.suffix}" : var.storage_account_name
@@ -40,8 +58,8 @@ locals {
4058
resource "azapi_resource" "storage_account" {
4159
type = "Microsoft.Storage/storageAccounts@2021-04-01"
4260
name = local.storage_account_name
43-
location = azurerm_resource_group.rg.location
44-
parent_id = azurerm_resource_group.rg.id
61+
location = local.location
62+
parent_id = local.rg_id
4563

4664
body = jsonencode({
4765
kind = "StorageV2"
@@ -90,8 +108,8 @@ resource "azapi_resource" "filesystem" {
90108

91109
resource "azurerm_synapse_workspace" "ws" {
92110
name = local.synapse_workspace_name
93-
resource_group_name = azurerm_resource_group.rg.name
94-
location = azurerm_resource_group.rg.location
111+
resource_group_name = local.rg_name
112+
location = local.location
95113
managed_resource_group_name = local.managed_rg_name
96114
storage_data_lake_gen2_filesystem_id = local.dfs_filesystem_id
97115

5_analytics-bigdata/synapse-analytics/outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
output "resource_group_id" {
44
description = "The ID of the resource group."
5-
value = azurerm_resource_group.rg.id
5+
value = local.rg_id
66
}
77

88
output "storage_account_id" {

5_analytics-bigdata/synapse-analytics/terraform.tfvars

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ location = "eastus"
33

44
# Synapse workspace names are globally unique.
55
# This template appends a random suffix by default to reduce collisions.
6-
synapse_workspace_name = "synw-analytics-dev"
7-
managed_resource_group_name = "rg-synapse-managed-analytics-dev"
6+
synapse_workspace_name = "synw-analytics-dev"
7+
8+
# Optional. If omitted, the template auto-generates a Synapse managed RG name.
9+
# managed_resource_group_name = "rg-synapse-managed-analytics-dev"
810

911
# Storage account names must be lowercase alphanumeric and globally unique.
1012
# This template appends a random suffix by default (without dashes).

5_analytics-bigdata/synapse-analytics/variables.tf

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# This file defines the input variables used in the Terraform configuration.
33

44
variable "resource_group_name" {
5-
description = "The name of the Azure Resource Group to create and deploy Synapse into."
5+
description = "The name of the Azure Resource Group to deploy Synapse into. If create_resource_group is true, Terraform will create this resource group."
66
type = string
77

88
validation {
@@ -36,15 +36,18 @@ variable "synapse_workspace_name" {
3636
}
3737

3838
variable "managed_resource_group_name" {
39-
description = "Base name of the managed resource group for Synapse. If append_random_suffix is true, the final name will be '<base>-<suffix>'."
39+
description = "Optional base name of the Synapse managed resource group. If null/omitted, the template auto-generates a name. If append_random_suffix is true, the final name will be '<base>-<suffix>'."
4040
type = string
41+
default = null
4142

4243
validation {
4344
condition = (
44-
length(trimspace(var.managed_resource_group_name)) > 0
45-
&& length(var.managed_resource_group_name) <= (var.append_random_suffix ? (90 - 1 - var.random_suffix_length) : 90)
45+
var.managed_resource_group_name == null ? true : (
46+
length(try(trimspace(var.managed_resource_group_name), "")) > 0
47+
&& length(try(var.managed_resource_group_name, "")) <= (var.append_random_suffix ? (90 - 1 - var.random_suffix_length) : 90)
48+
)
4649
)
47-
error_message = "managed_resource_group_name must be 1-90 chars and leave room for '-<suffix>' when append_random_suffix is true."
50+
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."
4851
}
4952
}
5053

0 commit comments

Comments
 (0)