Azure Networking Cookbook

4.5 (2 reviews total)
By Mustafa Toroman
  • Instant online access to over 8,000+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Azure Virtual Network

About this book

Microsoft provides organizations with an effective way of managing their network with Azure's networking services. No matter the size of your organization, Azure provides a way to highly reliable performance and secure connectivity with its networking services.

The book starts with an introduction to the Azure networking like creating Azure virtual networks, designing address spaces and subnets. Then you will learn to create and manage network security groups, application security groups, and IP addresses in Azure. Gradually, we move on to various aspects like S2S, P2S, and Vnet2Vnet connections, DNS and routing, load balancers and traffic manager. This book will cover every aspect and function required to deliver practical recipes to help readers learn from basic cloud networking practices to planning, implementing, and securing their infrastructure network with Azure. Readers will not only be able to upscale their current environment but will also learn to monitor, diagnose, and ensure secure connectivity. After learning to deliver a robust environment readers will also gain meaningful insights from recipes on best practices.

By the end of this book, readers will gain hands-on experience in providing cost-effective solutions that benefit organizations.

Publication date:
March 2019


Chapter 1. Azure Virtual Network

In this very first chapter, you will learn about the basics of Azure networking, including creating Azure Virtual Networks, designing address spaces, and subnets. This will lay the foundation for all future recipes that we'll cover in this book.

We will cover the following recipes in this chapter:

  • Creating a virtual network in the portal
  • Creating a virtual network with PowerShell
  • Adding a subnet in the portal
  • Adding a subnet with PowerShell
  • Changing the address space size
  • Changing the subnet size

Technical requirements

For this chapter, the following is required:

  • An Azure subscription
  • Azure PowerShell

Code samples can be found in


Creating a virtual network in the portal

Azure Virtual Network represents your local network in the cloud. It enables other Azure resources to communicate over a secure private network without exposing endpoints over the internet.

Getting ready

Before you start, open a web browser and go to the Azure portal at 

How to do it...

In order to create a new virtual network using the Azure portal, use the following steps:

  1. In the Azure portal, select Create a resource and choose Virtual network under Networking services (or, search for virtual network in the search bar).
  2. A new blade will open where we need to provide information for the virtual network to include Name, define Address space, select the Subscription option we want to use, select the Resource group option for where the virtual network will be deployed, select Location (Azure data center) for where the virtual network will be deployed, and define Name and Address range for the first subnet. We also have the option to select what kind of DDoS protection we want to use and if we want to use the Firewall option; an example is shown in the following screenshot:
  1. Creating a virtual network usually doesn't take much time and should be completed in under two minutes. Once deployment is finished, you can start using the virtual network.

How it works...

