Azure Load Balancer NAT Rule

This page shows how to write Terraform and Azure Resource Manager for Load Balancer NAT Rule and write them securely.

azurerm_lb_nat_rule (Terraform)

The NAT Rule in Load Balancer can be configured in Terraform with the resource name azurerm_lb_nat_rule. The following sections describe 10 examples of how to use the resource and its parameters.

Example Usage from GitHub

azure_lb.tf#L61
resource "azurerm_lb_nat_rule" "SSH1" {
  resource_group_name            = var.resource_group_name
  loadbalancer_id                = azurerm_lb.lb_f5.id
  name                           = "SSH1"
  protocol                       = "Tcp"
  frontend_port                  = 22
loadbalancers.tf#L81
resource "azurerm_lb_nat_rule" "LB1natrule11356" {
  resource_group_name            = azurerm_resource_group.TESTlab.name
  loadbalancer_id                = azurerm_lb.LB1.id
  name                           = "RDPnatrule1VM1356"
  protocol                       = "Tcp"
  frontend_port                  = 3389
main.tf#L95
resource "azurerm_lb_nat_rule" "ssh-nat-rule" {
    resource_group_name            = azurerm_resource_group.my-group.name
    loadbalancer_id                = azurerm_lb.my-lb.id
    name                           = "ssh-nat-rule"
    protocol                       = "Tcp"
    frontend_port                  = 22
main.tf#L13
resource "azurerm_lb_nat_rule" "lb_nat_rule" {
  backend_port                   = 80
  frontend_ip_configuration_name = var.pip_name
  frontend_port                  = 80
  loadbalancer_id                = azurerm_lb.lb.id
  name                           = "inbound"
11.natrule2.tf#L1
resource "azurerm_lb_nat_rule" "natrule2" {
  resource_group_name            = azurerm_resource_group.david-rg.name
  loadbalancer_id                = azurerm_lb.david-lb.id
  name                           = "web2SSH"
  protocol                       = "Tcp"
  frontend_port                  = 50002
10.natrule1.tf#L1
resource "azurerm_lb_nat_rule" "natrule1" {
  resource_group_name            = azurerm_resource_group.david-rg.name
  loadbalancer_id                = azurerm_lb.david-lb.id
  name                           = "web1SSH"
  protocol                       = "Tcp"
  frontend_port                  = 50001
natrule2.tf#L1
resource "azurerm_lb_nat_rule" "natrule2" {
  resource_group_name            = azurerm_resource_group.user12rg.name
  loadbalancer_id                = azurerm_lb.user12-lb.id
  name                           = "web2SSH"
  protocol                       = "Tcp"
  frontend_port                  = 50002
natrule1.tf#L1
resource "azurerm_lb_nat_rule" "natrule1" {
  resource_group_name            = azurerm_resource_group.user12rg.name
  loadbalancer_id                = azurerm_lb.user12-lb.id
  name                           = "web1SSH"
  protocol                       = "Tcp"
  frontend_port                  = 50001
12.natrule2.tf#L1
resource "azurerm_lb_nat_rule" "natrule2" {
  resource_group_name            = azurerm_resource_group.user24-rg1.name
  loadbalancer_id                = azurerm_lb.user24-lb.id
  name                           = "web2SSH"
  protocol                       = "Tcp"
  frontend_port                  = 50002
natrule2.tf#L1
resource "azurerm_lb_nat_rule" "natrule2" {
  resource_group_name            = azurerm_resource_group.user12rg.name
  loadbalancer_id                = azurerm_lb.user12-lb.id
  name                           = "web2SSH"
  protocol                       = "Tcp"
  frontend_port                  = 50002

Review your Terraform file for Azure best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

Parameters

Explanation in Terraform Registry

Manages a Load Balancer NAT Rule. -> NOTE: This resource cannot be used with with virtual machine scale sets, instead use the azurerm_lb_nat_pool resource.

NOTE When using this resource, the Load Balancer needs to have a FrontEnd IP Configuration Attached

Microsoft.Network/loadBalancers/inboundNatRules (Azure Resource Manager)

The loadBalancers/inboundNatRules in Microsoft.Network can be configured in Azure Resource Manager with the resource name Microsoft.Network/loadBalancers/inboundNatRules. The following sections describe how to use the resource and its parameters.

Example Usage from GitHub

An example could not be found in GitHub.

Parameters

  • name required - string
  • type required - string
  • apiVersion required - string
  • properties required
      • frontendIPConfiguration required
          • id required - string

            Resource ID.

      • protocol required - string

        The reference to the transport protocol used by the load balancing rule.

      • frontendPort required - integer

        The port for the external endpoint. Port numbers for each rule must be unique within the Load Balancer. Acceptable values range from 1 to 65534.

      • backendPort required - integer

        The port used for the internal endpoint. Acceptable values range from 1 to 65535.

      • idleTimeoutInMinutes optional - integer

        The timeout for the TCP idle connection. The value can be set between 4 and 30 minutes. The default value is 4 minutes. This element is only used when the protocol is set to TCP.

      • enableFloatingIP optional - boolean

        Configures a virtual machine's endpoint for the floating IP capability required to configure a SQL AlwaysOn Availability Group. This setting is required when using the SQL AlwaysOn Availability Groups in SQL server. This setting can't be changed after you create the endpoint.

      • enableTcpReset optional - boolean

        Receive bidirectional TCP Reset on TCP flow idle timeout or unexpected connection termination. This element is only used when the protocol is set to TCP.

Frequently asked questions

What is Azure Load Balancer NAT Rule?

Azure Load Balancer NAT Rule is a resource for Load Balancer of Microsoft Azure. Settings can be wrote in Terraform.

Where can I find the example code for the Azure Load Balancer NAT Rule?

For Terraform, the jungcheolkwon/blueprint, nzebar/Terraform and tkaburagi/tfe-on-azure source code examples are useful. See the Terraform Example section for further details.