Use Public NAT with Compute Engine
This page shows a demonstration of a Public NAT gateway that providesnetwork address translation services for a Compute Engine VM instance. Beforeyou begin, read thePublic NAT overview.
Prerequisites
You need to do the following before setting up Public NAT.
Get IAM permissions
Theroles/compute.networkAdminrole gives you permissions to create a NAT gateway on Cloud Router,reserve and assign NAT IP addresses, and specify subnetworks (subnets) whosetraffic should use network address translation by the NAT gateway.
Set up Google Cloud
Before you get started, set up the following items in Google Cloud.
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- Create a project: To create a project, you need the Project Creator role (
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission.Learn how to grant roles.
Verify that billing is enabled for your Google Cloud project.
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Toinitialize the gcloud CLI, run the following command:
gcloudinit
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- Create a project: To create a project, you need the Project Creator role (
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission.Learn how to grant roles.
Verify that billing is enabled for your Google Cloud project.
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Toinitialize the gcloud CLI, run the following command:
gcloudinit
Note:
The Google Cloud CLI instructions on this page assume that you have set your project ID before issuing commands. You can set a project ID with the following command:
gcloud config set projectPROJECT_ID
You can also view a project ID that is already set:
gcloud config list --format='text(core.project)'
Example
The following is an end-to-end example that demonstrates a sample Public NATgateway and a sample Compute Engine VM that uses the Public NATgateway.
Step 1: Create a VPC network and subnet
If you already have a network and subnet, you can skip this step.
Console
In the Google Cloud console, go to theVPC networks page.
ClickCreate VPC network.
Enter aName of
custom-network1.UnderSubnets, setSubnet creation mode toCustom.
UnderNew subnet, enter aName of
subnet-us-east-192.InRegion, selectus-east4.
Enter anIP address range of
192.168.1.0/24.ClickDone, and then clickCreate.
gcloud
Create a new custom mode VPC network in your project:
gcloud compute networks create custom-network1 \ --subnet-mode custom
Specify the subnet prefix for your first region. In this example, weassign
192.168.1.0/24to regionus-east4.gcloud compute networks subnets create subnet-us-east-192 \ --network custom-network1 \ --region us-east4 \ --range 192.168.1.0/24
Terraform
You can use aTerraform module to create a custom Virtual Private Cloud (VPC) networkand subnet.
module "test-vpc-module" { source = "terraform-google-modules/network/google" version = "~> 13.0" project_id = var.project_id # Replace this with your project ID in quotes network_name = "custom-network1" mtu = 1460 subnets = [ { subnet_name = "subnet-us-east-192" subnet_ip = "192.168.1.0/24" subnet_region = "us-east4" } ]}Step 2: Create a VM instance with no external IP address
Console
In the Google Cloud console, go to theVM instances page.
ClickCreate instance.
Specify aName of
nat-test-1for your instance.Set theRegion tous-east4.
Set theZone tous-east4-c.
Click theManagement, security, disks, networking, sole tenancy link.
Click theNetworking tab.
UnderNetwork interfaces, clickEditfor the VM's default interface.
- Set theNetwork to
custom-network1. - Set theSubnetwork to
subnet-us-east-192. - SetExternal IP toNone.
- ClickDone.
- Set theNetwork to
To create and start the instance, clickCreate.
gcloud
gcloud compute instances create nat-test-1 \ --image-family debian-9 \ --image-project debian-cloud \ --network custom-network1 \ --subnet subnet-us-east-192 \ --zone us-east4-c \ --no-address
Terraform
You can use aTerraform resourceto create a VM instance.
resource "google_compute_instance" "default" { project = var.project_id zone = "us-east4-c" name = "nat-test-1" machine_type = "e2-medium" boot_disk { initialize_params { image = "debian-cloud/debian-9" } } network_interface { network = "custom-network1" subnetwork = var.subnet # Replace with a reference or self link to your subnet, in quotes }}Step 3: Create a firewall rule that allows SSH connections
Console
In the Google Cloud console, go to theFirewall policies page.
ClickCreate firewall rule.
Enter aName of
allow-ssh.Specify aNetwork of
custom-network1.SetDirection of traffic toIngress.
SetAction on match toAllow.
SetTargets toAll instances in the network.
SetSource filter toIPv4 ranges.
SetSource IP ranges to
35.235.240.0/20.SetProtocols and ports toSpecified protocols and ports.
Select thetcp checkbox and enter port
22.ClickCreate.
gcloud
gcloud compute firewall-rules create allow-ssh \ --network custom-network1 \ --source-ranges 35.235.240.0/20 \ --allow tcp:22
Terraform
You can use aTerraform resourceto create a firewall rule.
resource "google_compute_firewall" "rules" { project = var.project_id name = "allow-ssh" network = var.network # Replace with a reference or self link to your network, in quotes allow { protocol = "tcp" ports = ["22"] } source_ranges = ["35.235.240.0/20"]}Step 4: Create IAP SSH permissions for your test instance
In a later step, use Identity-Aware Proxy (IAP) to connect to your testinstance.
Console
In the Google Cloud console, go to theIdentity-Aware Proxy page.
Select theSSH and TCP resources tab.
To update member permissions on resources, select the checkbox next toAll Tunnel Resources > us-east4-c > nat-test-1.
In the right pane, clickAdd member.
To grant users, groups, or service accounts access to the resources,in theNew members field, specify their email addresses.
If you are just testing this feature, you can enter your own emailaddress.
To grant the members access to the resources through CloudIAP's TCP forwarding feature, in theRole drop-downlist, selectCloud IAP > IAP-secured Tunnel User.
ClickSave.
gcloud
This command grants SSH access by using IAP to all VMinstances in your project. If you want to grant SSH access by usingIAP to an individual VM, use the Google Cloud consoleinstructions.
gcloud projects add-iam-policy-bindingPROJECT_ID \ --member=MEMBER_INFO \ --role=roles/iap.tunnelResourceAccessor
Replace the following:
PROJECT_ID: your project IDMEMBER_INFO: a comma-separated list ofmembertype:emailpairs. Examples:- For an individual user:
user:test-user@example.com - For a group:
group:admins@example.com - For a service account:
serviceAccount:test123@example.domain.com
- For an individual user:
Terraform
You can use aTerraform resourceto create IAP SSH permissions for your test instance.
resource "google_project_iam_member" "project" { project = var.project_id role = "roles/iap.tunnelResourceAccessor" member = "serviceAccount:test123@example.domain.com"}Step 5: Log in tonat-test-1 and confirm that it cannot reach the internet
Console
In the Google Cloud console, go to theVM instances page.
For
nat-test-1, in theConnect column, click theSSH drop-downarrow, and then selectOpen in browser window.At the command prompt of the VM, enter
curl example.comand then pressEnter.You should get no result. If you do, you might have created
nat-test-1with an external IP address, or there might be some other problem.To troubleshoot, seeVMs can reach the internet unexpectedly without Cloud NAT.To end the command, you might have to enter
Ctrl+C.
gcloud
Add a Compute Engine SSH key to your local host:
ssh-add ~/.ssh/google_compute_engine
Connect to
nat-test-1and run a command:gcloud compute ssh nat-test-1 \ --zone us-east4-c \ --command "curl example.com" \ --tunnel-through-iap
You should get no result. If you do, you might have created
nat-test-1with an external IP address, or there might be some other problem.To troubleshoot, seeVMs can reach the internet unexpectedly without Cloud NAT.To end the command, you might have to enter
Ctrl+C.
Step 6: Create a NAT configuration using Cloud Router
You must create the Cloud Router in the same region as the instancesthat use Public NAT. Cloud Router is only used to place NATinformation onto the VMs. It is not used as part of the actual NAT gateway.
This configuration allows all instances in the region to use Public NATfor all primary andalias IP ranges. It also automaticallyallocates the external IP addresses for the NAT gateway. For more options, seetheGoogle Cloud CLI documentation.
Note: Public NAT uses Cloud Router only to group NATconfiguration information (control plane). Public NAT does notdirect a Cloud Router to use BGP or to add routes. NAT trafficdoesnot pass through a Cloud Router (data plane).Console
In the Google Cloud console, go to theCloud NAT page.
ClickGet started orCreate NAT gateway.
Enter aGateway name of
nat-config.Set theVPC network to
custom-network1.Set theRegion tous-east4.
UnderCloud Router, selectCreate new router.
- Enter aName of
nat-router. - ClickCreate.
- Enter aName of
ClickCreate.
gcloud
Create a Cloud Router:
gcloud compute routers create nat-router \ --network custom-network1 \ --region us-east4
Add a configuration to the router:
gcloud compute routers nats create nat-config \ --router-region us-east4 \ --router nat-router \ --nat-all-subnet-ip-ranges \ --auto-allocate-nat-external-ips
Terraform
You can use aTerraform resourceto create a Cloud Router.
resource "google_compute_router" "router" { project = var.project_id name = "nat-router" network = var.network region = "us-east4"}You can use aTerraform moduleto create a NAT configuration.
module "cloud-nat" { source = "terraform-google-modules/cloud-nat/google" version = "~> 5.0" project_id = var.project_id region = "us-east4" router = google_compute_router.router.name name = "nat-config" source_subnetwork_ip_ranges_to_nat = "ALL_SUBNETWORKS_ALL_IP_RANGES"}Step 7: Attempt to connect to the internet again
It might take up to three minutes for the NAT configuration to propagate to the VM,so wait at least a minute before trying to access the internet again.
Console
In the Google Cloud console, go to theVM instances page.
For
nat-test-1, in theConnect column, click theSSH drop-downarrow, and then selectOpen in browser window.At the command prompt of the VM, enter
curl example.comand then pressEnter.
gcloud
Connect tonat-test-1 and run a command:
gcloud compute ssh nat-test-1 \ --zone us-east4-c \ --command "curl example.com" \ --tunnel-through-iap
You should see output that contains the following content:
<html><head><title>Example Domain</title>.........</head><body><div> <h1>Example Domain</h1> <p>This domain is established to be used for illustrative examples in documents. You can use this domain in examples without prior coordination or asking for permission.</p> <p><a href="http://www.iana.org/domains/example">More information...</a></p></div></body></html>
What's next
- Set up aPublic NAT gateway.
- Create anexample Google Kubernetes Engine (GKE) setup.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-02-19 UTC.