what the best way to add additional storage to a VM, using the script below im using the count option which works, but storage_data_disk usage causes it to fail.
# Create virtual machine
resource "azurerm_linux_virtual_machine" "myterraformvm" {
count = 2
name = "testpoc0${count.index + 1}"
location = "westeurope"
resource_group_name = azurerm_resource_group.myterraformgroup.name
#network_interface_ids = azurerm_network_interface.myterraformnic.*.id
network_interface_ids = [element(azurerm_network_interface.myterraformnic.*.id, count.index + 1)]
size = "Standard_B2s"
os_disk {
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/poc.pub")
}
boot_diagnostics {
storage_account_uri = azurerm_storage_account.mystorageaccount.primary_blob_endpoint
}
tags = {
environment = "poc"
}
}
As suggested you can use Azure Managed Disk resource to create data disk and then use Azure Virtual Machine Disk Attachment to attach the disks to respective vm's.
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_B2s"
os_disk {
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")
}
}
resource "azurerm_managed_disk" "example" {
count = 2
name = "testpoc0${count.index + 1}-md"
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" {
count=2
managed_disk_id = azurerm_managed_disk.example[count.index].id
virtual_machine_id = azurerm_linux_virtual_machine.myterraformvm[count.index].id
lun ="10"
caching = "ReadWrite"
}
Outputs:
From portal:
Reference:
azurerm_virtual_machine_data_disk_attachment | Resources | hashicorp/azurerm | Terraform Registry