Terraform creating VM from managed disk image made in Packer

情到浓时终转凉″ 提交于 2020-01-21 18:39:49

问题


I have created a custom VM image using Packer, and now I am trying to create a new VM based on this image using Terraform, but I am confused on how I need to set up my .TF file . I can create the rest of the infrastructure okay.

I think my packer json file created a managed disk image but I am unsure how to set this up and cannot find an example online.

I am quite new to infraastructure as code and the Azure ecco system in general

main.tf

    resource "azurerm_managed_disk" "managedDisk" {
  name                 = "managed_disk_test1"
  location             = "northeurope"
  resource_group_name  = "${azurerm_resource_group.packer.name}"
  storage_account_type = "Standard_LRS"
  create_option        = "FromImage"
  image_reference_id   = "/subscriptions/33efe2dc-e7a0-4fb8-827d-8be939879420/resourceGroups/packerRG/providers/Microsoft.Compute/images/myPackerImage"
  disk_size_gb         = "1"
}


resource "azurerm_virtual_machine" "PackerVm_TEST" {
    name =  "${var.hostname}"
    location = "northeurope"
    resource_group_name   = "${azurerm_resource_group.packer.name}"
network_interface_ids = ["${azurerm_network_interface.packerNetInt_Test.id}"]
    vm_size = "Standard_D2s_v3"


    storage_os_disk {
        name            = "FromPackerImageOsDisk"
        managed_disk_type = "Standard_LRS"
        caching           = "ReadWrite"
        create_option     = "FromImage"
    }

        os_profile {
            computer_name  = "PackerVmTEST"
            admin_username = "packermakeradmin1"
            admin_password = "RMKRTest123"
          }
            os_profile_windows_config {
                enable_automatic_upgrades = "true"
                provision_vm_agent ="true"
            }

}

packer.json

{
  "builders": [{
    "type": "azure-arm",

    "client_id": "",
    "client_secret": "",
    "tenant_id": "",
    "subscription_id": "",
    "object_id": "",

    "managed_image_resource_group_name": "packerRG",
    "managed_image_name": "myPackerImage",

    "os_type": "Windows",
    "image_publisher": "MicrosoftWindowsServer",
    "image_offer": "WindowsServer",
    "image_sku": "2016-Datacenter",

    "communicator": "winrm",
    "winrm_use_ssl": "true",
    "winrm_insecure": "true",
    "winrm_timeout": "3m",
    "winrm_username": "packer",

    "azure_tags": {
        "dept": "Engineering",
        "task": "Image deployment"
    },

    "location": "northeurope",
    "vm_size": "Standard_DS2_v2"
  }],
  "provisioners": [{
    "type": "powershell",
    "inline": [
      "Add-WindowsFeature Web-Server",
      "if( Test-Path $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml ){ rm $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml -Force}",
      "& $Env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /shutdown /quiet"
    ]
  }]
}

Output when I run terraform apply

* azurerm_virtual_machine.PackerVm_TEST: compute.VirtualMachinesClient#CreateOrUpdate: Failure responding to request: StatusCode=400 -- Original Error: autorest/azure: Service returned an error. Status=400 Code="InvalidParameter" Message="Cannot specify user ima
ge overrides for a disk already defined in the specified image reference."

回答1:


There is two ways to attach a managed disk to a VM.

Either you remove the azure_managed_disk ressource and you specify the image reference in the azurerm_virtual_machine ressource. The managed disk will be automatically created and attached to the VM.

resource "azurerm_virtual_machine" "PackerVm_TEST" {
name =  "${var.hostname}"
location = "northeurope"
resource_group_name   = "${azurerm_resource_group.packer.name}"
network_interface_ids = ["${azurerm_network_interface.packerNetInt_Test.id}"]
vm_size = "Standard_D2s_v3"

storage_os_disk {
    name            = "FromPackerImageOsDisk"
    managed_disk_type = "Standard_LRS"
    caching           = "ReadWrite"
    create_option     = "FromImage"
}
storage_image_reference {
    id = "/subscriptions/33efe2dc-e7a0-4fb8-827d-8be939879420/resourceGroups/packerRG/providers/Microsoft.Compute/images/myPackerImage"
}

os_profile {
    computer_name  = "PackerVmTEST"
    admin_username = "packermakeradmin1"
    admin_password = "RMKRTest123"
  }
os_profile_windows_config {
    enable_automatic_upgrades = "true"
    provision_vm_agent ="true"
}

}

Or you add the managed disk id in the azurerm_virtual_machine ressource.

resource "azurerm_managed_disk" "managedDisk" {
  name                 = "managed_disk_test1"
  location             = "northeurope"
  resource_group_name  = "${azurerm_resource_group.packer.name}"
  storage_account_type = "Standard_LRS"
  create_option        = "FromImage"
  image_reference_id   = "/subscriptions/33efe2dc-e7a0-4fb8-827d-8be939879420/resourceGroups/packerRG/providers/Microsoft.Compute/images/myPackerImage"
  disk_size_gb         = "1"
}

resource "azurerm_virtual_machine" "PackerVm_TEST" {
    name =  "${var.hostname}"
    location = "northeurope"
    resource_group_name   = "${azurerm_resource_group.packer.name}"
    network_interface_ids = ["${azurerm_network_interface.packerNetInt_Test.id}"]
    vm_size = "Standard_D2s_v3"

    storage_os_disk {
        name            = "FromPackerImageOsDisk"
        managed_disk_id = "${azurerm_managed_disk.managedDisk.id}"
        managed_disk_type = "Standard_LRS"
        caching           = "ReadWrite"
        create_option     = "Attach"
    }
    os_profile {
        computer_name  = "PackerVmTEST"
        admin_username = "packermakeradmin1"
        admin_password = "RMKRTest123"
      }
    os_profile_windows_config {
        enable_automatic_upgrades = "true"
        provision_vm_agent ="true"
    }
}

From terraform documentation

managed_disk_id - (Optional) Specifies an existing managed disk to use by id. Can only be used when create_option is Attach.



来源:https://stackoverflow.com/questions/48079735/terraform-creating-vm-from-managed-disk-image-made-in-packer

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