I have defined computer name variable in file.tfvar file, when do terraform apply i got error like :
This is my code:
in main.tf:
resource "azurerm_windows_virtual_machine" "vm-report_identifier" {
name = var.vm_reportserver_name
resource_group_name = azurerm_resource_group.main_resource_group_identitfier.name
location = azurerm_resource_group.main_resource_group_identitfier.location
size = var.vm_ReportingServices_size
admin_username = var.vm_ReportingServices_admin_user
admin_password = var.vm_ReportingServices_admin_pass
network_interface_ids = [azurerm_network_interface.nic_ReportingServices_identifier.id]
os_disk {
caching = var.vm_ReportingServices_osdisk_caching
storage_account_type = var.vm_ReportingServices_st_type
}
source_image_reference {
publisher = var.vm_ReportingServices_publisher
offer = var.vm_ReportingServices_offer
sku = var.vm_ReportingServices_sku
version = var.vm_ReportingServices_version
}
}
in variable.tf
# Virtual Machine-ReportingServices
variable "vm_reportserver_name" { type = string }
variable "vm_ReportingServices_size" { type = string }
variable "vm_ReportingServices_admin_user" {type = string}
variable "vm_ReportingServices_admin_pass" { type = string }
variable "vm_ReportingServices_osdisk_caching" { type = string }
variable "vm_ReportingServices_st_type" { type = string }
variable "vm_ReportingServices_publisher" { type = string }
variable "vm_ReportingServices_offer" { type = string }
variable "vm_ReportingServices_sku" { type = string }
variable "vm_ReportingServices_version" { type = string }
in file.tfvar
# Virtial Machine-ReportingServices
vm_reportserver_name = "reportserver-prod"
vm_ReportingServices_size = "Standard_D2s_v3"
vm_ReportingServices_admin_user = "reportserver-prod-admin"
vm_ReportingServices_admin_pass = "Psdjhu31w0rd12945340!"
vm_ReportingServices_osdisk_caching = "ReadWrite"
vm_ReportingServices_st_type = "StandardSSD_LRS"
vm_ReportingServices_publisher = "MicrosoftWindowsServer"
vm_ReportingServices_offer = "WindowsServer"
vm_ReportingServices_sku = "2022-datacenter-azure-edition-smalldisk"
vm_ReportingServices_version = "latest"
azurerm_windows_virtual_machine.vm-report_identifier: Creating...
Error: unable to assume default computer name "computer_name" can be at most 15 characters, got 17. Please adjust the "name", or specify an explicit "computer_name"
with azurerm_windows_virtual_machine.vm-report_identifier, on main.tf line 379, in resource "azurerm_windows_virtual_machine" "vm-report_identifier":
379: resource "azurerm_windows_virtual_machine" "vm-report_identifier"
My ask is how to an explicitly specify "computer_name" to azurerm virtual machine in terraform
As mentioned above,
computer_name
can be at most 15 characters. We can see from your code that Terraform is trying to set the defaultcomputer_name
from the resource name, which is 17 characters (reportserver-prod
), hence invalid.If the default
computer_name
is invalid, it has to be explicitly defined using thecomputer_name
argument as follows:main.tf
add the following to variable.tf
add the following to file.tfvar
See the attribute definition from the resource documentation:
You will then end up with different names for the Azure Resource and for the Windows hostname