Azure Primer - Setting up the Network Layer


Azure isn't a platform I play with regularly, I can't say it's offering is one of my favourite but it has come a long way and does deserve some representation.

In this tutorial, we are going to play with Terraform, it's a familiar staple of this blog and requires not much explanation. It also allows us to put off the subject of ARTs within Azure as well. This guide does not include the use of variables which we should do in profession circumstances.

Before we get started, make sure you have the following prerequisites:

  • An Azure account set up and you are logged in to the Azure portal.
  • Terraform installed on your local machine.

As always Configuration File!

The first step in setting up a network on Azure using Terraform is to create a configuration file. This file will contain the code that defines the resources you want to create on Azure.

To create a configuration file, open your favourite text editor and create a new file called main.tf.

At the top of the file, define the provider for Azure:

provider "azurerm" {
  version = "=2.34.0"
  subscription_id = "your-subscription-id"
  tenant_id = "your-tenant-id"
}

Replace "your-subscription-id" and "your-tenant-id" with the appropriate values for your Azure account.

The Virtual Network

The next step is to create a virtual network, or VNet, using Terraform. A VNet is a representation of your own network in the cloud. It enables you to securely connect Azure resources to each other, as well as to on-premises networks.

To create a VNet with Terraform, add the following code to your configuration file:

resource "azurerm_virtual_network" "example" {
  name                = "example-vnet"
  resource_group_name = "example-resource-group"
  location            = "westus2"
  address_space       = ["10.0.0.0/16"]
}

This code creates a VNet called "example-vnet" in the "westus2" region and assigns it an address space of 10.0.0.0/16.

Creating the Subnets!

Now that you have created a VNet, the next step is to create one or more subnets within it. A subnet is a range of IP addresses within a VNet that you can use to group Azure resources.

To create a subnet with Terraform, add the following code to your configuration file:

resource "azurerm_subnet" "example" {
  name                 = "example-subnet"
  resource_group_name  = "example-resource-group"
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefix       = "10.0.1.0/24"
}

This code creates a subnet called "example-subnet" within the "example-vnet" VNet and assigns it an address prefix of 10.0.1.0/24.


Configure your Network Security Groups

A network security group (NSG) is a layer of protection for your Azure network that allows you to control inbound and outbound traffic to and from your resources. You can create NSGs at the subnet level or at the individual resource level.

To create an NSG with Terraform, add the following code to your configuration file:

resource "azurerm_network_security_group" "example" {
  name                = "example-nsg"
  resource_group_name = "example-resource-group"
  location            = "westus2"
}

This code creates an NSG called "example-nsg" in the "example-resource-group" resource group in the "westus2" region.

Once your NSG is created, you can add rules to it to control the traffic to and from your resources. To do this, add the following code to your configuration file:

resource "azurerm_network_security_rule" "example" {
  name                        = "example-rule"
  resource_group_name         = "example-resource-group"
  network_security_group_name  = azurerm_network_security_group.example.name
  priority                    = 100
  direction                   = "Inbound"
  access                      = "Allow"
  protocol                    = "Tcp"
  source_port_range           = "*"
  destination_port_range      = "80"
  source_address_prefix       = "*"
  destination_address_prefix  = "*"
}

This code creates an inbound rule that allows TCP traffic on port 80 from any source to any destination.



Deploy ALL Resources

Now that you have defined all the resources you want to create on Azure, the final step is to deploy them. To do this, run the following command from the command line:

terraform apply

This will create all the resources defined in your configuration file on Azure.



Wrap Up!

In this tutorial, we showed you how to set up a network on the Azure Cloud Platform using Terraform. We walked through the steps of creating a VNet, subnets, and NSGs, and demonstrated how to configure these resources to meet your specific needs. We hope this tutorial was helpful and gives you a good starting point for building and managing networks on Azure using Terraform.