Terraform StorageProfile.dataDisks.lun does not have required value(s) for image specified in storage profile

Viewed 1225

I am trying to provision a linux image from my gallery that was generalized. However I am getting an error StorageProfile.dataDisks.lun does not have required value(s) for image specified in storage profile. However, the lun for datadisk is already set?

Looking around the closest I find is created linux image in azure, cant seem to deploy however as mentioned, the lun for the data disk has been set.

I am assuming the lun is for the OS disk but I don't set it in the [documentation][1]?

The image is based on Ubuntu 18.04 with one data disk attached on lun 0. It was customised and then generalized.

  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "2.72.0"
    }
  }
}

provider "azurerm" {
  features {}
}

#get my ip
data "http" "icanhazip" {
  url = "http://icanhazip.com"
}

data "azurerm_ssh_public_key" "publickey" {
  name                = "x"
  resource_group_name = "x"
}

data "azurerm_shared_image_version" "asgi" {
  name                = "x"
  image_name          = "x-generalized"
  gallery_name        = "x"
  resource_group_name = data.azurerm_ssh_public_key.publickey.resource_group_name
}

output "public_ip" {
  value = azurerm_public_ip.publicip.ip_address
}

data "azurerm_resource_group" "main" {
  name = var.resource_group_name
}

data "azurerm_subnet" "vnet" {
  name                 = var.subnet_name
  virtual_network_name = var.virtual_network_name
  resource_group_name  = var.resource_group_name
}

# Create a Network Security Group with some rules
resource "azurerm_network_security_group" "main" {
  name                = "${var.linux_virtual_machine_name}-nsg"
  location            = data.azurerm_resource_group.main.location
  resource_group_name = data.azurerm_resource_group.main.name

  tags = {
    environment = var.environment
    region      = var.region
    role        = var.role
    owner       = var.owner
  }
}


resource "azurerm_network_security_rule" "sg-inbound" {
  name                   = "SG-inbound"
  priority               = 103
  direction              = "Inbound"
  access                 = "Deny"
  protocol               = "*"
  source_port_range      = "*"
  destination_port_range = "*"
  source_address_prefixes = [
    "x/32",
    "x/32",
    "x/32",
    "x/32"
  ]
  destination_address_prefix  = "*"
  resource_group_name         = data.azurerm_resource_group.main.name
  network_security_group_name = azurerm_network_security_group.main.name
}

resource "azurerm_network_security_rule" "sg-outbound" {
  name                   = "SG-outbound"
  priority               = 104
  direction              = "Outbound"
  access                 = "Deny"
  protocol               = "*"
  source_port_range      = "*"
  destination_port_range = "*"
  source_address_prefix  = "*"
  destination_address_prefixes = [
    "x/32",
    "x/32",
    "x/32",
    "x/32"
  ]
  resource_group_name         = data.azurerm_resource_group.main.name
  network_security_group_name = azurerm_network_security_group.main.name
}

resource "azurerm_network_security_rule" "mongorules" {
  for_each                    = local.nsgrules
  name                        = each.key
  direction                   = each.value.direction
  access                      = each.value.access
  priority                    = each.value.priority
  protocol                    = each.value.protocol
  source_port_range           = each.value.source_port_range
  destination_port_range      = each.value.destination_port_range
  source_address_prefix       = each.value.source_address_prefix
  destination_address_prefix  = each.value.destination_address_prefix
  resource_group_name         = data.azurerm_resource_group.main.name
  network_security_group_name = azurerm_network_security_group.main.name
}

# Create a network interface for VMs and attach the PIP and the NSG
# Create public IPs
resource "azurerm_public_ip" "publicip" {
  name                = "${var.linux_virtual_machine_name}-publicip"
  location            = data.azurerm_resource_group.main.location
  resource_group_name = data.azurerm_resource_group.main.name
  allocation_method   = "Static"
  sku                 = "Standard"
  domain_name_label   = var.linux_virtual_machine_name

  tags = azurerm_network_security_group.main.tags
}

# create a network interface
resource "azurerm_network_interface" "nic" {
  name                = "${var.linux_virtual_machine_name}-nic"
  location            = data.azurerm_resource_group.main.location
  resource_group_name = data.azurerm_resource_group.main.name
  tags                = azurerm_network_security_group.main.tags
  ip_configuration {
    name                          = "${var.linux_virtual_machine_name}-publicip"
    subnet_id                     = data.azurerm_subnet.vnet.id
    private_ip_address_allocation = "dynamic"
    public_ip_address_id          = azurerm_public_ip.publicip.id
  }
}


resource "azurerm_managed_disk" "dataDisk" {
  name                 = "${var.linux_virtual_machine_name}-datadisk"
  location             = data.azurerm_resource_group.main.location
  resource_group_name  = data.azurerm_resource_group.main.name
  storage_account_type = "StandardSSD_LRS"
  create_option        = "Empty"
  disk_size_gb         = 250

  tags = {
    environment = var.environment
    region      = var.region
    role        = var.role
    owner       = var.owner
    resource    = "dataDisk"
  }
}


# Create a new Virtual Machine based on the Golden Image
resource "azurerm_linux_virtual_machine" "vm" {
  name                            = var.linux_virtual_machine_name
  location                        = data.azurerm_resource_group.main.location
  resource_group_name             = data.azurerm_resource_group.main.name
  network_interface_ids           = ["${azurerm_network_interface.nic.id}"]
  size                            = var.vm_size
  admin_username                  = var.admin_username
  computer_name                   = var.linux_virtual_machine_name
  disable_password_authentication = true
  source_image_id                 = data.azurerm_shared_image_version.asgi.id

  tags = azurerm_network_security_group.main.tags

  os_disk {
    name                 = "${var.linux_virtual_machine_name}-osDisk"
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

  admin_ssh_key {
    username   = "x"
    public_key = data.azurerm_ssh_public_key.publickey.public_key
  }

 
}

resource "azurerm_virtual_machine_data_disk_attachment" "attachDisk" {
  managed_disk_id    = azurerm_managed_disk.dataDisk.id
  virtual_machine_id = azurerm_linux_virtual_machine.vm.id
  lun                = 1
  caching            = "ReadWrite"
  depends_on = [
    azurerm_linux_virtual_machine.vm
  ]
}```


  [1]: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/linux_virtual_machine
1 Answers

Please use below:

    resource "azurerm_virtual_machine_data_disk_attachment" "attachDisk" {
      managed_disk_id    = azurerm_managed_disk.dataDisk.id
      virtual_machine_id = azurerm_linux_virtual_machine.vm.id
      lun                = "1"
      caching            = "ReadWrite"
      depends_on = [
        azurerm_linux_virtual_machine.vm
      ]
}```

Instead of

resource "azurerm_virtual_machine_data_disk_attachment" "attachDisk" {
  managed_disk_id    = azurerm_managed_disk.dataDisk.id
  virtual_machine_id = azurerm_linux_virtual_machine.vm.id
  lun                = 1
  caching            = "ReadWrite"
  depends_on = [
    azurerm_linux_virtual_machine.vm
  ]
}```

OR

Please check if lun 1 is being used by any other data disk that you have created earlier.

Reference:

azurerm_virtual_machine_data_disk_attachment | Resources | hashicorp/azurerm | Terraform Registry

Related