I am trying to create 3 VM's in Azure using Terraform count and each VM requires multiple disks. Is there any way to accomplish this ? I tried creating map of disk name and sizes but got an error that I can not use count and for_each together?
resource "azurerm_managed_disk" "this" {
for_each = var.disks
count = each.value > 0 ? var.node_count : 0
name = format("%s-%02d-datadisk", each.key, count.index + 1)
location = var.location
resource_group_name = var.resource_group_name
storage_account_type = "Premium_LRS"
create_option = "Empty"
disk_size_gb = each.value
tags = {
environment = "staging"
}
}
resource "azurerm_virtual_machine_data_disk_attachment" "this" {
for_each = var.disks
count = each.value > 0 ? var.node_count : 0
managed_disk_id = azurerm_managed_disk.this.*.id[count.index]
virtual_machine_id = azurerm_virtual_machine.this.*.id[count.index]
lun = "10"
caching = "None"
}
vm.tf
variable "disks" {
description = "Map of disk name and respective disk size"
type = map(string)
default = {
"binlog_disk" = "30"
"innodb_disk" = "20"
"data_disk" = "100"
"tmp_disk" = "10"
"backup_disk" = "150"
}
}
for_eachinazurerm_virtual_machine_data_disk_attachmenttoo?count.