It is not possible to add 10 OS disk for a particular VM , you can have only 1 OS disk Per VM and other than that you can attach data disks (2 disks for 1 VM VCPU)
.
So , as a solution you can use for_each
instead of count
for better functionality of the code and then you can use the below script to create the VM's with additional data disks as per your requirement:
In the below code I am creating 3 VM's with 1 OS disk and 5 Data disk each:
provider "azurerm"{
features{}
}
data "azurerm_resource_group" "example"{
name="ansumantest"
}
resource "azurerm_virtual_network" "example" {
name = "example-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" "test_subnet" {
name = "VM-subnet"
resource_group_name = data.azurerm_resource_group.example.name
virtual_network_name = azurerm_virtual_network.example.name
address_prefixes = ["10.0.0.0/24"]
}
resource "azurerm_public_ip" "myterraformpublicip" {
for_each = toset(var.instances)
name = "myPublicIP-${each.key}"
location = data.azurerm_resource_group.example.location
resource_group_name = data.azurerm_resource_group.example.name
allocation_method = "Dynamic"
}
resource "azurerm_network_interface" "myterraformnic" {
for_each= toset(var.instances)
name = "myNIC-${each.key}"
location = data.azurerm_resource_group.example.location
resource_group_name = data.azurerm_resource_group.example.name
ip_configuration {
name = "myNicConfiguration"
subnet_id = azurerm_subnet.test_subnet.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.myterraformpublicip[each.key].id
}
}
variable "instances" {
default = ["vm-test-1", "vm-test-2", "vm-test-3"]
}
variable "nb_disks_per_instance" {
default = 5
}
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_windows_virtual_machine" "example" {
for_each = toset(var.instances)
name = "example-machine${each.key}"
resource_group_name = data.azurerm_resource_group.example.name
location = data.azurerm_resource_group.example.location
size = "Standard_F2"
admin_username = "adminuser"
admin_password = "P@$$w0rd1234!"
network_interface_ids = [azurerm_network_interface.myterraformnic[format("%s", each.key)].id]
os_disk {
name = "${each.key}-OSDISK"
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
disk_size_gb = 180
}
source_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2016-Datacenter"
version = "latest"
}
}
resource "azurerm_managed_disk" "example" {
for_each = toset([for j in local.datadisk_lun_map : j.datadisk_name])
name = each.key
location = data.azurerm_resource_group.example.location
resource_group_name = data.azurerm_resource_group.example.name
storage_account_type = "Standard_LRS"
create_option = "Empty"
disk_size_gb = 10
}
resource "azurerm_virtual_machine_data_disk_attachment" "example" {
for_each = toset([for j in local.datadisk_lun_map : j.datadisk_name])
managed_disk_id = azurerm_managed_disk.example[each.key].id
virtual_machine_id = azurerm_windows_virtual_machine.example[element(split("_", each.key), 1)].id
lun = lookup(local.luns, each.key)
caching = "ReadWrite"
}
Output:



Matching the OS Disk with count is not required by providing another count inside the OS_disk Block :
For example, I created 2 VMs using terraform then its by default that 2 OS disk will also be created like below :
resource "azurerm_linux_virtual_machine" "myterraformvm" {
count = 2
name = "testpoc0${count.index + 1}"
location = data.azurerm_resource_group.example.location
resource_group_name = data.azurerm_resource_group.example.name
#network_interface_ids = azurerm_network_interface.myterraformnic.*.id
network_interface_ids = [element(azurerm_network_interface.myterraformnic.*.id, count.index + 1)]
size = "Standard_DS1_v2"
os_disk {
## count == not required here again as this is a child block inside the main VM resource block , so as many VM's will be created OS disk will also be created that many
name = "OsDisk${count.index + 1}"
caching = "ReadWrite"
storage_account_type = "Premium_LRS"
}
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
computer_name = "testpoc0${count.index}"
admin_username = "azureuser"
disable_password_authentication = true
admin_ssh_key {
username = "azureuser"
public_key = file("~/.ssh/id_rsa.pub")
}
}

