Azure Virtual Network: Create a virtual network with multiple subnets

0

Azure portal

  1. In an Internet browser, go to the Azure portal. Log in using your Azure account. If you don’t have an Azure account, you can sign up for a free trial.
  2. In the portal, click +New > Networking > Virtual network.
  3. On the Create virtual network blade, enter the following values, and then click Create:
    Setting Value
    Name myVnet
    Address space 10.0.0.0/16
    Subnet name Public
    Subnet address range 10.0.0.0/24
    Resource group Leave Create new selected, and then enter myResourceGroup.
    Subscription and location Select your subscription and location.

    If you’re new to Azure, learn more about resource groups, subscriptions, and locations(also referred to as regions).

  4. In the portal, you can create only one subnet when you create a virtual network. In this tutorial, you create a second subnet after you create the virtual network. You might later create Internet-accessible resources in the Public subnet. You also might create resources that aren’t accessible from the Internet in the Private subnet. To create the second subnet, in the Search resources box at the top of the page, enter myVnet. In the search results, click myVnet. If you have multiple virtual networks with the same name in your subscription, check the resource groups that are listed under each virtual network. Ensure that you click the myVnet search result that has the resource group myResourceGroup.
  5. On the myVnet blade, under SETTINGS, click Subnets.
  6. On the myVnet – Subnets blade, click +Subnet.
  7. On the Add subnet blade, for Name, enter Private. For Address range, enter 10.0.1.0/24. Click OK.
  8. On the myVnet – Subnets blade, review the subnets. You can see the Public and Privatesubnets that you created.
  9. Optional: To delete the resources that you create in this tutorial, complete the steps in Delete resources in this article.

Azure CLI

Azure CLI commands are the same, whether you execute the commands from Windows, Linux, or macOS. However, there are scripting differences between operating system shells. The script in the following steps executes in a Bash shell.

  1. Install and configure the Azure CLI. Ensure you have the most recent version of the Azure CLI installed. To get help for CLI commands, type az <command> --help. Rather than installing the CLI and its pre-requisites, you can use the Azure Cloud Shell. The Azure Cloud Shell is a free Bash shell that you can run directly within the Azure portal. The Cloud Shell has the Azure CLI preinstalled and configured to use with your account. To use the Cloud Shell, click the Cloud Shell (>_) button at the top of the portal or just click the Try it button in the steps that follow.
  2. If running the CLI locally, log in to Azure with the az login command. If using the Cloud Shell, you’re already logged in.
  3. Review the following script and its comments. In your browser, copy the script and paste it into your CLI session:

    #!/bin/bash

    # Create a resource group.
    az group create \
    –name myResourceGroup \
    –location eastus

    # Create a virtual network with one subnet named Public.
    az network vnet create \
    –name myVnet \
    –resource-group myResourceGroup \
    –subnet-name Public

    # Create an additional subnet named Private in the virtual network.
    az network vnet subnet create \
    –name Private \
    –address-prefix 10.0.1.0/24 \
    –vnet-name myVnet \
    –resource-group myResourceGroup

  4. When the script is finished running, review the subnets for the virtual network. Copy the following command, and then paste it into your CLI session:
     az network vnet subnet list –resource-group myResourceGroup –vnet-name myVnet –output table
  5. Optional: To delete the resources that you create in this tutorial, complete the steps in Delete resources in this article.

PowerShell

  1. Install the latest version of the PowerShell AzureRm module. If you’re new to Azure PowerShell, see Azure PowerShell overview.
  2. In a PowerShell session, log in to Azure with your Azure account using the login-azurermaccount command.
  3. Review the following script and its comments. In your browser, copy the script and paste it into your PowerShell session:

    # Create a resource group.
    New-AzureRmResourceGroup `
    -Name myResourceGroup `
    -Location eastus

    # Create the public and private subnets.
    $Subnet1 = New-AzureRmVirtualNetworkSubnetConfig `
    -Name Public `
    -AddressPrefix 10.0.0.0/24
    $Subnet2 = New-AzureRmVirtualNetworkSubnetConfig `
    -Name Private `
    -AddressPrefix 10.0.1.0/24

    # Create a virtual network.
    $Vnet=New-AzureRmVirtualNetwork `
    -ResourceGroupName myResourceGroup `
    -Location eastus `
    -Name myVnet `
    -AddressPrefix 10.0.0.0/16 `
    -Subnet $Subnet1,$Subnet2

  4. To review the subnets for the virtual network, copy the following command, and then paste it into your PowerShell session:
    $Vnet.subnets | Format-Table Name, AddressPrefix
  5. Optional: To delete the resources that you create in this tutorial, complete the steps in Delete resources in this article.

You might also like More from author

Leave A Reply

Your email address will not be published.