'Terraform Inconsistent Azure VM Names

I have the following terraform code that creates multiple instances of a Azure VM based on a variable called nb_instance

module "create-servers" { 
  
   source              = "../../Modules/Create-Vms" 
   resource_group_name = azurerm_resource_group.rg.name
   vm_hostname         = "testvm"
   nb_instances = 2

On my create-servers module, I have the following code

resource "azurerm_virtual_machine" "vm-windows" { 
   count                         = var.nb_instances 
   name                          = "${var.vm_hostname}${format("%02d",count.index+1)}" 
   resource_group_name           = data.azurerm_resource_group.vm.name 
 

Which works great, however I'm getting inconsitent naming convention between the Azure resource and the actual server name of the VM.

Azure displays the server as testvm01 and testvm02 however the server's actual OS name is testvm-1 and testvm-2 . My desired name is testvm02 which is what I expected within the module. Is there a workaround for this to keep both names consistent?



Solution 1:[1]

You also need to set the property os_profile.computer_name

https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_machine#os_profile

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 silent