
Before you begin
- Labs create a Google Cloud project and resources for a fixed time
- Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
- On the top left of your screen, click Start lab to begin
By default, a Cloud Run service will connect to external endpoints on the internet using a dynamic IP address pool. This method is not suitable if the Cloud Run service needs to connect to an external endpoint that requires connections originating from a static IP address, for example, as a database or API using an IP address-based firewall. For those types of connections, you must configure your Cloud Run service to route requests through a static IP address.
This lab teaches the process of configuring a Cloud Run service to send requests using a static IP address.
These labs are based on intermediate knowledge of Google Cloud. While the steps required are covered in the content, it would be helpful to have familiarity with any of the following products:
In this lab, you learn to:
This Qwiklabs hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is a panel populated with the temporary credentials that you must use for this lab.
Copy the username, and then click Open Google Console. The lab spins up resources, and then opens another tab that shows the Choose an account page.
On the Choose an account page, click Use Another Account. The Sign in page opens.
Paste the username that you copied from the Connection Details panel. Then copy and paste the password.
After a few moments, the Cloud console opens in this tab.
Google Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud.
Google Cloud Shell provides command-line access to your Google Cloud resources.
In Cloud console, on the top right toolbar, click the Open Cloud Shell button.
Click Continue.
It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. For example:
gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
Output:
Example output:
Output:
Example output:
From Cloud Shell, enable the Cloud Run API :
If you are asked to authorize the use of your credentials, do so. You should then see a successful message similar to this one:
Create a LOCATION environment variable:
Set the compute region:
Clone the buildpacks samples repository to your local directory using the command below:
Copy an existing application that you will use later to create a second service.
Change to the sample code folder:
Using any editor, edit the file hello.go
in the sample-go
folder, and add the /service
http handler to the main
function:
Update the import
statement to add the io/ioutil
package:
Build an application container for your sample application with the pack
command.
If asked to provide authorization for the Shell, give it permission to run.
Run the command below to bind the sample app created in the previous step to port 8080, so that it can be accessed via the browser:
In the Cloud Shell window, click on the Web Preview icon and select Preview on port 8080.
A new browser window or tab will open showing the "hello, world" message. Success!
Back in the console window, click Control C
(Control and C key held down at the same time) to stop the web service and return control of the command line to the Cloud Shell.
First, set up authentication to Docker repositories:
When prompted, type Y.
Set the default Buildpacks builder:
Publish your container image:
You are now ready for the next part of the lab.
Creating a subnetwork for your Serverless VPC Access connector to reside in ensures that other compute resources in your VPC, such as Compute Engine VMs or Google Kubernetes Engine clusters, do not accidentally use the static IP you have configured to access the internet.
First, you will need to find the name of your VPC network by calling up a list of the networks associate with your account, using Cloud shell:
You should see something like the following in the output, identifying the networks you can use when attaching your Serverless VPC Access connector. For this lab, the only network that will be available is the default
network, so the information returned will be similar to the output shown below:
Create a subnetwork called mysubnet
with a range of 192.168.0.0/28
specified in CIDR format, to be used for your Serverless VPC Access connector.
To create this subnetwork in the project's default
network (note also that ), run the command:
To route your Cloud Run service's outbound traffic to a VPC network, you first need to set up a Serverless VPC Access connector.
Create a Serverless VPC Access connector named myconnector
with your previously created subnetwork named mysubnet
using the command:
If prompted to enable the vpcaccess.googleapis.com
on your project, type y.
Wait for the process to complete before proceeding.
To route outbound requests to external endpoints through a static IP (which is the main goal of this lab), you must first configure a Cloud NAT gateway.
Create a new Cloud Router to program your NAT gateway:
Next, reserve a static IP address using the command below, where myoriginip
is the name being assigned to your IP address resource.
A reserved IP address resource retains the underlying IP address when the resource it is associated with is deleted and re-created. Using the same region as your Cloud NAT router will help to minimize latency and network costs.
To route outbound requests to external endpoints through a static IP, you must configure a Cloud NAT gateway.
Bring all of the resources you've just created together to create a Cloud NAT gateway named mynat
.
To configure your router to route the traffic originating from the VPC network, run the command:
After NAT has been configured, deploy your Cloud Run service with the Serverless VPC Access connector, and set VPC egress to route all traffic through the VPC network:
If prompted to enable run.googleapis.com
for your project, type y.
If successful, after a few minutes you should see output similar to that below:
Store the service URL in an environment variable for use later:
Replace [Service URL] with the value of the Service URL
from the output of the previous step.
Navigate to the Service URL
in a separate browser tab.
You should see the same "hello, world" message that you saw displayed in the web preview pane in Task 1.
To test service to service communication, you create a second service that will respond to requests on its service URL. The service will be configured to only serve internal traffic routed to it using the VPC service connector.
Change to the sample code folder:
Using any editor, edit the file hello.go
in the sample-go-service
folder, and update the /
http handler in the main
function:
Edit the go.mod
file in this folder to update the module entry:
Build the second service from source, and deploy it to Cloud Run with the Serverless VPC Access connector allowing internal traffic only:
To create the Artifact registry repository, type Y at the prompt.
If successful, after a few minutes you should see output from the command that contains the service URL.
Store the service URL in an environment variable for use later:
Replace [Service URL] with the value of the Service URL
from the output of the previous step.
Navigate to the Service URL
in a separate browser tab.
You should not receive any response from the service since the service is configured to accept internal requests only from within the VPC network or VPC service control perimeter.
To verify internal service communication, test the services by making a request to the original Cloud Run service. You'll also verify the originating IP address of the calling service.
First, update the original Cloud Run service with an environment variable that contains the URL of the second Cloud Run sample-go-service
:
Use curl with the original sample-go
service URL, and append the /service
request path:
The output from the curl command contains the response from the second Cloud Run sample-go-service
, which was invoked by the original calling service sample-go
:
Update the service URL environment variable to a service that verifies the originating IP address:
Use curl with the original sample-go
service URL, and append the /service
request path:
The output from the curl command contains the originating IP address.
In the Google Cloud console, use the Navigation menu (), and navigate to VPC Network > IP Addresses.
Verify that the myoriginip
static IP address value matches the IP address in the output of the curl command.
After completing the above steps, you have:
Follow the Serverless Expeditions video series to learn more about how to utilise these products within your project.
Manual Last Updated February 22, 2024
Lab Last Tested February 22, 2024
Copyright 2022 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.
This content is not currently available
We will notify you via email when it becomes available
Great!
We will contact you via email if it becomes available
One lab at a time
Confirm to end all existing labs and start this one