0
votes

I wish to get a marketplace image to a managed disk and then have this managed disk attached to a Azure virtual machine with Terraform.

This enables the change of the virtual machine configuration where a destroy and rebuild leaves the virtual machine intact.

I have found people with similar problems but the issues get closed off with no example left of how to get this achieved.

For the platform image

data "azurerm_platform_image" "2016-Datacenter" {
  location  = "West Europe"
  publisher = "MicrosoftWindowsServer"
  offer     = "WindowsServer"
  sku       = "2016-Datacenter"
}

Create the managed disk with the platform image

resource "azurerm_managed_disk" "Server-osdisk" {
  resource_group_name  = "rgroup"
  location             = "West Europe"
  create_option        = "FromImage"
  image_reference_id   = "${data.azurerm_platform_image.server2016.id}"
  disk_size_gb         = "127"
  name                 = "Server-osdisk"
  storage_account_type = "Standard_LRS"
}

Then reference it in the azurerm_virtual_machine

resource "azurerm_virtual_machine" "main" {
  # ...

  os_profile {
    computer_name  = "Server"
    admin_username = ""
    admin_password = ""
  }

  storage_os_disk {
    managed_disk_id = "${azurerm_managed_disk.Server-osdisk.id}"

    # os_type           = "Windows"
    managed_disk_type = "Premium_LRS"
    caching           = "ReadWrite"
    create_option     = "Attach"
    name              = "Server"
  }
}

Throws

Status=400 Code="InvalidParameter" Message="Required parameter 'osDisk.osType' is missing (null)." Target="osDisk.osType"

If you add os_type in it throws up that you cannot have os_profile which is needed for computer name, username and password

People with same problem

Terraform creating VM from managed disk image made in Packer

Tried solution but throws up the error mentioned above

What am I missing on this?

1

1 Answers

2
votes

For your issue, I have a try and make it out. You change things to yours, it is just an example. The file here:

resource "azurerm_resource_group" "main" {
  name = "acctestRG"
  location = "West Europe"
}

data "azurerm_platform_image" "linux" {
  location  = "West Europe"
  publisher = "Canonical"
  offer     = "UbuntuServer"
  sku       = "16.04-LTS"
}

resource "azurerm_managed_disk" "source" {
  name = "acctestmd1"
  location = "West Europe"
  resource_group_name = "${azurerm_resource_group.main.name}"
  storage_account_type = "Standard_LRS"
  create_option = "FromImage"
  image_reference_id = "${data.azurerm_platform_image.linux.id}"

  tags {
    environment = "staging"
  }
}

resource "azurerm_virtual_network" "main" {
  name                = "azuretestvnet"
  address_space       = ["10.0.0.0/16"]
  location            = "${azurerm_resource_group.main.location}"
  resource_group_name = "${azurerm_resource_group.main.name}"
}

resource "azurerm_subnet" "internal" {
  name                 = "internal"
  resource_group_name  = "${azurerm_resource_group.main.name}"
  virtual_network_name = "${azurerm_virtual_network.main.name}"
  address_prefix       = "10.0.2.0/24"
}

resource "azurerm_network_interface" "main" {
  name                = "azuretestnic"
  location            = "${azurerm_resource_group.main.location}"
  resource_group_name = "${azurerm_resource_group.main.name}"

  ip_configuration {
    name                          = "testconfiguration1"
    subnet_id                     = "${azurerm_subnet.internal.id}"
    private_ip_address_allocation = "dynamic"
  }
}

resource "azurerm_virtual_machine" "main" {
  name  = "azurevm"
  location = "West Europe"
  resource_group_name = "${azurerm_resource_group.main.name}"
  network_interface_ids = ["${azurerm_network_interface.main.id}"]
  vm_size = "Standard_DS1_v2"


  storage_os_disk {
    os_type = "Linux"
    name = "acctestmd1"
    managed_disk_type = "Standard_LRS"
    caching           = "ReadWrite"
    create_option     = "Attach"
    managed_disk_id   = "${azurerm_managed_disk.source.id}"
  }
  os_profile_linux_config {
    disable_password_authentication = false
  }
}

And there are some things I have met and I think you should pay attention to them.

  1. the managed_disk_type in the VM and the storage_account_type in the managed disk should be same.
  2. the name of the managed disk should be the same in both.

Hope this will help you.