1. Answers
  2. Creating an Azure Network Subnet with Pulumi

How do I create an Azure network subnet with Pulumi?

In this guide, we will create a subnet within an Azure Virtual Network using Pulumi. We will define the necessary resources such as the resource group, virtual network, and subnet. Pulumi allows us to manage and provision these resources using TypeScript.

Key Points:

  • Define a resource group to contain the network resources.
  • Create a virtual network within the resource group.
  • Define a subnet within the virtual network.
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", {
    resourceGroupName: "myResourceGroup",
    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 within the Virtual Network
const subnet = new azure.network.Subnet("subnet", {
    resourceGroupName: resourceGroup.name,
    virtualNetworkName: virtualNetwork.name,
    addressPrefix: "10.0.1.0/24"
});

// Export the subnet ID
export const subnetId = subnet.id;

Summary

In this guide, we created a resource group, a virtual network, and a subnet within that virtual network using Pulumi. The subnet is defined with an address prefix within the virtual network’s address space, and the resource group and virtual network are created in the “WestUS” region. The subnet ID is exported as an output for further use.

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