1. Answers
  2. Building an Azure Compute Virtual Machine

How do I build an Azure compute virtual machine?

In this guide, we will create an Azure Virtual Machine using Pulumi. We will define all necessary resources, such as the resource group, virtual network, subnet, network interface, and the virtual machine itself.

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

// Create an Azure Resource Group
const resourceGroup = new azure.resources.ResourceGroup("resourceGroup", {
    location: "WestUS",
});

// Create a Virtual Network
const virtualNetwork = new azure.network.VirtualNetwork("virtualNetwork", {
    resourceGroupName: resourceGroup.name,
    location: resourceGroup.location,
    addressSpace: { addressPrefixes: ["10.0.0.0/16"] },
});

// Create a Subnet
const subnet = new azure.network.Subnet("subnet", {
    resourceGroupName: resourceGroup.name,
    virtualNetworkName: virtualNetwork.name,
    addressPrefix: "10.0.1.0/24",
});

// Create a Network Interface
const networkInterface = new azure.network.NetworkInterface("networkInterface", {
    resourceGroupName: resourceGroup.name,
    location: resourceGroup.location,
    ipConfigurations: [{
        name: "ipconfig1",
        subnet: { id: subnet.id },
        privateIPAllocationMethod: "Dynamic",
    }],
});

// Create a Virtual Machine
const virtualMachine = new azure.compute.VirtualMachine("virtualMachine", {
    resourceGroupName: resourceGroup.name,
    location: resourceGroup.location,
    networkProfile: {
        networkInterfaces: [{ id: networkInterface.id }],
    },
    hardwareProfile: {
        vmSize: "Standard_DS1_v2",
    },
    osProfile: {
        computerName: "hostname",
        adminUsername: "adminuser",
        adminPassword: "Password1234!",
    },
    storageProfile: {
        osDisk: {
            createOption: "FromImage",
            name: "myosdisk1",
        },
        imageReference: {
            publisher: "Canonical",
            offer: "UbuntuServer",
            sku: "18.04-LTS",
            version: "latest",
        },
    },
});

export const publicIpAddress = virtualMachine.id;

Key Points

  • We created a resource group to contain all resources.
  • A virtual network and subnet were created to provide networking for the VM.
  • A network interface was created and associated with the subnet.
  • Finally, a virtual machine was created with the specified hardware and OS profile.

Summary

We successfully created an Azure Virtual Machine using Pulumi. This included setting up the necessary networking components and configuring the VM with an Ubuntu image.

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