Skip to content

Commit 7065b2d

Browse files
feat: improve mi example (#123)
* feat: improve mi example * Add more content and improve example * updates and fixes * tidy
1 parent 3d74191 commit 7065b2d

34 files changed

Lines changed: 447 additions & 20 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,4 @@ avm.tflint_module.merged.hcl
4949
examples/*/policy
5050
*.mptfbackup
5151
.avm
52+
examples/managed_instance/scripts.zip

examples/managed_instance/README.md

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ terraform {
1414
required_version = ">= 1.9, < 2.0"
1515
1616
required_providers {
17+
archive = {
18+
source = "hashicorp/archive"
19+
version = "~> 2.7"
20+
}
1721
azapi = {
1822
source = "Azure/azapi"
1923
version = "~> 2.4"
@@ -338,19 +342,26 @@ resource "azurerm_key_vault_secret" "registry_string" {
338342
resource "azurerm_key_vault_secret" "registry_dword" {
339343
key_vault_id = azapi_resource.key_vault.id
340344
name = "registry-dword-value"
341-
value = "23FE" # Hexadecimal string for DWORD value of 35
345+
value = "336" # Must be an Integer
342346
343347
depends_on = [azapi_resource.role_assignment_kv_secrets_officer]
344348
}
345349
350+
data "archive_file" "scripts" {
351+
type = "zip"
352+
source_dir = "${path.module}/scripts"
353+
output_path = "${path.module}/scripts.zip"
354+
output_file_mode = "0644"
355+
}
356+
346357
# Upload scripts.zip as a placeholder for the install script package.
347-
# Replace the source with your own scripts.zip file.
348358
resource "azurerm_storage_blob" "scripts_zip" {
349359
name = "scripts.zip"
350360
storage_account_name = azapi_resource.storage_account.name
351361
storage_container_name = azapi_resource.blob_container.name
352362
type = "Block"
353-
source = "${path.module}/scripts.zip"
363+
content_md5 = data.archive_file.scripts.output_md5
364+
source = data.archive_file.scripts.output_path
354365
355366
depends_on = [azapi_resource.role_assignment_blob_reader, azapi_resource.role_assignment_blob_contributor_current_user]
356367
}
@@ -363,8 +374,7 @@ resource "azapi_resource" "role_assignment_blob_contributor_current_user" {
363374
type = "Microsoft.Authorization/roleAssignments@2022-04-01"
364375
body = {
365376
properties = {
366-
principalId = data.azapi_client_config.this.object_id
367-
#principalType = "User"
377+
principalId = data.azapi_client_config.this.object_id
368378
roleDefinitionId = "/subscriptions/${data.azapi_client_config.this.subscription_id}/providers/Microsoft.Authorization/roleDefinitions/ba92f5b4-2d11-453d-a403-e96b0029c9fe"
369379
}
370380
}
@@ -373,8 +383,7 @@ resource "azapi_resource" "role_assignment_blob_contributor_current_user" {
373383
depends_on = [azapi_resource.blob_container]
374384
}
375385
376-
# This is the module call
377-
# Windows Managed Instance uses isCustomMode, install scripts, and plan default identity
386+
# This is the module call to create the App Service Managed Instance plan with custom configuration using install scripts, registry adapters, and storage mounts.
378387
module "test" {
379388
source = "../.."
380389
@@ -384,9 +393,10 @@ module "test" {
384393
parent_id = azapi_resource.resource_group.id
385394
enable_telemetry = var.enable_telemetry
386395
# Install scripts - references the scripts.zip blob in the storage account
396+
# The install script logs can be found in C:\InstallScripts on the VM instances
387397
install_scripts = [
388398
{
389-
name = "FontInstaller"
399+
name = "CustomInstaller"
390400
source = {
391401
type = "RemoteAzureBlob"
392402
source_uri = "https://${azapi_resource.storage_account.name}.blob.core.windows.net/${azapi_resource.blob_container.name}/scripts.zip"
@@ -406,14 +416,14 @@ module "test" {
406416
# Registry adapters - configure Windows registry keys via Key Vault references
407417
registry_adapters = [
408418
{
409-
registry_key = "HKEY_LOCAL_MACHINE/SOFTWARE/MyApp/Config" # Registry key must start with HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER, or HKEY_USERS and contain at least one forward slash.
419+
registry_key = "HKEY_LOCAL_MACHINE/SOFTWARE/MyApp1/RegistryAdapterString" # Registry key must start with HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER, or HKEY_USERS and contain at least one forward slash.
410420
type = "String"
411421
key_vault_secret_reference = {
412422
secret_uri = "https://${azapi_resource.key_vault.name}.vault.azure.net/secrets/${azurerm_key_vault_secret.registry_string.name}"
413423
}
414424
},
415425
{
416-
registry_key = "HKEY_LOCAL_MACHINE/SOFTWARE/MyApp/DWordData" # Registry key must start with HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER, or HKEY_USERS and contain at least one forward slash.
426+
registry_key = "HKEY_LOCAL_MACHINE/SOFTWARE/MyApp1/RegistryAdapterDWORD" # Registry key must start with HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER, or HKEY_USERS and contain at least one forward slash.
417427
type = "DWORD"
418428
key_vault_secret_reference = {
419429
secret_uri = "https://${azapi_resource.key_vault.name}.vault.azure.net/secrets/${azurerm_key_vault_secret.registry_dword.name}"
@@ -455,6 +465,7 @@ resource "azapi_resource" "web_app" {
455465
properties = {
456466
serverFarmId = module.test.resource_id
457467
siteConfig = {
468+
alwaysOn = true # NOTE: If the web app is not deployed and running, you will not be able to RDP onto the instances
458469
netFrameworkVersion = "v10.0"
459470
metadata = [
460471
{
@@ -477,6 +488,8 @@ The following requirements are needed by this module:
477488

478489
- <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) (>= 1.9, < 2.0)
479490

491+
- <a name="requirement_archive"></a> [archive](#requirement\_archive) (~> 2.7)
492+
480493
- <a name="requirement_azapi"></a> [azapi](#requirement\_azapi) (~> 2.4)
481494

482495
- <a name="requirement_azurerm"></a> [azurerm](#requirement\_azurerm) (>= 3.0, < 5.0)
@@ -508,6 +521,7 @@ The following resources are used by this module:
508521
- [azurerm_key_vault_secret.storage_key](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/key_vault_secret) (resource)
509522
- [azurerm_storage_blob.scripts_zip](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/storage_blob) (resource)
510523
- [random_integer.region_index](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/integer) (resource)
524+
- [archive_file.scripts](https://registry.terraform.io/providers/hashicorp/archive/latest/docs/data-sources/file) (data source)
511525
- [azapi_client_config.this](https://registry.terraform.io/providers/Azure/azapi/latest/docs/data-sources/client_config) (data source)
512526
- [azapi_resource_action.storage_account_keys](https://registry.terraform.io/providers/Azure/azapi/latest/docs/data-sources/resource_action) (data source)
513527

examples/managed_instance/main.tf

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ terraform {
22
required_version = ">= 1.9, < 2.0"
33

44
required_providers {
5+
archive = {
6+
source = "hashicorp/archive"
7+
version = "~> 2.7"
8+
}
59
azapi = {
610
source = "Azure/azapi"
711
version = "~> 2.4"
@@ -326,19 +330,26 @@ resource "azurerm_key_vault_secret" "registry_string" {
326330
resource "azurerm_key_vault_secret" "registry_dword" {
327331
key_vault_id = azapi_resource.key_vault.id
328332
name = "registry-dword-value"
329-
value = "23FE" # Hexadecimal string for DWORD value of 35
333+
value = "336" # Must be an Integer
330334

331335
depends_on = [azapi_resource.role_assignment_kv_secrets_officer]
332336
}
333337

338+
data "archive_file" "scripts" {
339+
type = "zip"
340+
source_dir = "${path.module}/scripts"
341+
output_path = "${path.module}/scripts.zip"
342+
output_file_mode = "0644"
343+
}
344+
334345
# Upload scripts.zip as a placeholder for the install script package.
335-
# Replace the source with your own scripts.zip file.
336346
resource "azurerm_storage_blob" "scripts_zip" {
337347
name = "scripts.zip"
338348
storage_account_name = azapi_resource.storage_account.name
339349
storage_container_name = azapi_resource.blob_container.name
340350
type = "Block"
341-
source = "${path.module}/scripts.zip"
351+
content_md5 = data.archive_file.scripts.output_md5
352+
source = data.archive_file.scripts.output_path
342353

343354
depends_on = [azapi_resource.role_assignment_blob_reader, azapi_resource.role_assignment_blob_contributor_current_user]
344355
}
@@ -351,8 +362,7 @@ resource "azapi_resource" "role_assignment_blob_contributor_current_user" {
351362
type = "Microsoft.Authorization/roleAssignments@2022-04-01"
352363
body = {
353364
properties = {
354-
principalId = data.azapi_client_config.this.object_id
355-
#principalType = "User"
365+
principalId = data.azapi_client_config.this.object_id
356366
roleDefinitionId = "/subscriptions/${data.azapi_client_config.this.subscription_id}/providers/Microsoft.Authorization/roleDefinitions/ba92f5b4-2d11-453d-a403-e96b0029c9fe"
357367
}
358368
}
@@ -361,8 +371,7 @@ resource "azapi_resource" "role_assignment_blob_contributor_current_user" {
361371
depends_on = [azapi_resource.blob_container]
362372
}
363373

364-
# This is the module call
365-
# Windows Managed Instance uses isCustomMode, install scripts, and plan default identity
374+
# This is the module call to create the App Service Managed Instance plan with custom configuration using install scripts, registry adapters, and storage mounts.
366375
module "test" {
367376
source = "../.."
368377

@@ -372,9 +381,10 @@ module "test" {
372381
parent_id = azapi_resource.resource_group.id
373382
enable_telemetry = var.enable_telemetry
374383
# Install scripts - references the scripts.zip blob in the storage account
384+
# The install script logs can be found in C:\InstallScripts on the VM instances
375385
install_scripts = [
376386
{
377-
name = "FontInstaller"
387+
name = "CustomInstaller"
378388
source = {
379389
type = "RemoteAzureBlob"
380390
source_uri = "https://${azapi_resource.storage_account.name}.blob.core.windows.net/${azapi_resource.blob_container.name}/scripts.zip"
@@ -394,14 +404,14 @@ module "test" {
394404
# Registry adapters - configure Windows registry keys via Key Vault references
395405
registry_adapters = [
396406
{
397-
registry_key = "HKEY_LOCAL_MACHINE/SOFTWARE/MyApp/Config" # Registry key must start with HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER, or HKEY_USERS and contain at least one forward slash.
407+
registry_key = "HKEY_LOCAL_MACHINE/SOFTWARE/MyApp1/RegistryAdapterString" # Registry key must start with HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER, or HKEY_USERS and contain at least one forward slash.
398408
type = "String"
399409
key_vault_secret_reference = {
400410
secret_uri = "https://${azapi_resource.key_vault.name}.vault.azure.net/secrets/${azurerm_key_vault_secret.registry_string.name}"
401411
}
402412
},
403413
{
404-
registry_key = "HKEY_LOCAL_MACHINE/SOFTWARE/MyApp/DWordData" # Registry key must start with HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER, or HKEY_USERS and contain at least one forward slash.
414+
registry_key = "HKEY_LOCAL_MACHINE/SOFTWARE/MyApp1/RegistryAdapterDWORD" # Registry key must start with HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER, or HKEY_USERS and contain at least one forward slash.
405415
type = "DWORD"
406416
key_vault_secret_reference = {
407417
secret_uri = "https://${azapi_resource.key_vault.name}.vault.azure.net/secrets/${azurerm_key_vault_secret.registry_dword.name}"
@@ -443,6 +453,7 @@ resource "azapi_resource" "web_app" {
443453
properties = {
444454
serverFarmId = module.test.resource_id
445455
siteConfig = {
456+
alwaysOn = true # NOTE: If the web app is not deployed and running, you will not be able to RDP onto the instances
446457
netFrameworkVersion = "v10.0"
447458
metadata = [
448459
{
-181 Bytes
Binary file not shown.
224 KB
Binary file not shown.
216 KB
Binary file not shown.
223 KB
Binary file not shown.
216 KB
Binary file not shown.
Binary file not shown.
217 KB
Binary file not shown.

0 commit comments

Comments
 (0)