|
| 1 | +# main.tf |
| 2 | +# Creates an Azure Synapse Analytics workspace backed by an ADLS Gen2 filesystem. |
| 3 | +# Storage resources are created via AzAPI (management plane) to avoid key-based data-plane operations. |
| 4 | + |
| 5 | +resource "azurerm_resource_group" "rg" { |
| 6 | + name = var.resource_group_name |
| 7 | + location = var.location |
| 8 | + |
| 9 | + tags = var.tags |
| 10 | +} |
| 11 | + |
| 12 | +resource "random_string" "suffix" { |
| 13 | + length = var.random_suffix_length |
| 14 | + upper = false |
| 15 | + special = false |
| 16 | + numeric = true |
| 17 | + |
| 18 | + keepers = { |
| 19 | + resource_group_name = var.resource_group_name |
| 20 | + location = var.location |
| 21 | + workspace_base = var.synapse_workspace_name |
| 22 | + storage_base = var.storage_account_name |
| 23 | + managed_rg_base = var.managed_resource_group_name |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +locals { |
| 28 | + suffix = var.append_random_suffix ? random_string.suffix.result : "" |
| 29 | + 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 |
| 31 | + |
| 32 | + # Storage Account names must be lowercase alphanumeric and cannot contain dashes. |
| 33 | + storage_account_name = var.append_random_suffix ? "${var.storage_account_name}${local.suffix}" : var.storage_account_name |
| 34 | + |
| 35 | + # azurerm_synapse_workspace expects the Data Lake Gen2 filesystem id in DFS URL form. |
| 36 | + # Format: https://<accountname>.dfs.core.windows.net/<filesystem> |
| 37 | + dfs_filesystem_id = "https://${local.storage_account_name}.dfs.core.windows.net/${var.filesystem_name}" |
| 38 | +} |
| 39 | + |
| 40 | +resource "azapi_resource" "storage_account" { |
| 41 | + type = "Microsoft.Storage/storageAccounts@2021-04-01" |
| 42 | + name = local.storage_account_name |
| 43 | + location = azurerm_resource_group.rg.location |
| 44 | + parent_id = azurerm_resource_group.rg.id |
| 45 | + |
| 46 | + body = jsonencode({ |
| 47 | + kind = "StorageV2" |
| 48 | + sku = { |
| 49 | + name = "Standard_LRS" |
| 50 | + } |
| 51 | + properties = { |
| 52 | + isHnsEnabled = true |
| 53 | + minimumTlsVersion = "TLS1_2" |
| 54 | + supportsHttpsTrafficOnly = true |
| 55 | + |
| 56 | + # Often enforced by org policy; also avoids Terraform needing to use shared keys. |
| 57 | + allowSharedKeyAccess = false |
| 58 | + allowBlobPublicAccess = false |
| 59 | + } |
| 60 | + tags = var.tags |
| 61 | + }) |
| 62 | + |
| 63 | + response_export_values = [ |
| 64 | + "id", |
| 65 | + "name" |
| 66 | + ] |
| 67 | +} |
| 68 | + |
| 69 | +resource "azapi_resource" "filesystem" { |
| 70 | + type = "Microsoft.Storage/storageAccounts/blobServices/containers@2022-09-01" |
| 71 | + name = var.filesystem_name |
| 72 | + |
| 73 | + parent_id = "${azapi_resource.storage_account.id}/blobServices/default" |
| 74 | + |
| 75 | + body = jsonencode({ |
| 76 | + properties = { |
| 77 | + publicAccess = "None" |
| 78 | + } |
| 79 | + }) |
| 80 | + |
| 81 | + response_export_values = [ |
| 82 | + "id", |
| 83 | + "name" |
| 84 | + ] |
| 85 | + |
| 86 | + depends_on = [ |
| 87 | + azapi_resource.storage_account |
| 88 | + ] |
| 89 | +} |
| 90 | + |
| 91 | +resource "azurerm_synapse_workspace" "ws" { |
| 92 | + name = local.synapse_workspace_name |
| 93 | + resource_group_name = azurerm_resource_group.rg.name |
| 94 | + location = azurerm_resource_group.rg.location |
| 95 | + managed_resource_group_name = local.managed_rg_name |
| 96 | + storage_data_lake_gen2_filesystem_id = local.dfs_filesystem_id |
| 97 | + |
| 98 | + sql_administrator_login = var.sql_administrator_login |
| 99 | + sql_administrator_login_password = var.sql_administrator_password |
| 100 | + |
| 101 | + identity { |
| 102 | + type = "SystemAssigned" |
| 103 | + } |
| 104 | + |
| 105 | + tags = var.tags |
| 106 | + |
| 107 | + depends_on = [ |
| 108 | + azapi_resource.filesystem |
| 109 | + ] |
| 110 | +} |
0 commit comments