Creating an Azure NAT Gateway with Terraform and a Public IP Address

Walisson Dias
3 min readMay 11, 2023

--

Are you looking to connect your Azure Virtual Machines to the internet using a single public IP address? Look no further than the Azure Nat gateway! In this tutorial, we’ll walk you through the steps to create an Azure NAT gateway using terraform template and a public IP address.

Before we dive in, let’s quickly go over what a NAT gateway is and why you might need one. NAT stands for Network Address Translation, and a NAT gateway allows virtual machines in an Azure virtual network to connect to the internet using single ip public ip address. This can be useful for a number of reasons, such as reducing the number of public IP address you need to manage or limiting inbound access to your virtual machines.

Now, let’s get started with creating your NAT gateway.

Step 1: Create a Public IP Address

The first step in creating an Azure NAT gateway is to create a public IP address to associate with the gateway. This public IP address is used to allow inbound traffic to reach the NAT gateway.

resource "azurerm_public_ip" "gateway-pip" {
name = "gateway-pip"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
allocation_method = "Static"
sku = "Standard"
zones = ["1"]
}

Step 2: Create the NAT Gateway

Now that you’ve created a public IP address, you can create the NAT gateway. To create NAT gateway using Terraforms, the configuration should include the following resources:

resource "azurerm_nat_gateway" "nat-gateway" {
name = "nat-gateway"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
sku_name = "Standard"
idle_timeout_in_minutes = 10
zones = ["1"]
}

Step 3: Associate the NAT Gateway with a Subnet

Now that you’ve created the NAT gateway, you need to associate it with a subnet in your virtual network. This will allow virtual machines in that subnet to connect to the internet using the NAT gateway.


resource "azurerm_nat_gateway_public_ip_association" "pip-nat-association" {
nat_gateway_id = azurerm_nat_gateway.nat-gateway.id
public_ip_address_id = azurerm_public_ip.gateway-pip.id
}

resource "azurerm_subnet_nat_gateway_association" "subnet-nat-association" {
subnet_id = azurerm_subnet.hub-gateway-subnet.id
nat_gateway_id = azurerm_nat_gateway.nat-gateway.id
}

You can check which Public IP address the virtual machine is using by executing the command line listed below and observed that the Outbound IP address of the NAT Gateway was returned as the result in both cases (20.51.127.3).

adminuser@myvm-vnet1:~$ dig +short myip.opendns.com @resolver1.opendns.com
20.51.127.3

And that’s it! You’ve successfully created an Azure NAT gateway using a terraform template and a public IP address. Your virtual machines in the associated subnet can now connect to the internet using the NAT gateway.

In conclusion, the Azure NAT gateway is a powerful tool that can simplify your network infrastructure and improve security. By following these simple steps, you can easily create a NAT gateway that meets your needs. We hope this tutorial was

--

--

Walisson Dias

Certified Cloud Architect with 10+ years of experience Expert in Azure and Office 365 solutions and migrations Passionate about cloud technologies🚀