terraformterraform-provider-azureazure-rm

while create virtual linux machine from custom image which has data disk - StatusCode=400 – Original Error: Code=“InvalidParameter”


Trying to create a azure linux vm from a custom image - that image has 1 data disk in already. And also here i tried to add second data disk by creating the azurerm_managed_disk.

But when run the terraform plan/apply , it will give below error

Error: creating Windows Virtual Machine: (Name "xxxxxxxxx" / Resource Group "xxxxxxxx"): compute.VirtualMachinesClient#CreateOrUpdate: Failure sending request: StatusCode=400 -- Original Error: Code="InvalidParameter" Message="StorageProfile.dataDisks.lun does not have required value(s) for image specified in storage profile." Target="storageProfile"

Here is the terraform code .How to resolve this issue ?

resource "azurerm_managed_disk" "my_disk" {
name                 = "new-data-disk"
location             = xxxxxxxxxxxx
resource_group_name  = xxxxxxxxxxx
storage_account_type = "Premium_LRS"
create_option        = "Empty"
disk_size_gb         = 100
zones                = local.availability_zone
}

resource "azurerm_virtual_machine" "my_azure_vm" {

storage_os_disk {
name              = "${local.my_vm_name}-os"
caching           = "ReadWrite"
create_option     = "FromImage"
managed_disk_type = "Premium_LRS"
disk_size_gb      = "64"
}

storage_data_disk {
name            = "${local.my_vm_name}-new_disk-1"
caching         = var.cache
managed_disk_id = azurerm_managed_disk.my_disk.id
create_option   = "Attach"
lun             = 2
disk_size_gb    = 100
}

 ... xxxxxxxxxxxxxx
}

Solution

  • While creating a virtual Linux machine from the custom image which has data disk - StatusCode=400 – Original Error: Code=“InvalidParameter” error persists.

    First of all, you can create the Azure VM from a custom image through Terraform, no matter how you create the image.

    To check the Azure gallery image resource ID follow the below steps.

    Go to Azure compute galleries > Select your gallery image > definition > select your image > Select Json View

    enter image description here

    The error message you're encountering suggests that the lun (Logical Unit Number) parameter for the data disk is not set correctly. The lun parameter specifies the position of the data disk in the storage profile, and it must match the configuration of the custom image you're using.

    Terraform Code:

        provider "azurerm"  {
          features {}
        }
        
        data "azurerm_resource_group" "example" {
          name = "vmresourcegroup"
        }
        resource "azurerm_virtual_network" "main" {
          name                = "vmgallery-network"
          address_space       = ["10.0.0.0/16"]
          location            = data.azurerm_resource_group.example.location
          resource_group_name = data.azurerm_resource_group.example.name
        }
        resource "azurerm_subnet" "internal" {
          name                 = "galleryinternal"
          resource_group_name  = data.azurerm_resource_group.example.name
          virtual_network_name = azurerm_virtual_network.main.name
          address_prefixes     = ["10.0.2.0/24"]
        }
        resource "azurerm_network_interface" "main" {
          name                = "vmgallery-nic"
          location            = data.azurerm_resource_group.example.location
          resource_group_name = data.azurerm_resource_group.example.name
        
          ip_configuration {
            name                          = "testconfiguration1"
            subnet_id                     = azurerm_subnet.internal.id
            private_ip_address_allocation = "Dynamic"
          }
        }
    
        resource "azurerm_virtual_machine" "testingvm" {
          name                        = "galleryimage"
          resource_group_name         = data.azurerm_resource_group.example.name
          location                    = data.azurerm_resource_group.example.location
          vm_size                     = "Standard_DS1_v2"
          network_interface_ids       = [azurerm_network_interface.main.id]
        
          storage_image_reference {
            id = "/subscriptions/ahdjd d f hff-bd66-bfhffhf/resourceGroups/vmresourcegroup/providers/Microsoft.Compute/galleries/galleryimage/images/demovmimage"  
            }
          
         storage_os_disk {
           name              = "galleryimage-os"
           caching           = "ReadWrite"
           create_option     = "FromImage"
           managed_disk_type = "Premium_LRS"
           disk_size_gb      = "64"
         }
    
        storage_data_disk {
          name            = "galleryimage-os1"
          caching         = "ReadWrite"
          managed_disk_id = azurerm_managed_disk.my_disk.id
          create_option   = "Attach"
          lun             = 1  # Update the lun value to match the existing      data disk
          disk_size_gb    = 100
       }
     
         os_profile {
           computer_name  = "demovmimage"
           admin_username = "Vinay"
           admin_password = "Password1234!"
         }
         os_profile_linux_config {
            disable_password_authentication = false
        }
      }
    

    Output:

    enter image description here

    enter image description here