Private Service Connect: Using Custom Domains to access Global Google APIs with PSC backends

1. Introduction

Private Service Connect (PSC) is a capability of Google Cloud networking that allows consumers to access managed services privately from inside their VPC network. PSC Backends are a subset of PSC features that enables a load balancer to be in front of global Google managed services such as Google Cloud Storage enabling other load balancing integrations to be applied such as a different FQDN. At the point of publishing this codelab, there is only a subset of Google services that can be used with global PSC Backends. That list can be found here.

This Codelab will explore how to set up PSC backends to access Google Cloud Storage with an internal FQDN.

What you'll learn

  • Deploying basic VPC networking infrastructure
  • Deploying basic buckets in Google Cloud Storage
  • Deploying a cross-region internal Application Load Balancer with a PSC backend to Google Cloud Storage

What you'll need

  • Google Cloud Project with Owner permissions
  • The following organizational policies not enforced in the Google Cloud Project: Shielded VMs

2. Codelab topology

edffc1205f998be9.png

In this Codelab, you will deploy a VPC network, subnets, private DNS zone, firewall rules, Google Cloud Storage Bucket, a sample file and a test VM. Next, you'll deploy a cross-regional application load balancer with a PSC backend for Google Cloud Storage. You'll finally test connectivity to the file in the Google Cloud Storage Bucket.

3. Setup and Requirements

Self-paced environment setup

  1. Sign-in to the Google Cloud Console and create a new project or reuse an existing one. If you don't already have a Gmail or Google Workspace account, you must create one.

295004821bab6a87.png

37d264871000675d.png

96d86d3d5655cdbe.png

  • The Project name is the display name for this project's participants. It is a character string not used by Google APIs. You can always update it.
  • The Project ID is unique across all Google Cloud projects and is immutable (cannot be changed after it has been set). The Cloud Console auto-generates a unique string; usually you don't care what it is. In most codelabs, you'll need to reference your Project ID (typically identified as PROJECT_ID). If you don't like the generated ID, you might generate another random one. Alternatively, you can try your own, and see if it's available. It can't be changed after this step and remains for the duration of the project.
  • For your information, there is a third value, a Project Number, which some APIs use. Learn more about all three of these values in the documentation.
  1. Next, you'll need to enable billing in the Cloud Console to use Cloud resources/APIs. Running through this codelab won't cost much, if anything at all. To shut down resources to avoid incurring billing beyond this tutorial, you can delete the resources you created or delete the project. New Google Cloud users are eligible for the $300 USD Free Trial program.

Start Cloud Shell

While Google Cloud can be operated remotely from your laptop, in this codelab you will be using Google Cloud Shell, a command line environment running in the Cloud.

From the Google Cloud Console, click the Cloud Shell icon on the top right toolbar:

Activate the Cloud Shell

It should only take a few moments to provision and connect to the environment. When it is finished, you should see something like this:

Screenshot of Google Cloud Shell terminal showing that the environment has connected

This virtual machine is loaded with all the development tools you'll need. It offers a persistent 5GB home directory, and runs on Google Cloud, greatly enhancing network performance and authentication. All of your work in this codelab can be done within a browser. You do not need to install anything.

4. Before you begin

Enable APIs

Inside Cloud Shell, make sure that your project is set up and configure variables.

gcloud auth login
gcloud config list project
gcloud config set project [YOUR-PROJECT-ID]
export projectid=[YOUR-PROJECT-ID]
export region1=us-central1
export zone1=us-central1-a
export region2=us-south1
echo $projectid
echo $region1
echo $zone1
echo $region2

Enable all necessary services

gcloud services enable compute.googleapis.com 
gcloud services enable networkmanagement.googleapis.com 
gcloud services enable storage.googleapis.com 
gcloud services enable dns.googleapis.com

5. Create the VPC Network

Create the VPC network where your VM client and load balancing components will be hosted.

VPC Network

From Cloud Shell

gcloud compute networks create myvpc \
    --subnet-mode=custom \
    --bgp-routing-mode=global 

Create the subnets in the VPC. The first subnet will be where your load balancer will be hosted. The second subnet is the proxy-only subnet for your load balancer, and the third is where your VM client will be hosted.

Create Subnets

From Cloud Shell

gcloud compute networks subnets create $region2-subnet \
    --network=myvpc \
    --range=10.100.0.0/24 \
    --region=$region2

gcloud compute networks subnets create $region2-proxy-subnet \
    --network=myvpc \
    --range=10.100.100.0/24 \
    --region=$region2 \
    --purpose=GLOBAL_MANAGED_PROXY \
    --role=ACTIVE

