Terraform v0.9.3
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"
}
}
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."
None
terraform apply fail
terraform apply no deploy the VM from an Azure Custom Image
Please list the steps required to reproduce the issue, for example:
terraform applyNone
None
the same behaviour
0.9.3
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.
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.
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.
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"
}
}
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}"
}
@brandontosch thanks, I do testing.
@kevit I will try your hack, haven't tried that solution.
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?
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.
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
Beautiful @tobiaswi, thanks!
@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.
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
@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.
@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"
}
@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.
@dobrerazvan Thats interesting, I am specifying it the same way.
@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 ?
@dobrerazvan thanks. Works as expected
I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.
Most helpful comment
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