New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deploy Azure VM from custom image fail #13932

Closed
bollicino opened this Issue Apr 25, 2017 · 20 comments

Comments

Projects
None yet
9 participants
@bollicino

bollicino commented Apr 25, 2017

Terraform Version

Terraform v0.9.3

Affected Resource(s)

  • azurerm_virtual_machine

Terraform Configuration Files

resource "azurerm_resource_group" "test" {
  name     = "acctestrg"
  location = "West US"
}

resource "azurerm_virtual_network" "test" {
  name                = "acctvn"
  address_space       = ["10.0.0.0/16"]
  location            = "West US"
  resource_group_name = "${azurerm_resource_group.test.name}"
}

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

resource "azurerm_network_interface" "test" {
  name                = "acctni"
  location            = "West US"
  resource_group_name = "${azurerm_resource_group.test.name}"

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


resource "azurerm_virtual_machine" "test" {
  name                  = "acctvm"
  location              = "West US"
  resource_group_name   = "${azurerm_resource_group.test.name}"
  network_interface_ids = ["${azurerm_network_interface.test.id}"]
  vm_size               = "Standard_DS2_v2_Promo"

  storage_os_disk {
    name          = "mydisk"
    managed_disk_type = "Premium_LRS"
    create_option = "FromImage"
    image_uri     = "/subscriptions/******/resourceGroups/operation/providers/Microsoft.Compute/images/myimage"
    os_type       = "linux"
  }

  os_profile {
    computer_name  = "hostname"
    admin_username = "testadmin"
    admin_password = "Password1234!"
  }

  os_profile_linux_config {
    disable_password_authentication = false
  }

  tags {
    environment = "staging"
  }
}

Debug Output

Error applying plan:

1 error(s) occurred:

* azurerm_virtual_machine.test: 1 error(s) occurred:

* azurerm_virtual_machine.test: compute.VirtualMachinesClient#CreateOrUpdate: Failure responding to request: StatusCode=400 -- Original Error: autorest/azure: Service returned an error. Status=400 Code="InvalidParameter" Message="Parameter 'osDisk.image' is not allowed."

Panic Output

None

Expected Behavior

terraform apply fail

Actual Behavior

terraform apply no deploy the VM from an Azure Custom Image

Steps to Reproduce

Please list the steps required to reproduce the issue, for example:

  1. terraform apply

Important Factoids

None

References

None

@kevit

This comment has been minimized.

kevit commented Apr 25, 2017

the same behaviour
0.9.3

@brandontosch

This comment has been minimized.

Contributor

brandontosch commented Apr 25, 2017

So as far as I can tell the Azure API itself just does not support this behavior at the moment. I was experimenting with some workarounds, like creating an explicit managed disk with an import of a vhd and then attaching that as the os disk, but then the Azure API complains that you can't specify anything for os_profile when you are attaching the disk. Since os_profile is required in terraform right now we could technically remove that requirement and allow the VM to be provisioned with an attached managed disk, but you wouldn't have any of the os_profile capabilities. Need some guidance on how this should work and then I'm more than willing to implement the changes.

@brandontosch

This comment has been minimized.

Contributor

brandontosch commented Apr 25, 2017

Also, are you able to accomplish this through the Azure portal? Or powershell? Or any other means outside of terraform? Those examples would be very helpful. Might be a piece of the API I'm not familiar with that we could use.

@brandontosch

This comment has been minimized.

Contributor

brandontosch commented Apr 25, 2017

Ok, I think I figured it out. There is an id proprety on storage_image_reference that currently is not exposed. When exposing that and providing the uri to the image as the value then I'm able to create the VM off of an image using managed disks. I'll get a PR open soon and you can verify if this works for your scenario.

This was referenced Apr 25, 2017

@brandontosch

This comment has been minimized.

Contributor

brandontosch commented Apr 25, 2017

PR #13960 has been opened with a proposed fix.

@bollicino @kevit @dobrerazvan if you could take a build off my branch and try out your use cases, that would be much appreciated.

With the changes, the example given in this issue would become:

resource "azurerm_virtual_machine" "test" {
  name                  = "acctvm"
  location              = "West US"
  resource_group_name   = "${azurerm_resource_group.test.name}"
  network_interface_ids = ["${azurerm_network_interface.test.id}"]
  vm_size               = "Standard_DS2_v2_Promo"

  storage_image_reference {
    id = "/subscriptions/******/resourceGroups/operation/providers/Microsoft.Compute/images/myimage"
  }

  storage_os_disk {
    name          = "mydisk"
    managed_disk_type = "Premium_LRS"
    create_option = "FromImage"
    os_type       = "linux"
  }

  os_profile {
    computer_name  = "hostname"
    admin_username = "testadmin"
    admin_password = "Password1234!"
  }

  os_profile_linux_config {
    disable_password_authentication = false
  }

  tags {
    environment = "staging"
  }
}

@tombuildsstuff tombuildsstuff added the bug label Apr 26, 2017

@kevit

This comment has been minimized.

kevit commented Apr 26, 2017

actually I found out a workaround for myself

variable "image" { default = "https://newimage.blob.core.windows.net/vmcontainerc5ae490a-6ff7-4ca1-8c36-86a86e8ffdbc/osDisk.c6d4940a-6ff7-4ca1-8c36-86a86eaabe432.vhd" }

    storage_image_reference {
	publisher = "Canonical"
	offer = "UbuntuServer"
	sku = "16.04-LTS"
	version = "latest"
	}

    storage_os_disk {
	    name             = "server_osdisk"
	    create_option    = "FromImage"
	    vhd_uri             = "${var.image}"
  }

@dobrerazvan

This comment has been minimized.

dobrerazvan commented Apr 26, 2017

@brandontosch thanks, I do testing.

@kevit I will try your hack, haven't tried that solution.

@brandontosch

This comment has been minimized.

Contributor

brandontosch commented Apr 27, 2017

If you're trying to accomplish this without managed disks then couldn't you just use the image_uri as provided in the original config and remove the managed_disk_type field or is that not working either?

@erikvdbergh

This comment has been minimized.

erikvdbergh commented Apr 28, 2017

Hi @brandontosch ,

When working on a workaround for issue #13985 , I encountered this bug. I built your branch and it fixes the problem for me (with managed disks). Many thanks.

@tobiaswi

This comment has been minimized.

tobiaswi commented May 1, 2017

Hi @brandontosch,

just a heads up that Microsoft put up a new Azure ARM Template docu page a few weeks ago if you aren't already aware:
https://docs.microsoft.com/en-us/azure/templates/
It was a godsent for myself, helped me get managed disks to work with custom images (arm templates, not terraform. just starting with terrafrom atm.)

Here is the direct link to the Microsoft.Compute part:
https://docs.microsoft.com/en-us/azure/templates/microsoft.compute/virtualmachines

@brandontosch

This comment has been minimized.

Contributor

brandontosch commented May 1, 2017

Beautiful @tobiaswi, thanks!

@dobrerazvan

This comment has been minimized.

dobrerazvan commented May 4, 2017

@bollicino @brandontosch Works as designed. I was able to create a new vm image and spin up a vm with managed disk as os disk. Just be aware that at image creation the vhd must be stored on a non encrypted storage account.

@meher1993

This comment has been minimized.

meher1993 commented May 15, 2017

Hi,
@dobrerazvan @brandontosch
Trying to create a VM using managed disk as os disk. I have a custom image I was using for vhd. This is how I am trying to

storage_image_reference {
id = "https://xxxxxx.blob.core.windows.net/osdisks/CentOS68.vhd"
}
storage_os_disk {
name = "myosdisk3"
managed_disk_type="Standard_LRS"
caching = "ReadWrite"
os_type = "linux"
create_option = "FromImage"
}

Terraform is throwing an error that storage_image_reference.0.offer , storage_image_reference.0.publisher ,storage_image_reference.0.sku are not set. Any workarounds for this

@dobrerazvan

This comment has been minimized.

dobrerazvan commented May 16, 2017

@meher1993 you need to create a virtual machine image first. Id doesn't work with a vhd: https://docs.microsoft.com/en-us/azure/virtual-machines/windows/capture-image-resource.

@meher1993

This comment has been minimized.

meher1993 commented May 16, 2017

@dobrerazvan Can't believe I missed that step. So, I have created an image and specified the id , still facing the same error, that the required fields are not set in the storage_image_reference.

I am giving it like this

storage_image_reference {
id = "/subscriptions/subscription_id/resourceGroups/ResourceGroupName/providers/Microsoft.Compute/images/Imagename"
}

@dobrerazvan

This comment has been minimized.

dobrerazvan commented May 16, 2017

@meher1993 I have something like and is working.

storage_image_reference {
   id = "/subscriptions/xxx/resourceGroups/custom-images/providers/Microsoft.Compute/images/debian"
 }

 storage_os_disk {
   name          = "mydisk"
   managed_disk_type = "Premium_LRS"
   create_option = "FromImage"
   os_type       = "linux"
 }

Make sure you use the terraform binary build from the branch.

@meher1993

This comment has been minimized.

meher1993 commented May 16, 2017

@dobrerazvan Thats interesting, I am specifying it the same way.

@meher1993

This comment has been minimized.

meher1993 commented May 18, 2017

@erikvdbergh @dobrerazvan I wasn't able to build from the branch you pointed to. Whenever I try "make" it fails with errors. Can you point me to your successful build for darwin-amd64 ?

@meher1993

This comment has been minimized.

meher1993 commented May 19, 2017

@dobrerazvan thanks. Works as expected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment