Use Public NAT with Compute Engine
This page shows a demonstration of a Public NAT gateway that provides network address translation services for a Compute Engine VM instance. Before you begin, read the Public NAT overview.
Prerequisites
You need to do the following before setting up Public NAT.
Get IAM permissions
The roles/compute.networkAdmin role gives you permissions to create a NAT gateway on Cloud Router, reserve and assign NAT IP addresses, and specify subnetworks (subnets) whose traffic 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.
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.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
In the Google Cloud console, on the project selector page, select or create a Google Cloud 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.
-
To initialize the gcloud CLI, run the following command:
gcloud init
Example
The following is an end-to-end example that demonstrates a sample Public NAT gateway and a sample Compute Engine VM that uses the Public NAT gateway.
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 the VPC networks page.
Click Create VPC network.
Enter a Name of
custom-network1.Under Subnets, set Subnet creation mode to Custom.
Under New subnet, enter a Name of
subnet-us-east-192.In Region, select us-east4.
Enter an IP address range of
192.168.1.0/24.Click Done, and then click Create.
gcloud
Create a new custom mode VPC network in your project:
gcloud compute networks create custom-network1 \ --subnet-mode customSpecify the subnet prefix for your first region. In this example, we assign
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 a Terraform module to create a custom Virtual Private Cloud (VPC) network and subnet.
Step 2: Create a VM instance with no external IP address
Console
In the Google Cloud console, go to the VM instances page.
Click Create instance.
Specify a Name of
nat-test-1for your instance.Set the Region to us-east4.
Set the Zone to us-east4-c.
Click the Management, security, disks, networking, sole tenancy link.
Click the Networking tab.
Under Network interfaces, click Edit for the VM's default interface.
- Set the Network to
custom-network1. - Set the Subnetwork to
subnet-us-east-192. - Set External IP to None.
- Click Done.
- Set the Network to
To create and start the instance, click Create.