Well-architected framework (WAF) on Azure
Designed for resilient, efficient cloud workloads
Hello everyone, welcome to my first blog post! š If you ever wondered how your Azure resources could keep chatty, privately, not being ādisturbedā by the outside world, then follow me in this article. Today, we dive into the networking world of Azure Virtual Networks (VNet). This guide will take you through setting up your secure and efficient VNet using Terraform, irrespective of your experience in the DevOps world or if youāre just starting your cloud path.
Think of Azure Virtual Network as your own neighborhood in this big Azure city. Just like you have streets, houses, and maybe a coffee shop around the corner, VNet provides a secure and isolated environment where your Azure resources, like Virtual Machines, Databases, etc., are allowed to communicate with each other and the outside world in a safe and efficient manner.
Now, letās dive into the key components involved with a VNet. Consider this as building and adding bricks to your cloud city.
Well, the concept of subnetting is to divide your city into different parts, just like in your hometown. And each of those subnets can host various kinds of resources. For example, one subnet for web servers, another subnet for databases. And IP Addressing is how your resources find each otherį ³like address numbers.
It would be as if you were the security guy at a marriage, and the bride and groom inadvertently invited two groups of friends to the same table with the same name tag. Thatās basically what it feels like for your Azure VNET when you have overlapping subnets.
Overlapping of subnets, technically speaking, means that inside one single VNet, there exist two or more subnets with the IP address range being either duplicated or having commonalities. This duplication is leading to a result of deceiving the networking components of Azure as much that it perturbs in routing the traffic.
Here, the address of Subnet B is 10.0.1.0/24 and its range is from 10.0.1.128 to 10.0.1.255, overwriting the address of Subnet A, which is 10.0.1.128/25, and whose range is from 10.0.1.0 to 10.0.1.255.
Explaining it in another way, overlapping subnets are basically like having two cafes with the same name in your city. Customers-or in this case, your data packets-get confused about which location to go to!
Talk is cheap, letās go with a Terraform example:
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "rg" {
name = "myResourceGroup"
location = "eastus2"
}
resource "azurerm_virtual_network" "vnet" {
name = "myVNet"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
}
resource "azurerm_subnet" "subnet_web" {
name = "webSubnet"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = ["10.0.1.0/24"]
}
resource "azurerm_subnet" "subnet_db" {
name = "dbSubnet"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = ["10.0.2.0/24"]
}
See? Building your VNet is easier than assembling IKEA furniture š ļø
Think of having a party, which for this example, shall be your analogy to your VNet: suppose you wanted to control who gets in and out. The NSGs are like that security guard at the door who allows which traffic and blocks which, based on pre-defined rules. They enable one to protect oneās Azure resources by filtering inbound and outbound network traffic to and from resources in a VNet.
NSGs are an ordered list of security rules that allow or deny network traffic based on criteria such as, amongst others, Source and Destination IP Addresses, Ports and Protocols, and the flow of traffic - Inbound or Outbound. As such, when a packet of information tries to enter or exit a resource, the NSG checks the same against its set of rules and makes a decision to Allow or Deny the same.
This will, in turn, provide a seamless and secure manner of communicating between VNets, similar to building a private bridge between two islands. It is fast, cost-effective, and allows VNets to keep logically separated while communicating across regions or within the same Azure Region.
Peered VNets communicate using private IP addresses; therefore, no public IP is required. 6. Applying NSGs and route tables to control the flow of network traffic between peered VNets would provide granular level control over the flow of network traffic between the networks.
####
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "rg" {
name = "myResourceGroup"
location = "eastus2"
}
# VNet 1
resource "azurerm_virtual_network" "vnet1" {
name = "myVNet1"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
}
resource "azurerm_subnet" "subnet_web_vnet1" {
name = "webSubnet1"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet1.name
address_prefixes = ["10.0.1.0/24"]
}
resource "azurerm_subnet" "subnet_db_vnet1" {
name = "dbSubnet1"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet1.name
address_prefixes = ["10.0.2.0/24"]
}
# VNet 2
resource "azurerm_virtual_network" "vnet2" {
name = "myVNet2"
address_space = ["10.1.0.0/16"]
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
}
resource "azurerm_subnet" "subnet_web_vnet2" {
name = "webSubnet2"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet2.name
address_prefixes = ["10.1.1.0/24"]
}
resource "azurerm_subnet" "subnet_db_vnet2" {
name = "dbSubnet2"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet2.name
address_prefixes = ["10.1.2.0/24"]
}
# Network Security Group
resource "azurerm_network_security_group" "nsg" {
name = "myNSG"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
}
# NSG Rules
resource "azurerm_network_security_rule" "allow_inbound_http" {
name = "AllowInboundHTTP"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "80"
source_address_prefix = "*"
destination_address_prefix = "*"
resource_group_name = azurerm_resource_group.rg.name
network_security_group_name = azurerm_network_security_group.nsg.name
}
resource "azurerm_network_security_rule" "allow_outbound_https" {
name = "AllowOutboundHTTPS"
priority = 200
direction = "Outbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "443"
source_address_prefix = "*"
destination_address_prefix = "*"
resource_group_name = azurerm_resource_group.rg.name
network_security_group_name = azurerm_network_security_group.nsg.name
}
# Associate NSG with Subnet
resource "azurerm_subnet_network_security_group_association" "webSubnet1_nsg_association" {
subnet_id = azurerm_subnet.subnet_web_vnet1.id
network_security_group_id = azurerm_network_security_group.nsg.id
}
resource "azurerm_subnet_network_security_group_association" "dbSubnet1_nsg_association" {
subnet_id = azurerm_subnet.subnet_db_vnet1.id
network_security_group_id = azurerm_network_security_group.nsg.id
}
# VNet Peering between VNet1 and VNet2
resource "azurerm_virtual_network_peering" "vnet1_to_vnet2" {
name = "vnet1-to-vnet2"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet1.name
remote_virtual_network_id = azurerm_virtual_network.vnet2.id
allow_virtual_network_access = true
allow_forwarded_traffic = true
}
resource "azurerm_virtual_network_peering" "vnet2_to_vnet1" {
name = "vnet2-to-vnet1"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet2.name
remote_virtual_network_id = azurerm_virtual_network.vnet1.id
allow_virtual_network_access = true
allow_forwarded_traffic = true
}
This is a core competency, right at the heart of all security and efficient communication across your resources in the cloud. With Terraform, you make it easier to provision and set up VNets, subnets, and NSGs while having much less stress in keeping track and maintaining control of network traffic throughout your infrastructure.