How to Create an Azure NAT Gateway (Portal, CLI & Terraform).
Network Address Translation (NAT) Gateway in Azure is a fully managed service that provides outbound connectivity for virtual networks. It simplifies outbound-only internet connectivity for virtual networks while ensuring high availability and scalability.
What is Azure NAT Gateway?
Azure NAT Gateway is a fully managed Network Address Translation (NAT) service that allows virtual machines and other resources within a virtual network to connect to the internet without requiring public IP addresses directly assigned to them. NAT Gateway manages outbound connections by translating the private IP addresses of resources in a virtual network to the public IP address(es) of the NAT Gateway itself.
Why Use Azure NAT Gateway?
There are several key benefits to using Azure NAT Gateway:
Simplified Outbound Connectivity: Manage outbound connections for multiple resources through a single point.
IP Address Conservation: Avoid assigning public IPs to each VM or resource that needs outbound internet access.
Enhanced Security: Reduce attack surface by hiding internal IPs behind the NAT Gateway.
Predictable Outbound IP: Ensures your services connect to external systems through consistent, predictable IP addresses.
High Availability: Built-in redundancy with no extra configuration required.
Scalability: Automatically scales to support up to 64,000 concurrent connections per NAT gateway IP address.
Source Network Address Translation (SNAT) Port Exhaustion Prevention: Helps avoid SNAT port exhaustion issues that can occur with load balancers.
How Does Azure NAT Gateway Work?
Azure NAT Gateway operates by:
Intercepting outbound traffic from virtual networks that are configured to use it.
Translating the private source IP addresses of your virtual machines to the public IP address(es) assigned to the NAT Gateway.
Managing SNAT port allocation to ensure efficient use of resources.
Tracking connections to properly route return traffic back to the originating virtual machines.
NAT Gateway can work with one or more public IP addresses or public IP prefixes. When using multiple IPs, Azure distributes the traffic across them to maximize the available SNAT ports.
Creating an Azure NAT Gateway
Let’s explore three different methods to create an Azure NAT Gateway:
Method 1: Using the Azure Portal
Sign in to the Azure Portal at https://portal.azure.com.
Search for “NAT gateways” in the top search bar and select it from the services list.
Click “Create NAT gateway” to start the creation process.
In the Basics tab:
- Select your subscription
- Choose or create a resource group
- Name your NAT gateway
- Select the region
- Set availability zone if needed
- Configure the idle timeout (default is 4 minutes)
In the Outbound IP tab:
- Choose to create new public IP addresses or use existing ones
- Alternatively, you can assign a public IP prefix
In the Subnet tab:
- Select the virtual network and specific subnets to associate with the NAT gateway
Review + create and then click Create after validation passes.
Wait for deployment to complete.
Method 2: Using Azure CLI
To create a NAT Gateway using Azure CLI, first ensure you have Azure CLI installed and are logged in with az login.
1# 1. Create a resource group (if not already available)
2az group create --name myResourceGroup --location eastus
3
4# 2. Create a virtual network and subnet
5az network vnet create \
6 --resource-group myResourceGroup \
7 --name myVNet \
8 --address-prefix 10.0.0.0/16 \
9 --subnet-name mySubnet \
10 --subnet-prefix 10.0.0.0/24
11
12# 3. Create a public IP address for the NAT gateway
13az network public-ip create \
14 --resource-group myResourceGroup \
15 --name myPublicIP \
16 --sku Standard \
17 --allocation-method Static
18
19# 4. Create the NAT gateway
20az network nat gateway create \
21 --resource-group myResourceGroup \
22 --name myNATgateway \
23 --public-ip-addresses myPublicIP \
24 --idle-timeout 10
25
26# 5. Update the subnet to use the NAT gateway
27az network vnet subnet update \
28 --resource-group myResourceGroup \
29 --vnet-name myVNet \
30 --name mySubnet \
31 --nat-gateway myNATgateway
Method 3: Using Terraform
To deploy an Azure NAT Gateway using Terraform, create a Terraform configuration file (e.g., main.tf) with the following content:
1# Configure the Azure provider
2provider "azurerm" {
3 features {}
4}
5
6# Create a resource group
7resource "azurerm_resource_group" "example" {
8 name = "nat-gateway-rg"
9 location = "East US"
10}
11
12# Create a virtual network
13resource "azurerm_virtual_network" "example" {
14 name = "example-vnet"
15 address_space = ["10.0.0.0/16"]
16 location = azurerm_resource_group.example.location
17 resource_group_name = azurerm_resource_group.example.name
18}
19
20# Create a subnet
21resource "azurerm_subnet" "example" {
22 name = "example-subnet"
23 resource_group_name = azurerm_resource_group.example.name
24 virtual_network_name = azurerm_virtual_network.example.name
25 address_prefixes = ["10.0.1.0/24"]
26}
27
28# Create a public IP for NAT Gateway
29resource "azurerm_public_ip" "example" {
30 name = "nat-gateway-publicip"
31 location = azurerm_resource_group.example.location
32 resource_group_name = azurerm_resource_group.example.name
33 allocation_method = "Static"
34 sku = "Standard"
35}
36
37# Create NAT Gateway
38resource "azurerm_nat_gateway" "example" {
39 name = "example-natgateway"
40 location = azurerm_resource_group.example.location
41 resource_group_name = azurerm_resource_group.example.name
42 idle_timeout_in_minutes = 10
43}
44
45# Associate public IP with NAT Gateway
46resource "azurerm_nat_gateway_public_ip_association" "example" {
47 nat_gateway_id = azurerm_nat_gateway.example.id
48 public_ip_address_id = azurerm_public_ip.example.id
49}
50
51# Associate subnet with NAT Gateway
52resource "azurerm_subnet_nat_gateway_association" "example" {
53 subnet_id = azurerm_subnet.example.id
54 nat_gateway_id = azurerm_nat_gateway.example.id
55}
To deploy this configuration:
- Save the code to a file named
main.tf - Initialize Terraform:
1terraform init - Preview the changes:
1terraform plan - Apply the configuration:
1terraform apply
Best Practices for NAT Gateway
Monitor SNAT port usage to avoid exhaustion issues.
Use IP prefixes when you need a large number of outbound IP addresses.
Set appropriate idle timeout values based on your application’s behavior.
Consider zone-redundancy for production workloads.
Plan capacity based on your expected concurrent connections.
Limitations to Consider
NAT Gateway only supports outbound traffic flows.
Only TCP and UDP protocols are supported.
There are regional limits on the number of NAT Gateways per subscription.
A subnet can be associated with only one NAT Gateway.
Conclusion
Azure NAT Gateway provides a scalable, reliable solution for managing outbound connectivity from your virtual networks. Whether you prefer using the Azure Portal, CLI, or infrastructure as code with Terraform, implementing a NAT Gateway helps simplify network management while improving security and resource utilization.
By centralizing outbound connectivity, you can better control and monitor how your Azure resources connect to the internet, making your network architecture more robust and manageable.