admin管理员组

文章数量:1122846

Is it possible to use count.index in a variable name, so I can use a single map variable for multiple resources? For example

variables.tf:

variable "disk_share_sizes" {
  type        = map(number)
  description = "Sizes for VM Disks and File Shares"
  default = {
    fileshare   = 2048
    pvs0        = 20
    pvs1        = 10
    cca0        = 50
    cca1        = 80
  }
}

compute.tf

resource "azurerm_managed_disk" "vm_pvs" {
  count                = 2
  name                 = "pvs-disk-${format("%02d", count.index + 1)}"
  location             = "useast"
  resource_group_name  = "MyRGName"
  storage_account_type = "MyStorAccount"
  create_option        = "Empty"
  disk_size_gb         = var.vm_disk_sizes.pvs[count.index]
}

resource "azurerm_managed_disk" "vm_cca" {
  count                = 2
  name                 = "cca-disk-${format("%02d", count.index + 1)}"
  location             = "useast"
  resource_group_name  = "MyRGName"
  storage_account_type = "MyStorAccount"
  create_option        = "Empty"
  disk_size_gb         = var.vm_disk_sizesa[count.index]
}

This produces the below error (repeated for both resources):

│ Error: Invalid index
│
│   on compute.tf line 8, in resource "azurerm_managed_disk" "my_vms":
│  8:   disk_size_gb         = var.vm_disk_sizes.vm[count.index]
│     ├────────────────
│     │ count.index is a number
│     │ var.vm_disk_sizes.vm is a number
│
│ This value does not have any indices.

I can't use "var.vm_disk_sizes.vm${count.index}" as this is a variable name and putting it in double quotes will just make it a string. I did try using ${var.vm_disk_sizes.vm${count.index}} but it doesn't like this either

Can this be done without making the variable a list rather than a map? I'm using a map as my actual map is larger, and it would be difficult to split this into multiple lists for all the different VMs and disk sizes.

Is it possible to use count.index in a variable name, so I can use a single map variable for multiple resources? For example

variables.tf:

variable "disk_share_sizes" {
  type        = map(number)
  description = "Sizes for VM Disks and File Shares"
  default = {
    fileshare   = 2048
    pvs0        = 20
    pvs1        = 10
    cca0        = 50
    cca1        = 80
  }
}

compute.tf

resource "azurerm_managed_disk" "vm_pvs" {
  count                = 2
  name                 = "pvs-disk-${format("%02d", count.index + 1)}"
  location             = "useast"
  resource_group_name  = "MyRGName"
  storage_account_type = "MyStorAccount"
  create_option        = "Empty"
  disk_size_gb         = var.vm_disk_sizes.pvs[count.index]
}

resource "azurerm_managed_disk" "vm_cca" {
  count                = 2
  name                 = "cca-disk-${format("%02d", count.index + 1)}"
  location             = "useast"
  resource_group_name  = "MyRGName"
  storage_account_type = "MyStorAccount"
  create_option        = "Empty"
  disk_size_gb         = var.vm_disk_sizes.cca[count.index]
}

This produces the below error (repeated for both resources):

│ Error: Invalid index
│
│   on compute.tf line 8, in resource "azurerm_managed_disk" "my_vms":
│  8:   disk_size_gb         = var.vm_disk_sizes.vm[count.index]
│     ├────────────────
│     │ count.index is a number
│     │ var.vm_disk_sizes.vm is a number
│
│ This value does not have any indices.

I can't use "var.vm_disk_sizes.vm${count.index}" as this is a variable name and putting it in double quotes will just make it a string. I did try using ${var.vm_disk_sizes.vm${count.index}} but it doesn't like this either

Can this be done without making the variable a list rather than a map? I'm using a map as my actual map is larger, and it would be difficult to split this into multiple lists for all the different VMs and disk sizes.

Share Improve this question edited Nov 21, 2024 at 18:02 Dragonsys asked Nov 21, 2024 at 16:34 DragonsysDragonsys 271 silver badge5 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

Ideally, you would use the for_each meta-argument instead (since your variable is defined as a map):

resource "azurerm_managed_disk" "my_vms" {
  for_each             = var.vm_disk_sizes
  name                 = "vm-disk-${each.key}"
  location             = "useast"
  resource_group_name  = "MyRGName"
  storage_account_type = "MyStorAccount"
  create_option        = "Empty"
  disk_size_gb         = each.value
}

I did find a way to do this, using part of Marko E's answer, for anyone else who might come along later. I had to make lists for each item, but I can keep them groups by using an object mapping

I changed my variable map to an object, and then var.vm_disk_sizes.pvs[count.index] worked as expected

variables.tf

variable "disk_share_sizes" {
  description = "Sizes for VM Disks and File Shares"
  type        = object({
    pvs  = list(number)
    cca  = list(number)
  })
  default = {
    fileshare   = 2048
    pvs        = [20,10]
    cca        = [50,80]
  }
}

compute.tf

resource "azurerm_managed_disk" "vm_pvs" {
  count                = 2
  name                 = "pvs-disk-${format("%02d", count.index + 1)}"
  location             = "useast"
  resource_group_name  = "MyRGName"
  storage_account_type = "MyStorAccount"
  create_option        = "Empty"
  disk_size_gb         = var.disk_share_sizes.pvs[count.index]
}

resource "azurerm_managed_disk" "vm_cca" {
  count                = 2
  name                 = "cca-disk-${format("%02d", count.index + 1)}"
  location             = "useast"
  resource_group_name  = "MyRGName"
  storage_account_type = "MyStorAccount"
  create_option        = "Empty"
  disk_size_gb         = var.disk_share_sizes.cca[count.index]
}

本文标签: azureTerraform Use countindex in variable nameStack Overflow