We deploy virtual networks to Resource group under Subscription in the Azure data center that we choose. Location and Subscription are important parameters; we will only be able to attach Azure resources to this virtual network if they are in the same subscription and region (as the Azure data center). The Address space option defines the number of IP addresses that will be available for our network. It uses the Classless Inter-Domain Routing (CIDR) format and the largest range we can choose is /8. In the portal, we need to create an initial subnet and define the subnet address range. The smallest subnet allowed is /29 and the largest is /8 (however, this can't be larger than the virtual network range).


Creating a virtual network with PowerShell

PowerShell is a command-line shell and scripting language based on the .NET Framework. It's often used by system administrators to automate tasks and manage operating systems. Azure PowerShell is a PowerShell module that allows us to automate and manage Azure resources. Azure PowerShell is also very often used to automate deployment tasks and can also be used to deploy a new Azure Virtual Network.

Getting ready

Before we start, we need to connect to the Azure subscription from a PowerShellconsole. Here's the command to do this:


This will open a new window where we need to input the credentials for the Azure subscription.

Afterward, we need to create a resource group where our virtual network will be deployed:

New-AzureRmResourceGroup -name 'Packt-Networking-Script' -Location 'westeurope'

The output should be similar to the following screenshot:

How to do it...

Deploying Azure Virtual Network is done in a single script. We need to define parameters for the resource group, location, name, and address range. Here is an example script:

New-AzureRmVirtualNetwork -ResourceGroupName 'Packt-Networking-Script' -Location 'westeurope' -Name 'Packt-Script' -AddressPrefix

You should receive the following output:

How it works...

The difference between deploying of a virtual networkfrom the portal and using PowerShell is that no subnet needs to be defined in PowerShell. The subnet is deployed in a separate command that can be executed either when you are deploying a virtual network or later on. We are going to see this command in the Adding subnets with PowerShell recipe.


Adding a subnet in the portal

Beside adding subnets while creating a virtual network, we can add additional subnets to our network at any time.

Getting ready

Before you start, open a web browser and go to the Azure portal at Here, locate the previously created virtual network.

How to do it...

In order to add a subnet to a virtual network using the Azure portal, we must use the following steps:

  1. In the virtual network blade, go to the Subnets section. 
  2. Select the Add subnet option.
  1. A new blade will open. We need to provide information for the subnet, including Name and Address range in the CIDR format. Address range must be in the range limit of the virtual network address range and cannot overlap with the address range of other subnets in the virtual network. Optionally, we can add information for Network security group, Route tables, Service endpoints, and Subnet delegation. These options will be covered in later recipes: 
  2. We can also add a gateway subnet in the same blade. To add a gateway subnet, select the Gateway subnet option.

For a gateway subnet, the only parameter we need to define is Address range. The same rules apply as for adding a regular subnet. This time, we don't have to provide a name as it's already defined. You can add only one gateway subnet per virtual network. Service endpoints are not allowed in the gateway subnet:

  1. After the subnets are added, we can see the newly created subnets in the subnet blade under the virtual network:

How it works...

A single virtual network can have a multiple number of subnets defined. Subnets can't overlap and must be in the range of the virtual network address range. For each subnet, four IP addresses are used for management and can't be used. Depending on the network settings, we can define the communication rules between subnets in the virtual network. A gateway subnet is used for VPN connections, and this will be covered in later chapters.


Adding a subnet with PowerShell

When creating Azure Virtual Network with PowerShell, a subnet is not created in the same step and requires an additional command to be executed separately.

Getting ready

Before creating a subnet, we need to collect information about the virtual network that the new subnet will be associated with. The parameters that need to be provided are the name of the virtual network and the resource group that the virtual network is located in:

$VirtualNetwork = Get-AzureRmVirtualNetwork -Name 'Packt-Script' -ResourceGroupName 'Packt-Networking-Script'

How to do it...

  1. To add a subnet to the virtual network, we need to execute a command and provide the name and address prefix. The address prefix is again in CIDR format:
Add-AzureRmVirtualNetworkSubnetConfig -Name FrontEnd -AddressPrefix -VirtualNetwork $VirtualNetwork
  1. We need to confirm these changes by executing the following:
$VirtualNetwork | Set-AzureRmVirtualNetwork
  1. We can add an additional subnet by running all commands in a single step, as follows:
$VirtualNetwork = Get-AzureRmVirtualNetwork -Name 'Packt-Script' -ResourceGroupName 'Packt-Networking-Script'
Add-AzureRmVirtualNetworkSubnetConfig -Name BackEnd -AddressPrefix -VirtualNetwork $VirtualNetwork
$VirtualNetwork | Set-AzureRmVirtualNetwork

How it works...

The subnet is created and added to the virtual network, but we need to confirm the changes before they can become effective. All the rules when creating or adding subnet size using the Azure portal apply here as well; the subnet must be within the virtual network's address space and cannot overlap with other subnets in the virtual network. The smallest subnet allowed is /29, and the largest is /8.

There's more...

We can create and add multiple subnets in a single script, as follows:

$VirtualNetwork = Get-AzureRmVirtualNetwork -Name 'Packt-Script' -ResourceGroupName 'Packt-Networking-Script'
$FrontEnd = Add-AzureRmVirtualNetworkSubnetConfig -Name FrontEnd -AddressPrefix -VirtualNetwork $VirtualNetwork
$BackEnd = Add-AzureRmVirtualNetworkSubnetConfig -Name BackEnd -AddressPrefix -VirtualNetwork $VirtualNetwork
$VirtualNetwork | Set-AzureRmVirtualNetwork

Changing the address space size

After the initial address space is defined during the creation of a virtual network, we can still change the address space size as needed. We can either increase or decrease the size of the address space, or change the address space completely by using a new address range.

Getting ready

Before you start, open a web browser and go to the Azure portal at 

How to do it...

In order to change the address space size for a virtual network using the Azure portal, we must observe the following steps:

  1. In a virtual network blade, locate Address space under Settings.
  2. In the available address space, click on Address space and change the value. An example is shown in the following screenshot:

  1. After you have entered a new value for Address space, click Save to apply the changes.

How it works...

Although you can change the address space at any time, there are some rules that determine what you can or cannot do. Address space can't be decreased if you have subnets defined in the address space that wouldn't be covered by a new address space. For example, if the address space was in the range of, it would cover addresses from to If one of the subnets was defined as, we wouldn't be able to change the virtual network to, as this will leave the subnet outside the new space.

Address space can't be changed to the new address space if you have subnets defined. In order to completely change the address space, you need to remove all subnets first. For example, if we have the address space defined as, we wouldn't be able to change it to, since having any subnets in the old space would leave them in an undefined address range.


Changing the subnet size

Similar to the virtual network address space, we can change the size of a subnet at any time.

Getting ready

Before you start, open a web browser and go to the Azure portal at 

How to do it...

In order to change subnet size using the Azure portal, we must use the following steps:

  1. In a virtual network blade, select the Subnets option.
  2. Select the subnet you want to change.
  3. In the Subnets option, enter a new value for the subnet size under Address range. An example of how to do this is shown in the following screenshot:
  1. After entering a new value, click on Save.
  2. In the Subnets list, you can see the changes applied and the address space has changed, as shown in the following screenshot:

How it works...

When changing subnet size, there are some rules that must be followed. You can't change the address space if it's not within the virtual network address space range and the subnet range can't overlap with other subnets in a virtual network. If devices are assigned to this subnet, you can't change the subnet to exclude the addresses that these devices are already assigned to.

About the Author

  • Mustafa Toroman

    Mustafa Toroman is a program architect and lead system engineer with Authority Partners. With years of experience of designing and monitoring infrastructure solutions, lately he focuses on designing new solutions in the cloud and migrating existing solutions to the cloud. He is very interested in DevOps processes, and he's also an Infrastructure-as-Code enthusiast. Mustafa has over 50 Microsoft certificates and has been an MCT for the last 8 years. He often speaks at international conferences about cloud technologies, and he has been awarded MVP for Microsoft Azure since 2016. Mustafa also authored ‘Hands-On Cloud Administration in Azure’ and ‘Azure Networking Cookbook’, and co-authored ‘Learn Node.js with Azure’, all published by Packt.

    Browse publications by this author

Latest Reviews

(2 reviews total)
very easy to understand for beginners. the basics of Azure virtual networks.
PRices ok. Found most of what I was looking for

Recommended For You

Book Title
Unlock this full book with a FREE 10-day trial
Start Free Trial