1. Answers
  2. Building an Azure Compute Data Disk Attachment

How do I build an Azure compute datadiskattachment?

In this guide, we’ll walk through the steps to create an Azure virtual machine, a managed data disk, and how to attach this data disk to the virtual machine.

Steps involved:

  1. Provision a resource group.
  2. Provision a virtual network and a subnet.
  3. Create a network interface for the virtual machine.
  4. Create a virtual machine.
  5. Create a managed data disk.
  6. Attach the managed data disk to the virtual machine.

This example demonstrates the essential resources and their configurations.

import * as pulumi from "@pulumi/pulumi";
import * as azure from "@pulumi/azure";

// Create a resource group
const example = new azure.core.ResourceGroup("example", {
    name: "example-resources",
    location: "East US",
});
// Create a virtual network
const exampleVnet = new azure.network.VirtualNetwork("example_vnet", {
    name: "example-vnet",
    addressSpaces: ["10.0.0.0/16"],
    location: example.location,
    resourceGroupName: example.name,
});
// Create a subnet
const exampleSubnet = new azure.network.Subnet("example_subnet", {
    name: "example-subnet",
    resourceGroupName: example.name,
    virtualNetworkName: exampleVnet.name,
    addressPrefixes: ["10.0.1.0/24"],
});
// Create a network interface
const exampleNic = new azure.network.NetworkInterface("example_nic", {
    name: "example-nic",
    location: example.location,
    resourceGroupName: example.name,
    ipConfigurations: [{
        name: "internal",
        subnetId: exampleSubnet.id,
        privateIpAddressAllocation: "Dynamic",
    }],
});
// Create a virtual machine
const exampleVm = new azure.compute.LinuxVirtualMachine("example_vm", {
    name: "example-vm",
    location: example.location,
    resourceGroupName: example.name,
    networkInterfaceIds: [exampleNic.id],
    size: "Standard_DS1_v2",
    osDisk: {
        caching: "ReadWrite",
        storageAccountType: "Standard_LRS",
    },
    sourceImageReference: {
        publisher: "Canonical",
        offer: "UbuntuServer",
        sku: "18.04-LTS",
        version: "latest",
    },
    adminUsername: "adminuser",
    adminPassword: "Password1234!",
});
// Create a managed data disk
const exampleDisk = new azure.compute.ManagedDisk("example_disk", {
    name: "example-datadisk",
    location: example.location,
    resourceGroupName: example.name,
    storageAccountType: "Standard_LRS",
    createOption: "Empty",
    diskSizeGb: 10,
});
// Attach the managed data disk to the virtual machine
const exampleAttachment = new azure.compute.DataDiskAttachment("example_attachment", {
    managedDiskId: exampleDisk.id,
    virtualMachineId: exampleVm.id,
    lun: 0,
    caching: "ReadWrite",
});

Key Points:

  • Resource Group: Container for the resources.
  • Virtual Network and Subnet: Set up network topology.
  • Network Interface: Connects VM to the network.
  • Virtual Machine: Running instance that will use the disk.
  • Managed Disk: Persistent storage created and managed separately.
  • Data Disk Attachment: Configuration for attaching the disk to the VM.

Summary:

We’ve set up a complete infrastructure to create a virtual machine, a managed data disk, and attached this disk to the virtual machine. Each resource component was detailed to ensure a clear understanding of the process and the connections between them. This structure allows adding persistent storage to your VMs effectively.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up