Skip to content

azurermVirtualMachineDataDiskAttachment

Manages attaching a Disk to a Virtual Machine.

\~> NOTE: Data Disks can be attached either directly on the azurermVirtualMachine resource, or using the azurermVirtualMachineDataDiskAttachment 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 storageDataDisk block in the azurermVirtualMachine resource.

Example Usage

import * as cdktf from "cdktf";
/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as azurerm from "./.gen/providers/azurerm";
/*The following providers are missing schema information and might need manual adjustments to synthesize correctly: azurerm.
For a more precise conversion please use the --provider flag in convert.*/
/*Terraform Variables are not always the best fit for getting inputs in the context of Terraform CDK.
You can read more about this at https://cdk.tf/variables*/
const prefix = new cdktf.TerraformVariable(this, "prefix", {
  default: "example",
});
const vmName = `\${${prefix.value}}-vm`;
const azurermResourceGroupExample = new azurerm.resourceGroup.ResourceGroup(
  this,
  "example",
  {
    location: "West Europe",
    name: `\${${prefix.value}}-resources`,
  }
);
const azurermVirtualNetworkMain = new azurerm.virtualNetwork.VirtualNetwork(
  this,
  "main",
  {
    address_space: ["10.0.0.0/16"],
    location: azurermResourceGroupExample.location,
    name: `\${${prefix.value}}-network`,
    resource_group_name: azurermResourceGroupExample.name,
  }
);
const azurermManagedDiskExample = new azurerm.managedDisk.ManagedDisk(
  this,
  "example_3",
  {
    create_option: "Empty",
    disk_size_gb: 10,
    location: azurermResourceGroupExample.location,
    name: `\${${vmName}}-disk1`,
    resource_group_name: azurermResourceGroupExample.name,
    storage_account_type: "Standard_LRS",
  }
);
/*This allows the Terraform resource name to match the original name. You can remove the call if you don't need them to match.*/
azurermManagedDiskExample.overrideLogicalId("example");
const azurermSubnetInternal = new azurerm.subnet.Subnet(this, "internal", {
  address_prefixes: ["10.0.2.0/24"],
  name: "internal",
  resource_group_name: azurermResourceGroupExample.name,
  virtual_network_name: azurermVirtualNetworkMain.name,
});
const azurermNetworkInterfaceMain =
  new azurerm.networkInterface.NetworkInterface(this, "main_5", {
    ip_configuration: [
      {
        name: "internal",
        private_ip_address_allocation: "Dynamic",
        subnet_id: azurermSubnetInternal.id,
      },
    ],
    location: azurermResourceGroupExample.location,
    name: `\${${prefix.value}}-nic`,
    resource_group_name: azurermResourceGroupExample.name,
  });
/*This allows the Terraform resource name to match the original name. You can remove the call if you don't need them to match.*/
azurermNetworkInterfaceMain.overrideLogicalId("main");
const azurermVirtualMachineExample = new azurerm.virtualMachine.VirtualMachine(
  this,
  "example_6",
  {
    location: azurermResourceGroupExample.location,
    name: vmName,
    network_interface_ids: [azurermNetworkInterfaceMain.id],
    os_profile: [
      {
        admin_password: "Password1234!",
        admin_username: "testadmin",
        computer_name: vmName,
      },
    ],
    os_profile_linux_config: [
      {
        disable_password_authentication: false,
      },
    ],
    resource_group_name: azurermResourceGroupExample.name,
    storage_image_reference: [
      {
        offer: "UbuntuServer",
        publisher: "Canonical",
        sku: "16.04-LTS",
        version: "latest",
      },
    ],
    storage_os_disk: [
      {
        caching: "ReadWrite",
        create_option: "FromImage",
        managed_disk_type: "Standard_LRS",
        name: "myosdisk1",
      },
    ],
    vm_size: "Standard_F2",
  }
);
/*This allows the Terraform resource name to match the original name. You can remove the call if you don't need them to match.*/
azurermVirtualMachineExample.overrideLogicalId("example");
const azurermVirtualMachineDataDiskAttachmentExample =
  new azurerm.virtualMachineDataDiskAttachment.VirtualMachineDataDiskAttachment(
    this,
    "example_7",
    {
      caching: "ReadWrite",
      lun: "10",
      managed_disk_id: azurermManagedDiskExample.id,
      virtual_machine_id: azurermVirtualMachineExample.id,
    }
  );
/*This allows the Terraform resource name to match the original name. You can remove the call if you don't need them to match.*/
azurermVirtualMachineDataDiskAttachmentExample.overrideLogicalId("example");

Argument Reference

The following arguments are supported:

  • virtualMachineId - (Required) The ID of the Virtual Machine to which the Data Disk should be attached. Changing this forces a new resource to be created.

  • managedDiskId - (Required) The ID of an existing Managed Disk which should be attached. Changing this forces a new resource to be created.

  • lun - (Required) The Logical Unit Number of the Data Disk, which needs to be unique within the Virtual Machine. Changing this forces a new resource to be created.

  • caching - (Required) Specifies the caching requirements for this Data Disk. Possible values include none, readOnly and readWrite.

  • createOption - (Optional) The Create Option of the Data Disk, such as empty or attach. Defaults to attach. Changing this forces a new resource to be created.

  • writeAcceleratorEnabled - (Optional) Specifies if Write Accelerator is enabled on the disk. This can only be enabled on premiumLrs managed disks with no caching and M-Series VMs. Defaults to false.

Attributes Reference

The following attributes are exported:

  • id - The ID of the Virtual Machine Data Disk attachment.

Timeouts

The timeouts block allows you to specify timeouts for certain actions:

  • create - (Defaults to 30 minutes) Used when creating the Virtual Machine Data Disk Attachment.
  • update - (Defaults to 30 minutes) Used when updating the Virtual Machine Data Disk Attachment.
  • read - (Defaults to 5 minutes) Used when retrieving the Virtual Machine Data Disk Attachment.
  • delete - (Defaults to 30 minutes) Used when deleting the Virtual Machine Data Disk Attachment.

Import

Virtual Machines Data Disk Attachments can be imported using the resourceId, e.g.

terraform import azurerm_virtual_machine_data_disk_attachment.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/Microsoft.Compute/virtualMachines/machine1/dataDisks/disk1

-> Please Note: This is a Terraform Unique ID matching the format: {virtualMachineId}/dataDisks/{diskName}