Search code examples
azureterraformterraform-provider-azure

Syntax error in Terraform Azure VM resource creation


I was new to Terraform, I am trying to use Terraform to provision virtual machines in Azure infrastructure. However, I'm encountering a syntax error that I can't seem to resolve. I have provided the code snippet below and the error message I'm receiving. Can someone please help me identify and fix the issue?

Terraform Code:

resource "azurerm_virtual_machine" "my_vm" {
  name                  = "my-vm"
  location              = azurerm_resource_group.my_rg.location
  resource_group_name   = azurerm_resource_group.my_rg.name
  network_interface_ids = [azurerm_network_interface.my_nic.id]
    
  vm_size = "Standard_DS2_v2"

  storage_os_disk {
    name              = "myosdisk"
    caching           = "ReadWrite"
    create_option    = "FromImage"
    managed_disk_type = "Premium_LRS"
  }
    
  os_profile {
    computer_name  = "myvm"
    admin_username = "myadmin"
    admin_password = "myp@ssw0rd"
  }
    
  source_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServer"
    sku       = "2022-Datacenter"
    version   = "latest"
  }
}

Error Message:

Error: Unsupported block type on main.tf line 43, in resource "azurerm_virtual_machine" "my_vm": 43: source_image_reference { Blocks of type "source_image_reference" are not expected here.

I'm not sure why I'm getting this error. Is there something wrong with my syntax or configuration? Any help would be appreciated.


Solution

  • There is a note in the azurerm_virtual_machine resource:

    The azurerm_virtual_machine resource has been superseded by the azurerm_linux_virtual_machine and azurerm_windows_virtual_machine resources. The existing azurerm_virtual_machine resource will continue to be available throughout the 3.x releases however is in a feature-frozen state to maintain compatibility - new functionality will instead be added to the azurerm_linux_virtual_machine and azurerm_windows_virtual_machine resources.

    The azurerm_windows_virtual_machine resource has the argument you want.