Terraform creating VM from managed disk image

99封情书 提交于 2020-04-30 09:16:56

问题


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:


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.



来源:https://stackoverflow.com/questions/52723326/terraform-creating-vm-from-managed-disk-image

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!