1

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 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Hello,Thanks you for taking the time to respond, I did spot the disk sizes earlier and have amended, changed the name to keep the same, the problem with this setup is that you dont specifiy the computer name, username and password so the Windows machine is created from a image but in a generalized state.
You can use vm extension to set the user for the windows vm and make sure the agent installed in it with the os_profile_windows_config. For Linux, you can use ssh key. Maybe it's an issue in Terraform.
do you mean something like os_profile_windows_config { provision_vm_agent = true enable_automatic_upgrades = true additional_unattend_config { pass = "oobeSystem" component = "Microsoft-Windows-Shell-Setup" setting_name = "AutoLogon" content = "<AutoLogon><Password><Value>Pass</Value></Password><Enabled>true</Enabled><LogonCount>1</LogonCount><Username>admin</Username></AutoLogon>" } } or the azurerm_virtual_machine_extension resource
Yeah, it's that.
Ok, I will create a xml for bypassing the sysprep and use the settings I mentioned to invoke it
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.