Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
614 views
in Technique[技术] by (71.8m points)

azure - LUN numbers not ordered when deployed through terraform

I'm able to successfully deploying 5 data disks using terraform on Azure but when i check in portal these data disks are not ordered randomly like ( lun3, lun1, lun0, lun2, lun4) but when i deploy the same using ARM template they are updated in an order.

below is my code for attaching disks

resource "azurerm_managed_disk" "disk" {
  for_each             = toset([for index in local.map : j.index])
  name                 = ...
  location             = ...
  resource_group_name  = ...
  storage_account_type = ...
  create_option        = "Empty"
  disk_size_gb         = ..
}

resource "azurerm_virtual_machine_data_disk_attachment" "disk_attach" {
  for_each           = toset([for index in local.map : j.index])
  lun                = 0..n
  managed_disk_id    = azurerm_managed_disk.disk[each.key].id
  virtual_machine_id = ...
  caching            = "None"
}

terraform plan is generating resource in a sequential order but once the deployment is done the order in which they are populated in azure portal is random. I checked in documentation virtual_machine_data_disk_attachment & managed_disk but they are no flags/options/properties for such sequential ordering.

I have also checked az disk list but could not get much info to solve this.

enter image description here

Is there any way to make sure terraform generates & attaches data disks sequentially similar to how ARM template is doing i.e., in the order of LUN number ?

question from:https://stackoverflow.com/questions/65845087/lun-numbers-not-ordered-when-deployed-through-terraform

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here are two suggestions:

  1. You may consider deploying ARM Templates with Terraform. Read this blog for more details.
  2. Try to create the data disk with count or for_each referring to examples in this blog.

For example:

resource "azurerm_virtual_machine_data_disk_attachment" "managed_disk_attach" {
  count              = length(var.instances) * var.nb_disks_per_instance
  managed_disk_id    = azurerm_managed_disk.managed_disk.*.id[count.index]
  virtual_machine_id = azurerm_linux_virtual_machine.vm.*.id[ceil((count.index + 1) * 1.0 / var.nb_disks_per_instance) - 1]
  lun                = count.index + 10
  caching            = "ReadWrite"
}

or use for_each, you can use

    locals {
      vm_datadiskdisk_count_map = { for k in toset(var.instances) : k => var.nb_disks_per_instance }
      luns                      = { for k in local.datadisk_lun_map : k.datadisk_name => k.lun }
      datadisk_lun_map = flatten([
        for vm_name, count in local.vm_datadiskdisk_count_map : [
          for i in range(count) : {
            datadisk_name = format("datadisk_%s_disk%02d", vm_name, i)
            lun           = i
          }
        ]
      ])
    }



resource "azurerm_managed_disk" "managed_disk" {
  for_each             = toset([for j in local.datadisk_lun_map : j.datadisk_name])
  name                 = each.key
  location             = azurerm_resource_group.rg.location
  resource_group_name  = azurerm_resource_group.rg.name
  storage_account_type = "Standard_LRS"
  create_option        = "Empty"
  disk_size_gb         = 10
  tags                 = var.tags
}

resource "azurerm_virtual_machine_data_disk_attachment" "managed_disk_attach" {
  for_each           = toset([for j in local.datadisk_lun_map : j.datadisk_name])
  managed_disk_id    = azurerm_managed_disk.managed_disk[each.key].id
  virtual_machine_id = azurerm_linux_virtual_machine.vm[element(split("_", each.key), 1)].id
  lun                = lookup(local.luns, each.key)
  caching            = "ReadWrite"
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...