Azure Compute Virtual Machine Data Disk Attachment

This page shows how to write Terraform for Compute Virtual Machine Data Disk Attachment and write them securely.

azurerm_virtual_machine_data_disk_attachment (Terraform)

The Virtual Machine Data Disk Attachment in Compute can be configured in Terraform with the resource name azurerm_virtual_machine_data_disk_attachment. The following sections describe 9 examples of how to use the resource and its parameters.

Example Usage from GitHub

main.tf#L14
resource "azurerm_virtual_machine_data_disk_attachment" "disk0" {
  managed_disk_id    = azurerm_managed_disk.disk0.id
  virtual_machine_id = var.vmID
  lun                = "0"
  caching            = var.caching
}
main.tf#L9
resource "azurerm_virtual_machine_data_disk_attachment" "disk0" {
  managed_disk_id    = azurerm_managed_disk.disk0.id
  virtual_machine_id = var.vmID
  lun                = "0"
  caching            = var.caching
}
main.tf#L108
resource "azurerm_virtual_machine_data_disk_attachment" "diskattachment_vhgrrpoddb01" {
  count              = 4
  managed_disk_id    = element(azurerm_managed_disk.managedisk_vhgrrpoddb01.*.id, count.index)
  virtual_machine_id = element(azurerm_virtual_machine.vm.*.id, element(var.disk_vm_vhgrrpoddb01, count.index))
  lun                = element(var.disk_attachment_lun1,count.index)
  caching            = element(var.disk_attachment_caching1,count.index)
main.tf#L61
resource "azurerm_virtual_machine_data_disk_attachment" "diskbw4hana-1" {
  managed_disk_id    = azurerm_managed_disk.diskbw4hana-1.id
  virtual_machine_id = azurerm_virtual_machine.bw4hanavm.id
  caching            = "ReadWrite"
  lun                = 10
}
main.tf#L59
resource "azurerm_virtual_machine_data_disk_attachment" "diskmdcposolman-1" {
  managed_disk_id    = azurerm_managed_disk.diskmdcposolman-1.id
  virtual_machine_id = azurerm_virtual_machine.mdcposolmanvm.id
  caching            = "ReadWrite"
  lun                = 10
}
main.tf#L41
resource "azurerm_virtual_machine_data_disk_attachment" "sql_data" {
  virtual_machine_id = azurerm_windows_virtual_machine.vm01.id
  managed_disk_id    = azurerm_managed_disk.sql_data.id
  lun                = 0
  caching            = "None"
}
main.tf#L73
resource "azurerm_virtual_machine_data_disk_attachment" "gamedisk_ultradisk_attachment" {
  managed_disk_id    = azurerm_managed_disk.gamedisk_ultradisk.id
  virtual_machine_id = data.azurerm_virtual_machine.vm.id
  lun                = "10"
  caching            = "None"
}
main.tf#L130
resource "azurerm_virtual_machine_data_disk_attachment" "premium1" {
  managed_disk_id    = azurerm_managed_disk.premium1.id
  virtual_machine_id = azurerm_linux_virtual_machine.test.id
  lun                = "1"
  caching            = "ReadOnly"
}
vm.tf#L116
resource "azurerm_virtual_machine_data_disk_attachment" "web_vms_disk1_attach" {
  managed_disk_id    = azurerm_managed_disk.web_vms_disk1.id
  virtual_machine_id = element(module.web_vms.vm_ids, 0)
  lun                = "0"
  caching            = "None"
}

Review your Terraform file for Azure best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

Parameters

Explanation in Terraform Registry

Manages attaching a Disk to a Virtual Machine.

NOTE: Data Disks can be attached either directly on the azurerm_virtual_machine resource, or using the azurerm_virtual_machine_data_disk_attachment resource - but the two cannot be used together. If both are used against the same Virtual Machine, spurious changes will occur. -> Please Note: only Managed Disks are supported via this separate resource, Unmanaged Disks can be attached using the storage_data_disk block in the azurerm_virtual_machine resource.

Tips: Best Practices for The Other Azure Compute Resources

In addition to the azurerm_linux_virtual_machine, Azure Compute has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.

risk-label

azurerm_linux_virtual_machine

Ensure to use SSH authentication for virtual machines

It is better to use SSH authentication for virtual machines instead of password authentication to enforce more secure ways.

risk-label

azurerm_managed_disk

Ensure to enable the encryption on managed disks

It is better to enable the encryption on managed disks.

risk-label

azurerm_virtual_machine

Ensure to use SSH authentication for virtual machines

It is better to use SSH authentication for virtual machines instead of password authentication to enforce more secure ways.

Review your Azure Compute settings

In addition to the above, there are other security points you should be aware of making sure that your .tf files are protected in Shisho Cloud.

Azure Resource Manager Example

Azure Resource Manager code does not have the related resource.

Frequently asked questions

What is Azure Compute Virtual Machine Data Disk Attachment?

Azure Compute Virtual Machine Data Disk Attachment is a resource for Compute of Microsoft Azure. Settings can be wrote in Terraform.

Where can I find the example code for the Azure Compute Virtual Machine Data Disk Attachment?

For Terraform, the lasertown/throughput_test, lasertown/sles_upgrade_sp_swap and Lexar7/SAP_Deployment source code examples are useful. See the Terraform Example section for further details.