Over a million developers have joined DZone.

User-Defined Routing in the Cloud with Azure Resource Manager and Azure PowerShell 1.0 Preview

Here's a nice chunk of code that shows you how to configure IP forwarding and user-defined routes via the new Azure Resource Manager(ARM) API.

· Cloud Zone

Download the Essential Cloud Buyer’s Guide to learn important factors to consider before selecting a provider as well as buying criteria to help you make the best decision for your infrastructure needs, brought to you in partnership with Internap.

Custom IP routing topologies on Azure Virtual Networks have been available for several months via native User-Defined Routing (UDR) and IP Forwarding features.  However, recently I’ve been receiving questions on how to configure IP forwarding and user-defined routes via the new Azure Resource Manager(ARM) API. 

Azure UDR 2




















In this article, we’ll step through a set of PowerShell snippets that walk-through the entire end-to-end process of using the new Azure PowerShell 1.0 Preview module cmdlets for ARM to implement user-defined routing for the front-end subnet pictured above.

# Sign-in with Azure account credentials

Login-AzureRmAccount

# Select Azure Subscription

$subscriptionId = 
    (Get-AzureRmSubscription |
     Out-GridView `
        -Title "Select an Azure Subscription ..." `
        -PassThru).SubscriptionId

Select-AzureRmSubscription `
    -SubscriptionId $subscriptionId

# First time only - register ARM core resource providers

Register-AzureRmResourceProvider `
    -ProviderNamespace Microsoft.Compute

Register-AzureRmResourceProvider `
    -ProviderNamespace Microsoft.Storage

Register-AzureRmResourceProvider `
    -ProviderNamespace Microsoft.Network

Get-AzureRmResourceProvider | 
    Select-Object `
    -Property ProviderNamespace `
    -ExpandProperty ResourceTypes

# Select Resource Group in which existing VNET is provisioned

$rgName =
    (Get-AzureRmResourceGroup |
     Out-GridView `
        -Title "Select an Azure Resource Group ..." `
        -PassThru).ResourceGroupName

# Select Azure VNET and Location on which to enable UDR

$vnetName = 
    (Get-AzureRmVirtualNetwork `
        -ResourceGroupName $rgName).Name |
     Out-GridView `
        -Title "Select an Azure VNET ..." `
        -PassThru

$vnet = Get-AzureRmVirtualNetwork `
    -ResourceGroupName $rgName `
    -Name $vnetName

$location = $vnet.Location

# Select Azure Subnet on which to enable UDR

$subnetName = 
    $vnet.Subnets.Name |
    Out-GridView `
        -Title "Select an Azure Subnet ..." `
        -PassThru

$subnet = $vnet.Subnets | 
    Where-Object Name -eq $subnetName

# Create new UDR table

$routeTableName = "frontendroutetable"

$routeTable = New-AzureRmRouteTable `
    -Name $routeTableName `
    -ResourceGroupName $rgName `
    -Location $location

# Add a route to the UDR Table

$routeName = "frontendroute"

$routeTable | 
    Add-AzureRmRouteConfig `
        -Name $routeName `
        -AddressPrefix "192.168.2.0/24" `
        -NextHopType VirtualAppliance `
        -NextHopIpAddress "192.168.0.101" | 
    Set-AzureRmRouteTable

# Assign UDR table to selected subnet

Set-AzureRmVirtualNetworkSubnetConfig `
    -VirtualNetwork $vnet `
    -Name $subnetName `
    -AddressPrefix $subnet.AddressPrefix `
    -RouteTableId $routeTable.Id |
    Set-AzureRmVirtualNetwork

# Confirm UDR table is provisioned and assigned to subnet

Get-AzureRmRouteTable `
    -ResourceGroupName $rgName `
    -Name $routeTableName

# Configure “Appliance” VM for IP Forwarding on each NIC

$vmName = 
    (Get-AzureRmVM -ResourceGroupName $rgName).Name |
        Out-GridView `
            -Title "Select a VM to configure forwarding ..." `
            -PassThru

$nicName = 
    ((Get-AzureRmVM `
        -ResourceGroupName $rgName `
        -Name $vmName).NetworkInterfaceIDs).Split("/")[-1] |
            Out-GridView `
                -Title "Select a NIC to configure forwarding ..." `
                -PassThru

$nicConfig = 
    Get-AzureRmNetworkInterface `
        -ResourceGroupName $rgName `
        -Name $nicName

$nicConfig.EnableIPForwarding = $true

$nicConfig | Set-AzureRmNetworkInterface

See you in the Clouds!

- Keith

The Cloud Zone is brought to you in partnership with Internap. Read Bare-Metal Cloud 101 to learn about bare-metal cloud and how it has emerged as a way to complement virtualized services.

Topics:
azure ,powershell ,cloud

Published at DZone with permission of Keith Mayer, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}