Tutorial: Configure routing preference for a virtual machine
Article
This tutorial shows you how to configure routing preference for a virtual machine. Internet bound traffic from the virtual machine is routed via the ISP network when you choose Internet as your routing preference option. The default routing is via the Microsoft global network.
In this tutorial, you learn how to:
Create a virtual machine with a public IP address configured for Internet routing preference.
Verify the public IP address is set to Internet routing preference.
If you prefer to run CLI reference commands locally, install the Azure CLI. If you're running on Windows or macOS, consider running Azure CLI in a Docker container. For more information, see How to run the Azure CLI in a Docker container.
If you're using a local installation, sign in to the Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. For other sign-in options, see Sign in with the Azure CLI.
When you're prompted, install the Azure CLI extension on first use. For more information about extensions, see Use extensions with the Azure CLI.
Run az version to find the version and dependent libraries that are installed. To upgrade to the latest version, run az upgrade.
Azure PowerShell installed locally or Azure Cloud Shell
If you choose to install and use PowerShell locally, this article requires the Azure PowerShell module version 5.4.1 or later. Run Get-Module -ListAvailable Az to find the installed version. If you need to upgrade, see Install Azure PowerShell module. If you're running PowerShell locally, you also need to run Connect-AzAccount to create a connection with Azure.
In this section, you create a virtual machine and public IP address in the Azure portal. During the public IP address configuration, you select Internet for routing preference.
In the portal search box, enter Virtual machine. In the search results, select Virtual machines.
In Virtual machines, select + Create, then + Virtual machine.
In the Basics tab of Create a virtual machine, enter, or select the following information.
Setting
Value
Project details
Subscription
Select your subscription.
Resource group
Select Create new. Enter TutorVMRoutePref-rg. Select OK.
Instance details
Virtual machine name
Enter myVM.
Region
Select (US) West US 2.
Availability options
Select No infrastructure redundancy required.
Zone options
Select Self-selected zone.
Availability zone
Select Zone 1.
Security type
Select Standard.
Image
Select Windows Server 2022 Datacenter: Azure Edition - x64 Gen2.
Azure Spot instance
Leave the default of unchecked.
Size
Select a size.
Administrator account
Username
Enter a username.
Password
Enter a password.
Confirm password
Reenter password.
Inbound port rules
Public inbound ports
Select Allow selected ports.
Select inbound ports
Leave the default of RDP (3389). Opening port 3389 from the internet is not recommended for production workloads.
Select Next: Disks then Next: Networking, or select the Networking tab.
In the networking tab, enter or select the following information.
Setting
Value
Network interface
Virtual network
Leave the default of (new) TutorVMRoutePref-rg-vnet.
Subnet
Leave the default of (new) default (10.1.0.0/24).
Public IP
Select Create new. In Name, enter myPublicIP. In Routing preference, select Internet. In Availability zone, select Zone 1. Select OK.
Select Review + create.
Select Create.
In this section, you create a resource group, public IP address, and virtual machine using the Azure CLI. The public IP address created in the previous section is attached to the VM during creation.
Create a resource group
Create a resource group with az group create named TutorVMRoutePref-rg in the westus2 location.
az group create \
--name TutorVMRoutePref-rg \
--location westus2
Create a public IP address
Use az network public-ip create to create a standard zone-redundant public IPv4 address named myPublicIP in TutorVMRoutePref-rg. The Tag of Internet is applied to the public IP address as a parameter in the CLI command enabling the Internet routing preference.
Use az vm create to create a virtual machine. The public IP address created in the previous section is added as part of the CLI command and is attached to the VM during creation.
In this section, you create a resource group, public IP address, and virtual machine using Azure PowerShell. The public IP address created in the previous section is attached to the VM during creation.
Create a resource group
An Azure resource group is a logical container into which Azure resources are deployed and managed.
Create a resource group with New-AzResourceGroup named TutorVMRoutePref-rg in the westus2 location.
Use New-AzPublicIpAddress to create a standard zone-redundant public IPv4 address named myPublicIP in TutorVMRoutePref-rg. The Tag of Internet is applied to the public IP address as a parameter in the PowerShell command enabling the Internet routing preference.
## Create IP tag for Internet and Routing Preference. ##
$tag = @{
IpTagType = 'RoutingPreference'
Tag = 'Internet'
}
$ipTag = New-AzPublicIpTag @tag
## Create IP. ##
$ip = @{
Name = 'myPublicIP'
ResourceGroupName = 'TutorVMRoutePref-rg'
Location = 'westus2'
Sku = 'Standard'
AllocationMethod = 'Static'
IpAddressVersion = 'IPv4'
IpTag = $ipTag
Zone = 1,2,3
}
New-AzPublicIpAddress @ip
Create virtual machine
Use New-AzVM to create a virtual machine. The public IP address created in the previous section is added as part of the PowerShell command and is attached to the VM during creation.
In this section, you search for the public IP address previously created and verify the internet routing preference using the Azure portal.
In the portal search box, enter Public IP address. In the search results, select Public IP addresses.
In Public IP addresses, select myPublicIP.
Select Properties in Settings.
Verify Internet is displayed in Routing preference.
In this section, you use az network public-ip show to verify that Internet routing preference is configured for the public IP address with the Azure CLI.
az network public-ip show \
--resource-group TutorVMRoutePref-rg \
--name myPublicIP \
--query ipTags \
--output tsv
In this section, you use Get-AzPublicIpAddress to verify that Internet routing preference is configured for the public IP address with Azure PowerShell.
If you're not going to continue to use this application, delete the public IP address with the following steps:
In the search box at the top of the portal, enter Resource group.
In the search results, select Resource groups.
Select TutorVMRoutePref-rg
Select Delete resource group.
Enter myResourceGroup for TYPE THE RESOURCE GROUP NAME and select Delete.
When you're done with the virtual machine and public IP address, delete the resource group and all of the resources it contains with az group delete.
az group delete \
--name TutorVMRoutePref-rg
When you're done with the virtual machine and public IP address, delete the resource group and all of the resources it contains with Remove-AzResourceGroup.