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 https://github.com/PacktPublishing/Azure-Networking-Cookbook/tree/master/Chapter01.
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 https://portal.azure.com.
How to do it...
In order to create a new virtual network using the Azure portal, use the following steps:
- In the Azure portal, select Create a resource and choose Virtual network under Networking services (or, search for virtual network in the search bar).
- 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:

- 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 PowerShell console. Here's the command to do this:
Connect-AzureRmAccount
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 10.11.0.0/16
You should receive the following output:

How it works...
The difference between deploying of a virtual network from 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 https://portal.azure.com. 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:
- In the virtual network blade, go to the Subnets section.
- Select the Add subnet option.
- 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:
- 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:

- 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...
- 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 10.11.0.0/24 -VirtualNetwork $VirtualNetwork
- We need to confirm these changes by executing the following:
$VirtualNetwork | Set-AzureRmVirtualNetwork
- 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 10.11.1.0/24 -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 10.11.0.0/24 -VirtualNetwork $VirtualNetwork
$BackEnd = Add-AzureRmVirtualNetworkSubnetConfig -Name BackEnd -AddressPrefix 10.11.1.0/24 -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 https://portal.azure.com.
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:
- In a virtual network blade, locate Address space under Settings.
-
In the available address space, click on Address space and change the value. An example is shown in the following screenshot:

- 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 10.0.0.0/16, it would cover addresses from 10.0.0.1 to 10.0.255.254. If one of the subnets was defined as 10.0.255.0/24, we wouldn't be able to change the virtual network to 10.0.0.0/17, 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 10.0.0.0/16, we wouldn't be able to change it to 10.1.0.0/16, 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 https://portal.azure.com.
How to do it...
In order to change subnet size using the Azure portal, we must use the following steps:
- In a virtual network blade, select the Subnets option.
- Select the subnet you want to change.
- 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:

- After entering a new value, click on Save.
- 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.