gcloud compute networks subnets create $region1-subnet \
    --network=myvpc \
    --region=$region1 \
    --range=10.200.0.0/24 

Create Network Firewall Policy and Firewall Rules

From Cloud Shell

gcloud compute network-firewall-policies create my-vpc-policy --global

gcloud compute network-firewall-policies associations create \
    --firewall-policy my-vpc-policy \
    --network myvpc \
    --name network-myvpc \
    --global-firewall-policy

To allow IAP to connect to your VM instances, create a firewall rule that:

  • Applies to all VM instances that you want to be accessible by using IAP.
  • Allows ingress traffic from the IP range 35.235.240.0/20. This range contains all IP addresses that IAP uses for TCP forwarding.

From Cloud Shell

gcloud compute network-firewall-policies rules create 1000 \
    --action ALLOW \
    --firewall-policy my-vpc-policy \
    --description "SSH with IAP" \
    --direction INGRESS \
    --src-ip-ranges 35.235.240.0/20 \
    --layer4-configs tcp:22  \
    --global-firewall-policy

6. Create a Google Cloud Storage Bucket, a Sample File, and Grant Permissions

From Cloud Shell

gcloud storage buckets create gs://$projectid-pscbackend --location=us

echo "Here is my bucket file contents" > my-bucket-contents.txt 

gcloud storage cp my-bucket-contents.txt gs://$projectid-pscbackend/my-bucket-contents.txt

computesa=$(gcloud iam service-accounts list \
    --filter='displayName:Compute Engine default service account' \
    --format='value(email)')

echo $computesa


gcloud storage buckets add-iam-policy-binding gs://$projectid-pscbackend \
    --member="serviceAccount:$computesa" \
    --role="roles/storage.objectViewer"

Sample Output

Creating gs://xxxxxxxxxxx-pscbackend/...
Copying file://my-bucket-contents.txt to gs://xxxxxxxxxxx-pscbackend/my-bucket-contents.txt
  Completed files 1/1 | 32.0B/32.0B                                                                                                                                                                  
xxxxxxxxxxx-compute@developer.gserviceaccount.com
bindings:
- members:
  - projectEditor:xxxxxxxxxxx
  - projectOwner:xxxxxxxxxxx
  role: roles/storage.legacyBucketOwner
- members:
  - projectViewer:xxxxxxxxxxx
  role: roles/storage.legacyBucketReader
- members:
  - projectEditor:xxxxxxxxxxx
  - projectOwner:xxxxxxxxxxx
  role: roles/storage.legacyObjectOwner
- members:
  - projectViewer:xxxxxxxxxxx
  role: roles/storage.legacyObjectReader
- members:
  - serviceAccount:xxxxxxxxxxx-compute@developer.gserviceaccount.com
  role: roles/storage.objectViewer
etag: CAI=
kind: storage#policy
resourceId: projects/_/buckets/xxxxxxxxxxx-pscbackend
version: 1

7. Expose Google Cloud Storage through a Cross-Regional Internal Application Load Balancer

Create the Cross-Regional Internal Application Load Balancer

First start by creating the load balancer components. You will create a PSC NEG, a backend service, a URL Map, and HTTP target proxies.

In Cloud Shell

gcloud compute network-endpoint-groups create gcs-$region2-neg \
    --region=$region2 \
    --network-endpoint-type=private-service-connect \
    --psc-target-service=storage.googleapis.com

gcloud compute backend-services create gcs-bes \
    --load-balancing-scheme=INTERNAL_MANAGED \
    --protocol=HTTP \
    --global

gcloud compute backend-services add-backend gcs-bes \
    --global \
    --network-endpoint-group=gcs-$region2-neg \
    --network-endpoint-group-region=$region2

gcloud compute url-maps create gcsilb \
    --default-service=gcs-bes \
    --global

gcloud compute target-http-proxies create gcs-http-proxy \
    --url-map=gcsilb \
    --global

Create the load balancer forwarding rule.

In Cloud Shell

gcloud compute forwarding-rules create gcs-ilb-fr \
    --load-balancing-scheme=INTERNAL_MANAGED \
    --network=myvpc \
    --subnet=$region2-subnet \
    --target-http-proxy=gcs-http-proxy \
    --ports=80 \
    --subnet-region=$region2 \
    --global

8. Create Cloud DNS Private Zone for company.com

First let's determine the IP address of the Load balancer for the A-record and export it as a variable

In Cloud Shell

gcloud compute forwarding-rules describe gcs-ilb-fr \
    --global

export lbip=$(gcloud compute forwarding-rules describe gcs-ilb-fr \
    --global \
    --format='value(IPAddress)')

echo $lbip

Sample Output

IPAddress: 10.100.0.4
IPProtocol: TCP
creationTimestamp: 'xxxxxxxxxxxxxxx'
description: ''
fingerprint: xxxxxxxxxx
id: 'xxxxxxxxxxxxxx'
kind: compute#forwardingRule
labelFingerprint: xxxxxxxxxx
loadBalancingScheme: INTERNAL_MANAGED
name: gcs-ilb-fr
network: https://www.googleapis.com/compute/v1/projects/[projectID]/global/networks/myvpc
networkTier: PREMIUM
portRange: 80-80
selfLink: https://www.googleapis.com/compute/v1/projects/[projectID]/global/forwardingRules/gcs-ilb-fr
selfLinkWithId: https://www.googleapis.com/compute/v1/projects/[projectID]/global/forwardingRules/xxxxxxxxxxxxxx
subnetwork: https://www.googleapis.com/compute/v1/projects/[projectID]/regions/us-south1/subnetworks/us-south1-subnet
target: https://www.googleapis.com/compute/v1/projects/[projectID]/global/targetHttpProxies/gcs-http-proxy
10.100.0.4

Next, create the DNS private zone and A record for the internal load balancer you just created.

From Cloud Shell

gcloud dns managed-zones create "company-com" \
    --dns-name=company.com. \
    --description="company.com private dns zone" \
    --visibility=private \
    --networks=myvpc

gcloud dns record-sets create "storage.company.com" \
    --zone="company-com" \
    --type="A" \
    --ttl="300" \
    --rrdatas="$lbip"

9. Create the test VM

Create consumer-client VM

From Cloud Shell

gcloud compute instances create testvm \
    --zone="$zone1" \
    --subnet="$region1-subnet" \
    --no-address \
    --metadata "startup-script=#! /bin/bash
cat <<EOF > /etc/profile.d/gcp-startup-vars.sh
export MYBUCKET=\"$projectid-pscbackend\"
export computesa=\"$computesa\"
EOF
chmod +x /etc/profile.d/gcp-startup-vars.sh"

10. Test Connecting to Google Cloud Storage Through the Load Balancer

Connect to the Test VM

In Cloud Shell

gcloud compute ssh "testvm"\
    --zone "$zone1"\
    --tunnel-through-iap \
    --project $projectid

Test Connectivity

In Test VM

TOKEN=$(curl -s -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/$computesa/token" | jq -r .access_token)

curl -H "Authorization: Bearer $TOKEN" "http://storage.company.com/$MYBUCKET/my-bucket-contents.txt"

Expected Output

Here is my bucket file contents

Exit from the VM.

In TestVM

exit

SUCCESS!

11. Cleanup steps

From Cloud Shell

gcloud dns record-sets delete "storage.company.com" \
    --zone="company-com" \
    --type="A"

gcloud dns managed-zones delete "company-com"

gcloud compute forwarding-rules delete gcs-ilb-fr \
    --global \
    --quiet

gcloud compute target-http-proxies delete gcs-http-proxy \
    --global \
    --quiet

gcloud compute url-maps delete gcsilb \
    --global \
    --quiet

gcloud compute backend-services delete gcs-bes \
    --global \
    --quiet

gcloud compute network-endpoint-groups delete gcs-$region2-neg \
    --region=$region2 \
    --quiet

gcloud storage rm -r gs://$projectid-pscbackend \
    --quiet

gcloud compute instances delete testvm \
    --zone=$zone1 \
    --quiet

gcloud compute network-firewall-policies rules delete 1000 \
    --firewall-policy my-vpc-policy \
    --global-firewall-policy \
    --quiet

gcloud compute network-firewall-policies associations delete \
    --firewall-policy my-vpc-policy \
    --name=network-myvpc \
    --global-firewall-policy \
    --quiet

gcloud compute network-firewall-policies delete my-vpc-policy \
    --global \
    --quiet

gcloud compute networks subnets delete $region1-subnet \
    --region=$region1 \
    --quiet

gcloud compute networks subnets delete $region2-proxy-subnet \
    --region=$region2 \
    --quiet

gcloud compute networks subnets delete $region2-subnet \
    --region=$region2 \
    --quiet

gcloud compute networks delete myvpc \
    --quiet

12. Congratulations!

Congratulations for completing the Codelab.

What we've covered

  • Deploying basic files in Google Cloud Storage
  • Deploying a PSC backend for Google Cloud Storage
  • Deploying a cross-region internal Application Load Balancer