1

I am trying to deploy virtual smart zone to Azure via Terraform, but could not succeed many times. Also I read the article existing here. Could you please give me an advice and fix it? Thank you so much.

Here are the details: Terraform Version:

tien$ terraform -v
Terraform v0.11.13
+ provider.azurerm v1.27.1
+ provider.random v2.1.2

What terraform did I write:

resource "azurerm_virtual_machine" "vsz_vm" {
  name                          = "vsz.az.example.com"
  location                      = "${var.location}"
  resource_group_name           = "${azurerm_resource_group.abc.name}"
  network_interface_ids         = ["${azurerm_network_interface.vsz_nic.id}"]
  vm_size                       = "Standard_D4_v3"

  storage_image_reference {
    id                          = "/subscriptions/4389d27e-249a-4f95-8bd6-3486c60945e7/resourceGroups/ABC/providers/Microsoft.Storage/storageAccounts/vszafb3c92c014b61ab/images/vscg-5.1.1.0.598.vhd"
  }

  storage_os_disk {
    name                        = "vszOsDisk"
    managed_disk_type           = "Premium_LRS"
    create_option               = "FromImage"
    os_type                     = "Linux"
  }

  os_profile {
    computer_name               = "vsz.az.example.com"
    admin_username              = "azure"
  }

  os_profile_linux_config {
    disable_password_authentication = true
    ssh_keys {
      path                      = "/home/azure/.ssh/authorized_keys"
      key_data                  = "${var.ssh_public_key}"
    }
  }

  tags {
    environment                 = "${var.environment}"
  }
}

then I run terraform command

tien$ terraform init --> OK
tien$ terraform plan --> OK
tien$ terraform apply --> ERROR

What issue?

Error: Error applying plan:
1 error(s) occurred:
* module.azure_example_dlc.azurerm_virtual_machine.vsz_vm: 1 error(s) occurred:
* azurerm_virtual_machine.vsz_vm: compute.VirtualMachinesClient#CreateOrUpdate: Failure sending request: StatusCode=400 -- Original Error: Code="BadRequest" Message="Id /subscriptions/4389d27e-249a-4f95-8bd6-3486c60945e7/resourceGroups/ABC/providers/Microsoft.Storage/storageAccounts/vszafb3c92c014b61ab/images/vscg-5.1.1.0.598.vhd is not a valid resource reference."

The VHD file vscg-5.1.1.0.598.vhd exists in container "images" at Storage Account "vszafb3c92c014b61ab".

Also I create the Managed Image before creating VM from Managed Image as below code:

  storage_image_reference {
    id                          = "/subscriptions/4389d27e-249a-4f95-8bd6-3486c60945e7/resourceGroups/ABC/providers/Microsoft.Storage/storageAccounts/vszafb3c92c014b61ab/images/vscg-5.1.1.0.598.vhd"
  }

  storage_os_disk {
    name                        = "vszOsDisk"
    managed_disk_type           = "Premium_LRS"
    create_option               = "FromImage"
    os_type                     = "Linux"
  }
0

This issue was solved. My bad, I did choose the wrong approach.

  • used Managed Disk.
  • used Block blob

Here are what I did to solve:

  • Re-upload the VHD image to Page blob to replace Block blob.
  • use Unmanaged Disk. Why? my VHD image stores in Page blob. Then it is backed of Virtual Hard Disk of VM.

Note

  • I used to Azure Storage Explorer to replace for uploading the VHD file. The VHD 42 GB often meet an issue when uploading via Azure Web Browser.

Azure Storage supports three types of blobs:

  • Block blob stores text and binary data, up to about 4.7 TB. Block blob are made up of blocks of data that can be managed individually.
  • Append blob are made of blocks like block blobs, but are optimized for append operation. Append blob are ideal for scenarios such as logging data from virtual machine.
  • Page blob store random access files up to 8 TB in size. Page blob store the virtual hard drive (VHD) files serve as disks for Azure virtual machine.

Finally, this is my Terraform code

resource "azurerm_virtual_machine" "vsz_vm" {
  name                          = "vsz.az.example.com"
  location                      = "${var.location}"
  resource_group_name           = "${azurerm_resource_group.abc.name}"
  network_interface_ids         = ["${azurerm_network_interface.vsz_nic.id}"]
  vm_size                       = "Standard_D4_v3"


  storage_os_disk {
    name                        = "vszOsDisk"
    # source VHD as reference
    image_uri                   = "https://vszafb3.blob.core.windows.net/images/vscg-5.1.1.0.598.vhd"
    # destination VHD to create
    vhd_uri                     = "https://vszafb3.blob.core.windows.net/images/vscg-5.1.1.0.601.vhd"
    os_type                     = "Linux"
    create_option               = "FromImage"
  }

  os_profile {
    computer_name               = "vsz.az.example.com"
    admin_username              = "azure"
  }

  os_profile_linux_config {
    disable_password_authentication = true
    ssh_keys {
      path                      = "/home/azure/.ssh/authorized_keys"
      key_data                  = "${var.ssh_public_key}"
    }
  }

This issue was solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.