Create port mapping services
This page describes how service producers can publish services that usePrivate Service Connect port mapping.
Private Service Connect port mapping lets consumer clientsprivately communicate with specific service ports on specific producer VMsthrough a singlePrivate Service Connectendpoint.
Before you begin
- You mustenabletheCompute Engine API in your project.
- You mustcreate VMsfor each of the network endpoints that you want to attach to the port mappingNEG. The VMs must all be in the same region.
- ReadAbout published services.
- ReadAbout Private Service Connect port mapping.
Required roles
To get the permissions that you need to prepare to publish a service with Private Service Connect port mapping, ask your administrator to grant you the following IAM roles on the producer project:
- Compute Load Balancer Admin (
roles/compute.loadBalancerAdmin) - Compute Network Admin (
roles/compute.networkAdmin)
For more information about granting roles, seeManage access to projects, folders, and organizations.
You might also be able to get the required permissions throughcustom roles or otherpredefined roles.
Create and publish a port mapping service
Port mapping services have similar configurations to internal passthrough Network Load Balancers, buttraffic is not load balanced. To create and publish a port mapping service, dothe following:
- Create a port mapping NEG
- Add network endpoints to the port mapping NEG
- Create a port mapping service
- Publish the port mapping service
Create a port mapping NEG
Aport mapping network NEGis a regional NEG with the network endpoint type ofGCE_VM_IP_PORTMAP. It'snot possible to change a port mapping NEG's type after it is created.
When you create a port mapping NEG, you choose a subnet. Network endpoints thatyou associate with the port mapping NEG must have a primary network interfacein this subnet.
Console
In the Google Cloud console, go to theNetwork Endpoint Groups page.
ClickCreate network endpoint group.
Enter aName.
ClickNetwork endpoint group type, and then selectPort mapping NEG (Regional).
Select aRegion.
Select aNetwork.
Select aSubnetwork.
ClickCreate.
gcloud
Use thenetwork-endpoints-groups create command.
gcloud compute network-endpoint-groups createNEG \ --region=REGION \ --network=NETWORK \ --subnet=SUBNET \ --network-endpoint-type=GCE_VM_IP_PORTMAP
Replace the following:
NEG: the name of the port mapping NEGREGION: the region of the port mapping NEGNETWORK: the VPC network of theport mapping NEGSUBNET: the subnet of the port mapping NEG
API
Send aPOST request to theregionNetworkEndpointGroups.insert method.
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/networkEndpointGroups{ "name": "NEG", "networkEndpointType": "GCE_VM_IP_PORTMAP", "network": "projects/PROJECT_ID/global/networks/NETWORK", "subnetwork": "projects/PROJECT_ID/regions/REGION/subnetworks/SUBNET"}Replace the following:
PROJECT_ID: the project ID of the port mappingNEGREGION: the region of the port mapping NEGNEG: the name of the port mapping NEGNETWORK: the VPC network of theport mapping NEGSUBNET: the subnet of the port mapping NEG
Add network endpoints to the port mapping NEG
Create one or more network endpoints with port mappings and attach them tothe port mapping NEG.
Network endpoints for port mapping NEGs are zonal and must fulfill the followingrequirements:
- Each network endpoint refers to a Compute Engine VM that hasa primary network interface in the same subnet as the port mapping NEG.
- Each network endpoint refers to a unique client destination port.
Each network endpoint must refer to a unique combination of service portand VM.
Console
In the Google Cloud console, go to theNetwork Endpoint Groups page.
Click the name of the port mapping NEG that you want to update.
ClickAdd network endpoints.
ClickVM instance, and then select a VM.
ClickVM port 1, and then enter a service port.
ClickClient port 1, and then enter a client destination port.
For each additional network endpoint that you want to add, clickAdd network endpoint, and then enter the network endpoint details.
ClickCreate.
gcloud
Use thenetwork-endpoint-groups update command. Include an--add-endpoint flag for eachnetwork endpoint that you want to attach.
gcloud compute network-endpoint-groups updateNEG \ --region=REGION \ --add-endpoint=client-destination-port=CLIENT_DESTINATION_PORT_1,instance=projects/PROJECT_ID/zones/ZONE_1/instances/VM_1_NAME,port=VM_1_PORT \ --add-endpoint=client-destination-port=CLIENT_DESTINATION_PORT_2,instance=projects/PROJECT_ID/zones/ZONE_2/instances/VM_2_NAME,port=VM_2_PORT
Replace the following:
NEG: the name of the port mapping NEGREGION: the region of the port mapping NEGCLIENT_DESTINATION_PORT_1: the client destinationport of the first network endpointPROJECT_ID: the project ID of the producerprojectZONE_1: the zone of the first network endpointVM_1_NAME: the name of the destination VMfor the first network endpointVM_1_PORT: the service port of the firstnetwork endpointCLIENT_DESTINATION_PORT_2: the client destination port of thesecond network endpointZONE_2: the zone of the second network endpointVM_2_NAME: the name of the destination VMfor the second network endpointVM_2_PORT: the service port of the secondnetwork endpoint
API
Send aPOST request to theregionNetworkEndpointGroups.attachNetworkEndpoints method.
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/networkEndpointGroups/NEG/attachNetworkEndpoints{ "networkEndpoints": [ { "clientDestinationPort":CLIENT_DESTINATION_PORT_1, "instance": "projects/PROJECT_ID/zones/ZONE_1/instances/VM_1_NAME", "port":SERVICE_PORT_1 }, { "clientDestinationPort":CLIENT_DESTINATION_PORT_2, "instance": "projects/PROJECT_ID/zones/ZONE_2/instances/VM_2_NAME", "port":SERVICE_PORT_2 } ]}Replace the following:
PROJECT_ID: the project ID of the producerprojectREGION: the region of the port mapping NEGNEG: the name of the port mapping NEGCLIENT_DESTINATION_PORT_1: the client destinationport of the first network endpointZONE_1: the zone of the first network endpointVM_1_NAME: the name of the destination VMfor the first network endpointSERVICE_PORT_1: the service port of the firstnetwork endpointCLIENT_DESTINATION_PORT_2: the client destinationport of the second network endpointZONE_2: the zone of the second network endpointVM_2_NAME: the name of the destination VMfor the second network endpointSERVICE_PORT_2: the service port of the secondnetwork endpoint
Create a port mapping service
To create a port mapping service, complete the following steps.
You can't change the port mapping NEG that is associated with abackend service after you add the NEG. However, you canupdate the port mapping NEG's network endpoints.
You can't update the connection of a forwarding rule after it is created.If you need to connect to a different backend service,delete the forwarding rule,and then create a new one.
Console
Start your configuration
In the Google Cloud console, go to theLoad balancing page.
- ClickCreate load balancer.
- ForType of load balancer, selectNetwork Load Balancer (TCP/UDP/SSL) and clickNext.
- ForProxy or passthrough, selectPassthrough load balancer and clickNext.
- ForPublic facing or internal, selectInternal and clickNext.
- ClickConfigure.
Basic configuration
- Enter aLoad balancer name.
- Select aRegion.
- Select aNetwork.
Backend configuration
- ForBackend type, selectPort mapping network endpoint group.
- Select aPort mapping network endpoint group.
Frontend configuration
- ClickFrontend configuration.
- Optional: Enter aName.
- Optional: Enter aDescription.
- Select aProtocol.
- Select aSubnetwork.
- ClickDone.
- ClickCreate.
gcloud
To create a backend service, use the
backend-services createcommand.gcloud compute backend-services createSERVICE \ --load-balancing-scheme=internal \ --region=REGION \ --network=NETWORK
Replace the following:
SERVICE: the name of the backend serviceREGION: the region of the backend serviceNETWORK: the producer VPCnetwork of the backend service
To add your port mapping NEG to the backend service, use the
backend-services add-backendcommand.You can't add multiple port mapping NEGs to a single backend service.
gcloud compute backend-services add-backendSERVICE \ --network-endpoint-group=NEG \ --network-endpoint-group-region=REGION
Replace
NEGwith the name of the port mappingNEG.To create a forwarding rule for your port mapping service, use the
forwarding-rules createcommand.The forwarding rule must beconfigured to forward traffic for all client destination ports.
gcloud compute forwarding-rules createRULE \ --load-balancing-scheme=INTERNAL \ --ip-protocol=PROTOCOL \ --network=NETWORK \ --subnet=SUBNET \ --address=IP_ADDRESS \ --ports=ALL \ --region=REGION \ --backend-service=SERVICE
Replace the following:
RULE: the name of the forwarding rulePROTOCOL: the protocol of the forwarding rule,which can be eitherTCPorUDPSUBNET: the producer subnet, which must bethe same subnet that's associated with the port mapping NEGIP_ADDRESS: the IP address of the forwardingrule, which must come from the IP address range of the producersubnet
API
To create a backend service, send a
POSTrequest to theregionBackendServices.insertmethod.POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/backendServices{ "name": "SERVICE", "loadBalancingScheme": "INTERNAL", "network": "projects/PROJECT_ID/global/networks/NETWORK"}Replace the following:
PROJECT_ID: the ID of the backend service'sprojectREGION: the region of the backend serviceSERVICE: the name of the backend serviceNETWORK: the producer VPCnetwork
To add your port mapping NEG to the backend service, send a
PATCHrequest to theregionBackendServices.patchmethod.You can't add multiple port mapping NEGs to a single backend service.
PATCH https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/backendServices/SERVICE{ "backends": [ { "group": "https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/networkEndpointGroups/NEG" } ]}Replace
NEGwith the name of your portmapping NEG.To create a forwarding rule for your port mapping service, send a
POSTrequest to theforwardingRules.insertmethod.The forwarding rule must beconfigured to forward traffic for all client destination ports.
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/forwardingRules{ "name": "RULE", "loadBalancingScheme": "INTERNAL", "IPProtocol": "PROTOCOL", "network": "projects/PROJECT_ID/global/networks/NETWORK", "subnetwork": "projects/PROJECT_ID/regions/REGION/subnetworks/SUBNET", "IPAddress": "IP_ADDRESS", "allPorts": true, "backendService": "projects/PROJECT_ID/regions/REGION/backendServices/SERVICE"}Replace the following:
RULE: the name of the forwarding rulePROTOCOL: the protocol of the forwarding rule,which can be eitherTCPorUDPSUBNET: the producer subnet, which must bethe same subnet that's associated with the port mapping NEGIP_ADDRESS: the IP address of the forwardingrule, which must come from the IP address range of the producer subnet
Publish the port mapping service
To make your port mapping service available to consumers,publish the serviceby creating a service attachment. When you create the service attachment,specify the forwarding rule that is associated with your port mapping service.
What's next
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 2025-12-17 UTC.