Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading