generated from Azure/terraform-azurerm-avm-template
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.tf
218 lines (196 loc) · 8.42 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
resource "azapi_resource" "this" {
count = var.kind == "Default" ? 1 : 0
type = "Microsoft.MachineLearningServices/workspaces@2024-07-01-preview"
body = {
properties = {
publicNetworkAccess = var.is_private ? "Disabled" : "Enabled"
applicationInsights = local.application_insights_id
hbiWorkspace = var.hbi_workspace
keyVault = local.key_vault_id
storageAccount = local.storage_account_id
containerRegistry = local.container_registry_id
description = var.workspace_description
friendlyName = coalesce(var.workspace_friendly_name, (var.is_private ? "AMLManagedVirtualNetwork" : "AMLPublic"))
systemDatastoresAuthMode = var.storage_access_type
managedNetwork = {
isolationMode = var.workspace_managed_network.isolation_mode
status = {
sparkReady = var.workspace_managed_network.spark_ready
}
outboundRules = local.outbound_rules
}
encryption = var.customer_managed_key != null ? {
status = "Enabled" # the other option is Disabled
identity = var.customer_managed_key.user_assigned_identity != null ? {
userAssignedIdentity = var.customer_managed_key.user_assigned_identity.resource_id
} : null
keyVaultProperties = {
keyVaultArmId = var.customer_managed_key.key_vault_resource_id
keyIdentifier = var.customer_managed_key.key_version == null ? data.azurerm_key_vault_key.cmk[0].id : "${data.azurerm_key_vault_key.cmk[0].versionless_id}/${var.customer_managed_key.key_version}"
}
} : null
primaryUserAssignedIdentity = var.managed_identities.system_assigned == true ? "" : var.primary_user_assigned_identity.resource_id
}
kind = var.kind
}
location = var.location
name = "aml-${var.name}"
parent_id = data.azurerm_resource_group.current.id
replace_triggers_external_values = [
var.resource_group_name # since this is the value that determines if parent_id changes, require create/destroy if it changes
]
tags = var.tags
dynamic "identity" {
for_each = local.managed_identities
content {
type = identity.value.type
identity_ids = identity.value.user_assigned_resource_ids
}
}
lifecycle {
ignore_changes = [
tags, # tags are occasionally added by Azure
parent_id # because this comes from data, the azapi provider doesn't know it ahead of time which leads to destroy/recreate instead of update
]
}
}
resource "azapi_resource" "hub" {
count = var.kind == "Hub" ? 1 : 0
type = "Microsoft.MachineLearningServices/workspaces@2024-07-01-preview"
body = {
properties = {
publicNetworkAccess = var.is_private ? "Disabled" : "Enabled"
applicationInsights = local.application_insights_id
hbiWorkspace = var.hbi_workspace
keyVault = local.key_vault_id
storageAccount = local.storage_account_id
containerRegistry = local.container_registry_id
description = var.workspace_description
friendlyName = coalesce(var.workspace_friendly_name, (var.is_private ? "HubManagedVirtualNetwork" : "PublicHub"))
systemDatastoresAuthMode = var.storage_access_type
managedNetwork = {
isolationMode = var.workspace_managed_network.isolation_mode
status = {
sparkReady = var.workspace_managed_network.spark_ready
}
outboundRules = local.outbound_rules
}
encryption = var.customer_managed_key != null ? {
status = "Enabled" # the other option is Disabled
identity = var.customer_managed_key.user_assigned_identity != null ? {
userAssignedIdentity = var.customer_managed_key.user_assigned_identity.resource_id
} : null
keyVaultProperties = {
keyVaultArmId = var.customer_managed_key.key_vault_resource_id
keyIdentifier = var.customer_managed_key.key_version == null ? data.azurerm_key_vault_key.cmk[0].id : "${data.azurerm_key_vault_key.cmk[0].versionless_id}/${var.customer_managed_key.key_version}"
}
} : null
primaryUserAssignedIdentity = var.managed_identities.system_assigned == true ? "" : var.primary_user_assigned_identity.resource_id
}
kind = var.kind
}
location = var.location
name = "hub-${var.name}"
parent_id = data.azurerm_resource_group.current.id
replace_triggers_external_values = [
var.resource_group_name # since this is the value that determines if parent_id changes, require create/destroy if it changes
]
tags = var.tags
dynamic "identity" {
for_each = local.managed_identities
content {
type = identity.value.type
identity_ids = identity.value.user_assigned_resource_ids
}
}
lifecycle {
ignore_changes = [
tags, # When the service connections for CognitiveServices are created, tags are added to this resource
parent_id # because this comes from data, the azapi provider doesn't know it ahead of time which leads to destroy/recreate instead of update
]
}
}
# Azure AI Project
resource "azapi_resource" "project" {
count = var.kind == "Project" ? 1 : 0
type = "Microsoft.MachineLearningServices/workspaces@2024-07-01-preview"
body = {
properties = {
description = var.workspace_description
friendlyName = coalesce(var.workspace_friendly_name, "AI Project")
hubResourceId = var.ai_studio_hub_id
}
kind = var.kind
}
location = var.location
name = "aihubproject-${var.name}"
parent_id = data.azurerm_resource_group.current.id
dynamic "identity" {
for_each = local.managed_identities
content {
type = identity.value.type
identity_ids = identity.value.user_assigned_resource_ids
}
}
}
# AzAPI AI Services Connection
resource "azapi_resource" "aiserviceconnection" {
count = var.aiservices.create_service_connection ? 1 : 0
type = "Microsoft.MachineLearningServices/workspaces/connections@2024-07-01-preview"
body = {
properties = {
category = "AIServices"
target = local.ai_services.properties.endpoint
authType = "AAD"
isSharedToAll = true
metadata = {
ApiType = "Azure",
ResourceId = local.ai_services_id
}
}
}
name = "aiserviceconnection${var.name}"
parent_id = local.aml_resource.id
response_export_values = ["*"]
}
# Azure Machine Learning Compute Instance
resource "azapi_resource" "computeinstance" {
count = var.create_compute_instance ? 1 : 0
type = "Microsoft.MachineLearningServices/workspaces/computes@2024-07-01-preview"
body = {
properties = {
computeLocation = local.aml_resource.location
computeType = "ComputeInstance"
disableLocalAuth = true
properties = {
enableNodePublicIp = false
vmSize = "STANDARD_DS2_V2"
}
}
}
location = local.aml_resource.location
name = "ci-${var.name}"
parent_id = local.aml_resource.id
response_export_values = ["*"]
identity {
type = "SystemAssigned"
}
}
resource "azurerm_management_lock" "this" {
count = var.lock != null ? 1 : 0
lock_level = var.lock.kind
name = coalesce(var.lock.name, "lock-${var.lock.kind}")
scope = local.aml_resource.id
notes = var.lock.kind == "CanNotDelete" ? "Cannot delete the resource or its child resources." : "Cannot delete or modify the resource or its child resources."
}
resource "azurerm_role_assignment" "this" {
for_each = var.role_assignments
principal_id = each.value.principal_id
scope = local.aml_resource.id
condition = each.value.condition
condition_version = each.value.condition_version
delegated_managed_identity_resource_id = each.value.delegated_managed_identity_resource_id
role_definition_id = strcontains(lower(each.value.role_definition_id_or_name), lower(local.role_definition_resource_substring)) ? each.value.role_definition_id_or_name : null
role_definition_name = strcontains(lower(each.value.role_definition_id_or_name), lower(local.role_definition_resource_substring)) ? null : each.value.role_definition_id_or_name
skip_service_principal_aad_check = each.value.skip_service_principal_aad_check
}