### Gallery Application Input Example Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/README.md Example of how to define gallery applications for a virtual machine. This includes the version ID and an optional order for installation. ```hcl gallery_applications = { application_1 = { version_id = "/subscriptions/{subscriptionId}/resourceGroups//providers/Microsoft.Compute/galleries/{gallery name}/applications/{application name}/versions/{version}" order = 1 } } ``` -------------------------------- ### Unattend Content Example for Reboot Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/README.md Provides an example of unattend content configuration to perform an initial reboot command during Windows setup. ```hcl additional_unattend_contents = [ { content = "shutdown /r /t 0 /c \"initial reboot\"reboot1" setting = "FirstLogonCommands" } ] ``` -------------------------------- ### Admin SSH Keys Configuration Example Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/README.md Demonstrates how to configure multiple SSH public keys for different users on the virtual machine. ```hcl admin_ssh_keys = [ { public_key = "" username = "exampleuser" }, { public_key = "" username = "examleuser2" } ] ``` -------------------------------- ### Azure Backup Configuration Example Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/README.md Example of how to configure Azure backup settings for a virtual machine. This includes specifying the recovery vault and backup policy resource IDs, and optionally excluding specific disk LUNs. ```hcl azure_backup_configurations = { arbitrary_key = { recovery_vault_resource_id = azurerm_recovery_services_vault.test_vault.id backup_policy_resource_id = azurerm_backup_policy_vm.test_policy.id exclude_disk_luns = [ 0, 1 ] } } ``` -------------------------------- ### Windows Custom Script Extension Example Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/README.md Example of how to configure a custom script extension for a Windows virtual machine. This includes settings for execution command, storage, and file URIs. ```hcl #custom script extension example - windows extensions = { custom_script_extension_windows = { name = "CustomScriptExtension" publisher = "Microsoft.Compute" type = "CustomScriptExtension" type_handler_version = "1.10" settings = <" } SETTINGS } } ``` -------------------------------- ### Basic OS Disk Configuration Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/README.md Use this snippet for a basic OS disk setup with required caching and storage account type. ```hcl os_disk = { caching = "ReadWrite" storage_account_type = "Premium_LRS" } ``` -------------------------------- ### Basic Usage of Azure Monitor Agent Extension Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/modules/extension/_header.md This example demonstrates the most basic usage of the virtual machine extension module for deploying the Azure Monitor Agent. ```terraform module "avm-res-compute-virtualmachine-extension" { source = "Azure/avm-res-compute-virtualmachine/azurerm//modules/extension" name = "AzureMonitorWindowsAgent" virtualmachine_resource_id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM" publisher = "Microsoft.Azure.Monitor" type = "AzureMonitorWindowsAgent" type_handler_version = "1.2" auto_upgrade_minor_version = true automatic_upgrade_enabled = true settings = null } ``` -------------------------------- ### Terraform Configuration for Linux Custom Data Gzip Example Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/examples/linux_custom_data_gzip/README.md This snippet defines the Terraform configuration, including required providers, resource group, networking modules, and key vault setup for the example. ```hcl terraform { required_version = ">= 1.9, < 2.0" required_providers { azapi = { source = "azure/azapi" version = "~> 2.0" } azurerm = { source = "hashicorp/azurerm" version = ">= 3.116, < 5.0" } cloudinit = { source = "hashicorp/cloudinit" version = "~> 2.0" } random = { source = "hashicorp/random" version = "~> 3.7" } } } # tflint-ignore: terraform_module_provider_declaration, terraform_output_separate, terraform_variable_separate provider "azurerm" { features { resource_group { prevent_deletion_if_contains_resources = false } key_vault { purge_soft_delete_on_destroy = true } } } module "naming" { source = "Azure/naming/azurerm" version = "0.4.2" } module "regions" { source = "Azure/avm-utl-regions/azurerm" version = "0.5.0" availability_zones_filter = true } locals { deployment_region = "canadacentral" tags = { scenario = "linux_custom_data_gzip" } } resource "random_integer" "region_index" { max = length(module.regions.regions_by_name) - 1 min = 0 } resource "random_integer" "zone_index" { max = length(module.regions.regions_by_name[local.deployment_region].zones) min = 1 } resource "azurerm_resource_group" "this_rg" { location = local.deployment_region name = module.naming.resource_group.name_unique tags = local.tags } module "vm_sku" { source = "Azure/avm-utl-sku-finder/azapi" version = "0.3.0" location = azurerm_resource_group.this_rg.location cache_results = true vm_filters = { min_vcpus = 2 max_vcpus = 2 encryption_at_host_supported = true accelerated_networking_enabled = true premium_io_supported = true location_zone = random_integer.zone_index.result } depends_on = [random_integer.zone_index] } module "natgateway" { source = "Azure/avm-res-network-natgateway/azurerm" version = "0.2.1" location = azurerm_resource_group.this_rg.location name = module.naming.nat_gateway.name_unique resource_group_name = azurerm_resource_group.this_rg.name enable_telemetry = true public_ips = { public_ip_1 = { name = "nat_gw_pip1" } } tags = local.tags } module "vnet" { source = "Azure/avm-res-network-virtualnetwork/azurerm" version = "=0.8.1" address_space = ["10.0.0.0/16"] location = azurerm_resource_group.this_rg.location resource_group_name = azurerm_resource_group.this_rg.name name = module.naming.virtual_network.name_unique subnets = { vm_subnet_1 = { name = "${module.naming.subnet.name_unique}-1" address_prefixes = ["10.0.1.0/24"] nat_gateway = { id = module.natgateway.resource_id } } } tags = local.tags } data "azurerm_client_config" "current" {} module "avm_res_keyvault_vault" { source = "Azure/avm-res-keyvault-vault/azurerm" version = "=0.10.0" location = azurerm_resource_group.this_rg.location name = "${module.naming.key_vault.name_unique}-cd-gzip" resource_group_name = azurerm_resource_group.this_rg.name tenant_id = data.azurerm_client_config.current.tenant_id network_acls = { default_action = "Allow" } role_assignments = { deployment_user_secrets = { role_definition_id_or_name = "Key Vault Secrets Officer" principal_id = data.azurerm_client_config.current.object_id } } tags = local.tags wait_for_rbac_before_secret_operations = { create = "60s" } } ``` -------------------------------- ### Linux Source Image Reference Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/README.md Example of configuring a Linux source image reference for a virtual machine. This specifies the publisher, offer, SKU, and version of the desired Linux image. ```hcl source_image_reference = { publisher = "Canonical" offer = "0001-com-ubuntu-server-focal" sku = "20_04-lts" version = "latest" } ``` -------------------------------- ### Windows Source Image Reference Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/README.md Example of configuring a Windows source image reference for a virtual machine. This specifies the publisher, offer, SKU, and version of the desired Windows image. ```hcl source_image_reference = { publisher = "MicrosoftWindowsServer" offer = "WindowsServer" sku = "2019-Datacenter" version = "latest" } ``` -------------------------------- ### Upload Installation Script to Storage Blob Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/examples/windows_w_gallery_application/README.md This snippet uploads the PowerShell installation script to the Azure Storage Account blob. ```terraform resource "azurerm_storage_blob" "app" { name = "install-script.ps1" storage_account_name = azurerm_storage_account.app_account.name storage_container_name = azurerm_storage_container.app_container.name type = "Block" source = "${path.module}/install-vscode.ps1" } ``` -------------------------------- ### Configure Data Disk Managed Disks Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/README.md Example of how to define managed disks for a virtual machine. Specify storage type, LUN, caching, and size. ```hcl #Create a new empty disk and attach it as lun 0 data_disk_managed_disks = { disk1 = { name = "testdisk1-win-lun0" storage_account_type = "Premium_LRS" lun = 0 caching = "ReadWrite" disk_size_gb = 32 } } ``` -------------------------------- ### Termination Notification Configuration Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/README.md Example of configuring termination notification settings for a virtual machine. This includes enabling the notification and setting a timeout duration. ```hcl termination_notification = { enabled = true timeout = "PT5M" } ``` -------------------------------- ### Reference AVM Module from GitHub Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/AGENTS.md This example shows how to reference an AVM module directly from a GitHub repository. Ensure the URL structure matches the AVM module's location on GitHub. ```hcl source = "https://github.com/Azure/terraform-azurerm-avm-{type}-{service}-{resource}" ``` -------------------------------- ### Configure Role Assignments for Virtual Machine Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/README.md Example of how to configure role assignments for a virtual machine. This snippet demonstrates assigning a built-in role to a principal for the virtual machine's scope. ```hcl role_assignments = { role_assignment_1 = { #assign a built-in role to the virtual machine role_definition_id_or_name = "Storage Blob Data Contributor" principal_id = data.azuread_client_config.current.object_id description = "Example for assigning a role to an existing principal for the virtual machine scope" } } ``` -------------------------------- ### Default Linux Virtual Machine Configuration Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/examples/linux_default/README.md This snippet shows a basic configuration for a Linux virtual machine, including network interface setup and source image reference. It's useful for standard VM deployments. ```terraform module "testvm" { source = "../../" location = azurerm_resource_group.this_rg.location name = module.naming.virtual_machine.name_unique resource_group_name = azurerm_resource_group.this_rg.name zone = random_integer.zone_index.result account_credentials = { key_vault_configuration = { resource_id = module.avm_res_keyvault_vault.resource_id } } enable_telemetry = var.enable_telemetry network_interfaces = { network_interface_1 = { name = module.naming.network_interface.name_unique ip_configurations = { ip_configuration_1 = { name = "${module.naming.network_interface.name_unique}-ipconfig1" private_ip_subnet_resource_id = module.vnet.subnets["vm_subnet_1"].resource_id } } } } os_type = "Linux" sku_size = module.vm_sku.sku source_image_reference = { publisher = "Canonical" offer = "0001-com-ubuntu-server-focal" sku = "20_04-lts-gen2" version = "latest" } tags = local.tags depends_on = [ module.avm_res_keyvault_vault ] } ``` -------------------------------- ### Azure Gallery Application Version Configuration Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/examples/windows_w_gallery_application/README.md This snippet configures a specific version of the gallery application, including install and remove actions, and links it to the storage blob. ```terraform resource "azurerm_gallery_application_version" "test_app_version" { gallery_application_id = azurerm_gallery_application.app_gallery_sample.id location = azurerm_gallery_application.app_gallery_sample.location name = "0.1.0" package_file = "install-script.ps1" manage_action { install = "powershell.exe -command ./install-script.ps1" remove = "powershell.exe -command ./install-script.ps1 -mode uninstall" } source { media_link = azurerm_storage_blob.app.id } target_region { name = azurerm_gallery_application.app_gallery_sample.location regional_replica_count = 1 } } ``` -------------------------------- ### Azure Storage Account for Application Scripts Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/examples/windows_w_gallery_application/README.md This snippet defines an Azure Storage Account and a container to store the application installation script. ```terraform resource "azurerm_storage_account" "app_account" { account_replication_type = "ZRS" account_tier = "Standard" location = azurerm_resource_group.this_rg.location name = module.naming.storage_account.name_unique resource_group_name = azurerm_resource_group.this_rg.name } resource "azurerm_storage_container" "app_container" { name = module.naming.storage_container.name_unique container_access_type = "blob" storage_account_id = azurerm_storage_account.app_account.id } ``` -------------------------------- ### Configure VM Additional Capabilities Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/README.md Use this snippet to enable Ultra SSD or hibernation for virtual machine data disks. Defaults to false for Ultra SSD and null for hibernation. ```hcl vm_additional_capabilities = { ultra_ssd_enabled = true } ``` -------------------------------- ### Define Marketplace Image for Virtual Machine Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/README.md Configure the 'plan' block to specify a Marketplace image for the virtual machine. This includes the image's name, product, and publisher. Be aware of potential offer purchase restrictions based on your subscription's market. ```hcl plan = { name = "17_04_02-payg-essentials" product = "cisco-8000v" publisher = "cisco" } ``` -------------------------------- ### Timeouts Configuration Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/README.md Example of configuring custom timeouts for various operations on Azure resources managed by Terraform. This allows for fine-grained control over operation durations. ```hcl timeouts = { azurerm_virtual_machine_extension = { create = "30m" delete = "30m" update = "30m" read = "5m" } azurerm_virtual_machine_run_command = { create = "30m" delete = "30m" update = "30m" read = "5m" } } ``` -------------------------------- ### Upload Script to Storage Account Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/examples/windows_w_run_command/README.md Uploads a script named 'script1' with content 'echo hello world' to the 'example-sc' container in the storage account. This script can later be executed on the VM. ```terraform resource "azurerm_storage_blob" "example1" { name = "script1" storage_account_name = azurerm_storage_account.this.name storage_container_name = azurerm_storage_container.this.name type = "Block" source_content = "echo hello world" } ``` -------------------------------- ### Deploy Virtual Machine Module Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/examples/windows_w_encryption_at_host/README.md Instantiates the Virtual Machine module to create the Windows VM. This configuration assumes Encryption at Host is handled by the Disk Encryption Set. ```terraform module "testvm" { source = "../../" ``` -------------------------------- ### Optional Bastion Host Resources Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/examples/linux_ubuntu_with_plaintext_password/README.md This commented-out section shows how to provision an Azure Bastion host, including a Standard SKU public IP address and the Bastion host resource itself, which requires a dedicated subnet. ```hcl /* Uncomment this section if you would like to include a bastion resource with this example. resource "azurerm_public_ip" "bastionpip" { name = module.naming.public_ip.name_unique location = azurerm_resource_group.this_rg.location resource_group_name = azurerm_resource_group.this_rg.name allocation_method = "Static" sku = "Standard" } resource "azurerm_bastion_host" "bastion" { name = module.naming.bastion_host.name_unique location = azurerm_resource_group.this_rg.location resource_group_name = azurerm_resource_group.this_rg.name ip_configuration { name = "${module.naming.bastion_host.name_unique}-ipconf" subnet_id = module.vnet.subnets["AzureBastionSubnet"].resource_id public_ip_address_id = azurerm_public_ip.bastionpip.id } } */ ``` -------------------------------- ### Terraform Configuration and Provider Requirements Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/examples/linux_ubuntu_w_ssh_auth/README.md Defines the Terraform version and required providers (azapi, azurerm, random, tls) for the deployment. Ensure these providers are installed and configured. ```hcl terraform { required_version = ">= 1.9, < 2.0" required_providers { azapi = { source = "azure/azapi" version = "~> 2.0" } azurerm = { source = "hashicorp/azurerm" version = ">= 3.116, < 5.0" } random = { source = "hashicorp/random" version = "~> 3.7" } tls = { source = "hashicorp/tls" version = "~> 4.0" } } } ``` -------------------------------- ### Plain Text Cloud-Init Configuration Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/examples/linux_custom_data_gzip/README.md Defines a cloud-init configuration that is not gzipped and is base64 encoded. Use this for standard cloud-init scripts. ```hcl # Plain text cloud-init (no gzip) — base64 only data "cloudinit_config" "plaintext" { gzip = false base64_encode = true part { content_type = "text/cloud-config" content = <<-CLOUDINIT #cloud-config write_files: - path: /tmp/hello-plaintext.txt content: "Hello from plaintext cloud-init" CLOUDINIT } } ``` -------------------------------- ### Simple Private IP Single NIC Configuration Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/README.md Configures a single network interface with a private IP address. This is useful for basic VM network setups. ```HCL #Simple private IP single NIC with IPV4 private address network_interfaces = { network_interface_1 = { name = "testnic1" ip_configurations = { ip_configuration_1 = { name = "testnic1-ipconfig1" private_ip_subnet_resource_id = azurerm_subnet.this_subnet_1.id } } } } ``` -------------------------------- ### Reference AVM Module from Terraform Registry Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/AGENTS.md This is an example of how to reference an AVM module from the Terraform Registry. Remember to replace '{module}' with the actual module name and adjust the version as needed. ```hcl source = "https://registry.terraform.io/modules/Azure/{module}/azurerm/latest" ``` -------------------------------- ### Optional Bastion Host Configuration Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/examples/windows_w_run_command/README.md This commented-out block shows how to enable an Azure Bastion host. It includes the creation of a static public IP address and the Bastion host resource itself, which requires a dedicated subnet named 'AzureBastionSubnet'. ```hcl /* #uncomment this block to enable bastion host resource "azurerm_public_ip" "bastionpip" { allocation_method = "Static" location = azurerm_resource_group.this_rg.location name = module.naming.public_ip.name_unique resource_group_name = azurerm_resource_group.this_rg.name sku = "Standard" } resource "azurerm_bastion_host" "bastion" { location = azurerm_resource_group.this_rg.location name = module.naming.bastion_host.name_unique resource_group_name = azurerm_resource_group.this_rg.name */ ``` -------------------------------- ### Simple Windows VM Deployment with Backup Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/examples/windows_w_backup/README.md Deploys a Windows VM with premium SSDs, host encryption, and configures Azure Backup using a specified policy and recovery vault. It requires network interfaces and OS disk configurations. ```terraform module "testvm" { source = "../../" location = azurerm_resource_group.this_rg.location name = module.naming.virtual_machine.name_unique resource_group_name = azurerm_resource_group.this_rg.name zone = random_integer.zone_index.result account_credentials = { key_vault_configuration = { resource_id = module.avm_res_keyvault_vault.resource_id } } azure_backup_configurations = { vm_backup = { recovery_vault_resource_id = azapi_resource.test_vault.id backup_policy_resource_id = azurerm_backup_policy_vm.test_policy.id } } data_disk_managed_disks = { disk1 = { name = "${module.naming.managed_disk.name_unique}-lun0" storage_account_type = "Premium_LRS" lun = 0 caching = "ReadWrite" disk_size_gb = 32 } } enable_telemetry = var.enable_telemetry encryption_at_host_enabled = true network_interfaces = { network_interface_1 = { name = module.naming.network_interface.name_unique ip_configurations = { ip_configuration_1 = { name = "${module.naming.network_interface.name_unique}-ipconfig1" private_ip_subnet_resource_id = module.vnet.subnets["vm_subnet_1"].resource_id } } } } os_disk = { caching = "ReadWrite" storage_account_type = "Premium_LRS" } os_type = "Windows" patch_assessment_mode = "AutomaticByPlatform" patch_mode = "AutomaticByPlatform" sku_size = module.vm_sku.sku source_image_reference = { publisher = "MicrosoftWindowsServer" offer = "WindowsServer" sku = "2022-datacenter-g2" version = "latest" } tags = local.tags depends_on = [ module.avm_res_keyvault_vault, azurerm_backup_policy_vm.test_policy, azapi_resource.test_vault ] } ``` -------------------------------- ### Deploy Windows Virtual Machine with Load Balancing and Security Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/examples/windows_w_load_balancing/README.md Configures a Windows VM, associating it with an Application Gateway backend pool, a Load Balancer backend pool, and an RDP NAT rule. It also links NSG and ASG resources. ```hcl module "testvm" { source = "../../" location = azurerm_resource_group.this_rg.location name = module.naming.virtual_machine.name_unique resource_group_name = azurerm_resource_group.this_rg.name zone = random_integer.zone_index.result account_credentials = { key_vault_configuration = { resource_id = module.avm_res_keyvault_vault.resource_id } } enable_telemetry = var.enable_telemetry encryption_at_host_enabled = true network_interfaces = { network_interface_1 = { name = module.naming.network_interface.name_unique application_security_groups = { asg_1 = { application_security_group_resource_id = azurerm_application_security_group.test_asg.id } } network_security_groups = { nsg_1 = { network_security_group_resource_id = module.testnsg.nsg_resource.id } } ip_configurations = { ip_configuration_1 = { app_gateway_backend_pools = { app_gw_pool_1 = { app_gateway_backend_pool_resource_id = [for value in azurerm_application_gateway.network.backend_address_pool : value.id if value.name == local.backend_address_pool_name][0] } } load_balancer_backend_pools = { lb_pool_1 = { load_balancer_backend_pool_resource_id = module.loadbalancer.azurerm_lb_backend_address_pool["pool_1"].id } } load_balancer_nat_rules = { lb_nat_rule_1 = { load_balancer_nat_rule_resource_id = module.loadbalancer.azurerm_lb_nat_rule["rdp_nat_rule_1"].id } } name = "${module.naming.network_interface.name_unique}-ipconfig1" private_ip_subnet_resource_id = module.vnet.subnets["vm_subnet_1"].resource_id } } } } os_disk = { caching = "ReadWrite" storage_account_type = "Premium_LRS" } os_type = "Windows" sku_size = module.vm_sku.sku source_image_reference = { publisher = "MicrosoftWindowsServer" offer = "WindowsServer" sku = "2022-datacenter-g2" version = "latest" } tags = local.tags depends_on = [module.avm_res_keyvault_vault, module.testnsg, module.loadbalancer, azurerm_application_security_group.test_asg, azurerm_application_gateway.network] #setting explicit dependencies to enforce destroy ordering } ``` -------------------------------- ### Assign Role to VM System Managed Identity Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/README.md Example of assigning a built-in role to the system-assigned managed identity of a virtual machine. The scope can be a resource ID string or a Terraform resource reference. ```hcl role_assignments_system_managed_identity = { role_assignment_1 = { #assign a built-in role to the system assigned managed identity scope_resource_id = "/subscriptions/0000000-0000-0000-0000-000000000000/resourceGroups/test_resource_group/providers/Microsoft.Storage/storageAccounts/examplestorageacct" role_definition_id_or_name = "Storage Blob Data Contributor" description = "Example for assigning a role to the vm system managed identity" } } ``` -------------------------------- ### Terraform Provisioner for SSH Connection to Windows VM Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/examples/windows_w_remote_access/README.md Sets up a Terraform data resource to establish an SSH connection to a Windows VM. This is useful for automated tasks or initial setup via SSH. ```hcl resource "terraform_data" "test_connection_ssh" { triggers_replace = sha512(jsonencode([ azurerm_key_vault_certificate.self_signed_winrm.versionless_secret_id, local.admin_username, local.winrms_port, module.testvm.admin_password, module.testvm.virtual_machine_azurerm.virtual_machine_id ])) connection { agent = false # for windows host = azurerm_public_ip.this.ip_address password = module.testvm.admin_password port = 22 target_platform = "windows" type = "ssh" user = local.admin_username } } ``` -------------------------------- ### Create Azure Storage Account and Container Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/examples/windows_w_run_command/README.md This resource creates a standard ZRS-replicated storage account and a blob container named 'example-sc' within it. This is used to store scripts for the VM. ```terraform resource "random_string" "name_suffix" { length = 4 special = false upper = false } resource "azurerm_storage_account" "this" { account_replication_type = "ZRS" account_tier = "Standard" location = azurerm_resource_group.this_rg.location name = "avmstorage${random_string.name_suffix.result}" resource_group_name = azurerm_resource_group.this_rg.name } resource "azurerm_storage_container" "this" { name = "example-sc" container_access_type = "blob" storage_account_id = azurerm_storage_account.this.id } ``` -------------------------------- ### Terraform Configuration for Windows VM with WinRM Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/examples/windows_w_remote_access/README.md This snippet defines the Terraform configuration, including required providers, resource group, naming conventions, and network resources for a Windows VM setup with WinRM enabled. ```hcl terraform { required_version = ">= 1.9, < 2.0" required_providers { azapi = { source = "azure/azapi" version = "~> 2.0" } azurerm = { source = "hashicorp/azurerm" version = ">= 3.116, < 5.0" } random = { source = "hashicorp/random" version = "~> 3.7" } } } # tflint-ignore: terraform_module_provider_declaration, terraform_output_separate, terraform_variable_separate provider "azurerm" { features { resource_group { prevent_deletion_if_contains_resources = false } key_vault { purge_soft_delete_on_destroy = true } } } module "naming" { source = "Azure/naming/azurerm" version = "0.4.2" } module "regions" { source = "Azure/avm-utl-regions/azurerm" version = "0.5.0" availability_zones_filter = true } locals { admin_username = "azureuser" deployment_region = "canadacentral" #temporarily pinning on single region inline_remote_exec = [ "schtasks /Create /TN \"\\AVM\\RotateWinRMListenerThumbprint\" /SC MINUTE /MO 1 /TR \""C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" -ExecutionPolicy Bypass -Command & { . 'C:\\AzureData\\w_sc_task_rotate_winrms_cert.ps1'; Update-WinRMCertificate -CommonName 'CN=${module.naming.virtual_machine.name_unique}' -WinRmsPort ${local.winrms_port} }\" /RU \"SYSTEM\" /RL HIGHEST /F" ] os_type = "Windows" tags = { scenario = "basic_windows_w_winrms" } winrms_port = 15986 } resource "random_integer" "region_index" { max = length(module.regions.regions_by_name) - 1 min = 0 } resource "random_integer" "zone_index" { max = length(module.regions.regions_by_name[local.deployment_region].zones) min = 1 } resource "azurerm_resource_group" "this_rg" { location = local.deployment_region name = module.naming.resource_group.name_unique tags = local.tags } module "vm_sku" { source = "Azure/avm-utl-sku-finder/azapi" version = "0.3.0" location = azurerm_resource_group.this_rg.location cache_results = true vm_filters = { min_vcpus = 2 max_vcpus = 2 encryption_at_host_supported = true accelerated_networking_enabled = true premium_io_supported = true location_zone = random_integer.zone_index.result } depends_on = [random_integer.zone_index] } module "natgateway" { source = "Azure/avm-res-network-natgateway/azurerm" version = "0.2.1" location = azurerm_resource_group.this_rg.location name = module.naming.nat_gateway.name_unique resource_group_name = azurerm_resource_group.this_rg.name enable_telemetry = true public_ips = { public_ip_1 = { name = "nat_gw_pip1" } } } module "vnet" { source = "Azure/avm-res-network-virtualnetwork/azurerm" version = "=0.8.1" ``` -------------------------------- ### OS Disk with Increased Size and Write Acceleration Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/README.md Configure an OS disk with a specific name, increased size, and enabled write acceleration. Ensure caching is set to 'None' and storage account type is 'Premium_LRS' for write acceleration. ```hcl os_disk = { name = "sample os disk" caching = "None" storage_account_type = "Premium_LRS" disk_size_gb = 128 write_accelerator_enabled = true } ``` -------------------------------- ### Get AVM Module Versions using Versions API Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/AGENTS.md This URL format can be used to query the Terraform Registry API for available versions of an AVM module. Replace '{module}' with the specific module name. ```plaintext https://registry.terraform.io/v1/modules/Azure/{module}/[azurerm|azure]/versions ``` -------------------------------- ### Gzipped Cloud-Init Configuration Source: https://github.com/azure/terraform-azurerm-avm-res-compute-virtualmachine/blob/main/examples/linux_custom_data_gzip/README.md Defines a cloud-init configuration that is gzipped and base64 encoded. This is useful for larger configurations to reduce data transfer size. ```hcl # Gzipped cloud-init — this is the scenario that was broken (Issue #207) data "cloudinit_config" "gzipped" { gzip = true base64_encode = true part { content_type = "text/cloud-config" content = <<-CLOUDINIT #cloud-config write_files: - path: /tmp/hello-gzipped.txt content: "Hello from gzipped cloud-init" CLOUDINIT } } ```