|
| 1 | +data "azurerm_client_config" "current" {} |
| 2 | + |
| 3 | +resource "azapi_resource" "resource_group" { |
| 4 | + type = "Microsoft.Resources/resourceGroups@2022-09-01" |
| 5 | + name = var.resource_group_name |
| 6 | + location = var.location |
| 7 | + parent_id = "/subscriptions/${data.azurerm_client_config.current.subscription_id}" |
| 8 | + |
| 9 | + body = jsonencode({ |
| 10 | + tags = var.tags |
| 11 | + }) |
| 12 | + |
| 13 | + response_export_values = [ |
| 14 | + "id", |
| 15 | + "name" |
| 16 | + ] |
| 17 | +} |
| 18 | + |
| 19 | +resource "random_string" "suffix" { |
| 20 | + length = var.random_suffix_length |
| 21 | + upper = false |
| 22 | + special = false |
| 23 | + numeric = true |
| 24 | + |
| 25 | + keepers = { |
| 26 | + resource_group_name = var.resource_group_name |
| 27 | + location = var.location |
| 28 | + workspace_base = var.workspace_name |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +locals { |
| 33 | + suffix = var.append_random_suffix ? random_string.suffix.result : "" |
| 34 | + hyphen_suffix = var.append_random_suffix ? "-${local.suffix}" : "" |
| 35 | + |
| 36 | + workspace_name = "${var.workspace_name}${local.hyphen_suffix}" |
| 37 | + |
| 38 | + storage_account_name_raw = lower("${var.storage_account_name_prefix}${local.suffix}") |
| 39 | + storage_account_name = substr(join("", regexall("[a-z0-9]", local.storage_account_name_raw)), 0, 24) |
| 40 | + |
| 41 | + key_vault_name_raw = lower("${var.key_vault_name_prefix}${local.suffix}") |
| 42 | + key_vault_name = substr(join("", regexall("[a-z0-9-]", local.key_vault_name_raw)), 0, 24) |
| 43 | + |
| 44 | + container_registry_name_raw = lower("${var.container_registry_name_prefix}${local.suffix}") |
| 45 | + container_registry_name = substr(join("", regexall("[a-z0-9]", local.container_registry_name_raw)), 0, 50) |
| 46 | + |
| 47 | + application_insights_name = "${var.application_insights_name_prefix}${local.hyphen_suffix}" |
| 48 | +} |
| 49 | + |
| 50 | +resource "azurerm_storage_account" "sa" { |
| 51 | + name = local.storage_account_name |
| 52 | + resource_group_name = var.resource_group_name |
| 53 | + location = var.location |
| 54 | + account_tier = "Standard" |
| 55 | + account_replication_type = "LRS" |
| 56 | + |
| 57 | + shared_access_key_enabled = false |
| 58 | + default_to_oauth_authentication = true |
| 59 | + |
| 60 | + tags = var.tags |
| 61 | + |
| 62 | + depends_on = [ |
| 63 | + azapi_resource.resource_group |
| 64 | + ] |
| 65 | + |
| 66 | + lifecycle { |
| 67 | + precondition { |
| 68 | + condition = length(local.storage_account_name) >= 3 |
| 69 | + error_message = "Generated storage account name is too short. Adjust storage_account_name_prefix/random_suffix_length." |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +resource "azurerm_key_vault" "kv" { |
| 75 | + name = local.key_vault_name |
| 76 | + location = var.location |
| 77 | + resource_group_name = var.resource_group_name |
| 78 | + |
| 79 | + tenant_id = data.azurerm_client_config.current.tenant_id |
| 80 | + sku_name = "standard" |
| 81 | + |
| 82 | + soft_delete_retention_days = 7 |
| 83 | + purge_protection_enabled = false |
| 84 | + enable_rbac_authorization = true |
| 85 | + public_network_access_enabled = true |
| 86 | + |
| 87 | + tags = var.tags |
| 88 | + |
| 89 | + depends_on = [ |
| 90 | + azapi_resource.resource_group |
| 91 | + ] |
| 92 | + |
| 93 | + lifecycle { |
| 94 | + precondition { |
| 95 | + condition = length(local.key_vault_name) >= 3 |
| 96 | + error_message = "Generated key vault name is too short. Adjust key_vault_name_prefix/random_suffix_length." |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +resource "azurerm_application_insights" "appi" { |
| 102 | + name = local.application_insights_name |
| 103 | + location = var.location |
| 104 | + resource_group_name = var.resource_group_name |
| 105 | + application_type = "web" |
| 106 | + |
| 107 | + lifecycle { |
| 108 | + ignore_changes = [ |
| 109 | + workspace_id |
| 110 | + ] |
| 111 | + } |
| 112 | + |
| 113 | + tags = var.tags |
| 114 | + |
| 115 | + depends_on = [ |
| 116 | + azapi_resource.resource_group |
| 117 | + ] |
| 118 | +} |
| 119 | + |
| 120 | +resource "azurerm_container_registry" "acr" { |
| 121 | + name = local.container_registry_name |
| 122 | + location = var.location |
| 123 | + resource_group_name = var.resource_group_name |
| 124 | + sku = "Basic" |
| 125 | + admin_enabled = false |
| 126 | + |
| 127 | + tags = var.tags |
| 128 | + |
| 129 | + depends_on = [ |
| 130 | + azapi_resource.resource_group |
| 131 | + ] |
| 132 | + |
| 133 | + lifecycle { |
| 134 | + precondition { |
| 135 | + condition = length(local.container_registry_name) >= 5 |
| 136 | + error_message = "Generated container registry name is too short. Adjust container_registry_name_prefix/random_suffix_length." |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +resource "azurerm_machine_learning_workspace" "mlw" { |
| 142 | + name = local.workspace_name |
| 143 | + location = var.location |
| 144 | + resource_group_name = var.resource_group_name |
| 145 | + |
| 146 | + application_insights_id = azurerm_application_insights.appi.id |
| 147 | + key_vault_id = azurerm_key_vault.kv.id |
| 148 | + storage_account_id = azurerm_storage_account.sa.id |
| 149 | + container_registry_id = azurerm_container_registry.acr.id |
| 150 | + |
| 151 | + public_network_access_enabled = var.public_network_access_enabled |
| 152 | + |
| 153 | + dynamic "identity" { |
| 154 | + for_each = var.enable_system_assigned_identity ? [1] : [] |
| 155 | + content { |
| 156 | + type = "SystemAssigned" |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + tags = var.tags |
| 161 | + |
| 162 | + depends_on = [ |
| 163 | + azapi_resource.resource_group |
| 164 | + ] |
| 165 | +} |
0 commit comments