Cloud NAT rules (original) (raw)

This page provides an overview of Cloud NAT rules for Public NAT. These rules let you define how Cloud NAT is used to connect to the internet.

Cloud NAT rules for Public NAT support source network address translation (SNAT) based on source or destination address.

NAT rules

By default, when you configure a Cloud NAT gateway for Public NAT, packets that are translated by that NAT gateway use the same set of NAT IP addresses to reach all internet addresses. If you need more control over packets that are translated by Cloud NAT, you can add NAT rules.

A NAT rule defines a match condition and a corresponding action. After you specify NAT rules, each packet is matched with each NAT rule. If a packet matches the condition set in a rule, then the action corresponding to that match occurs.

NAT rules for Public NAT support both source and destination address matching:

Combining source- and destination-based conditions in a single NAT rule isn't allowed. For more information, see NAT rules specifications.

Cloud NAT rule configuration examples

This section provides configuration examples for source- and destination-based NAT rules.

Source-based rules

The following example shows how you can use source-based NAT rules.

By default, when a Cloud NAT gateway is configured for IPv4 traffic in a subnet, the gateway provides NAT for the primary internal IP address and alias IP ranges of any VM instance in that subnet. By using source-based NAT rules, you can also configure NAT for VM instances withIP forwarding enabled, as described in the following example.

In this example, Cloud NAT is configured in Subnet A. In the subnet, a VM instance with the primary internal IP address 10.1.1.2 and an alias IP range 10.2.1.0/24 sends traffic to the internet. Consider the following requirements for the VM instance:

Cloud NAT configuration with source-based rules.

Cloud NAT configuration with two source-based rules (click to enlarge).

To fulfill these requirements, you create two source-based NAT rules for forwarded packets and the default rule for packets from the primary internal IP address and the alias IP range of the VM instance:

If the source address of a packet forwarded by the VM instance doesn't match either rule 1 or rule 2, the packet is dropped.

For another example of how you can use source-based NAT rules, seeCloud WAN under the hood: A closer look at its differentiated networking capabilities.

Destination-based rules

The following example shows how to use NAT rules when your destination allows access from only a few IP addresses. We recommend that the traffic to such destinations from your Google Cloud VMs in private subnets be SNAT-translated with only the permitted IP addresses. We recommend that you not use these IP addresses for other destinations.

Consider the following requirements for VMs in Subnet-1 (10.10.10.0/24), which is in Region A of the VPC network test:

This VPC network also contains two additional subnets in the same region. These VMs must use NAT IP address 203.0.113.10 to send traffic to any destination.

Cloud NAT configuration with two Cloud NAT gateways.

Cloud NAT configuration with two Cloud NAT gateways (click to enlarge).

You can use NAT rules for this example, but you need two NAT gateways becauseSubnet-1 (10.10.10.0/24) has NAT rules that are different from the other subnets. To create this configuration, follow these steps:

  1. Create a gateway called Cloud NAT Gateway 1 for Subnet-1 with NAT IP address 203.0.113.40 and add the following rules:
    1. NAT rule 1 in Cloud NAT Gateway 1: when the destination is198.51.100.20/30, use 203.0.113.20 for NAT.
    2. NAT rule 2 in Cloud NAT Gateway 1: when the destination is198.51.100.30 or 198.51.100.31, use 203.0.113.30 for NAT.
  2. Create a gateway called Cloud NAT Gateway 2 for the region's other subnets and assign the NAT IP address as 203.0.113.10. No NAT rules are needed in this step.

NAT rules specifications

Rule expression language

NAT rules are written usingCommon Expression Languagesyntax.

An expression requires two components:

For example, the following expression uses the attributes destination.ip and198.51.100.0/24 in the operation inIpRange(). In this case, the expression returns true if destination.ip is within the 198.51.100.0/24 IP address range.

inIpRange(destination.ip, '198.51.100.0/24')

NAT rules support only the following attributes and operations:

Attributes

Attributes represent information from an outgoing packet, such as the source and destination IP address.

Attribute name Description
source.ip Source IP address of the packet
destination.ip Destination IP address of the packet

Operations

The following reference describes the operators that you can use with attributes to define rule expressions.

Operation Description
inIpRange(string, string) -> bool inIpRange(x, y) returns true if IP CIDR range y contains IP address_x_.
|
== Equals operator. x == y returnstrue if x is equal to_y_.

Example expressions

You can match packets based on either source or destination address, but not both.

Examples for source-based matching

Match packets with source IP address 10.0.0.25:

"source.ip == '10.0.0.25'"

Match packets with source IP address 10.0.0.25 or 10.0.0.26:

"source.ip == '10.0.0.25' || source.ip == '10.0.0.26'"

Match packets with source IP address range 10.0.2.0/24:

"inIpRange(source.ip, '10.0.2.0/24')"

Match packets with source IP address 10.0.0.25 or source IP address range10.0.2.0/24:

"source.ip == '10.0.0.25' || inIpRange(source.ip, '10.0.2.0/24')"

Examples for destination-based matching

Match packets with destination IP address 198.51.100.20:

"destination.ip == '198.51.100.20'"

Match packets with destination IP address 198.51.100.20 or198.51.100.21:

"destination.ip == '198.51.100.20' || destination.ip == '198.51.100.21'"

Match packets with destination IP address range 198.51.100.10/30:

"inIpRange(destination.ip, '198.51.100.10/30')"

Match packets with destination IP address 198.51.100.20 or destination IP address range 198.51.100.10/30:

"destination.ip == '198.51.100.20' || inIpRange(destination.ip, '198.51.100.10/30')"

What's next