
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
Set up a Memorystore for Redis instance
/ 20
Set up Serverless VPC Access
/ 20
Create an event-driven function for PubSub
/ 20
Create an HTTP function
/ 20
Connect to a VM instance from an HTTP function
/ 20
Cloud Run functions let you connect directly to your VPC network enabling your functions to access Compute Engine VM instances, Memorystore instances, and other resources with an internal IP address.
In this lab, you will use Serverless VPC Access to connect a Cloud Run function to your VPC network and access data in a Memorystore instance running in your network. You will also use the connector to access a web server running on a VM instance in your network.
In this lab, you will:
Here's a high-level overview of the resources that you create in this lab:
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:
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:
Before you create Cloud Run functions, you must enable the relevant APIs.
To set your Project ID
and Region
environment variables, activate Cloud Shell, and then run the following commands:
Run the following command to enable all necessary services:
Memorystore is a Google Cloud service that provides a fully managed, highly available, scalable, and secure in-memory cache solution for Redis and Memcached.
In this task, you set up a Memorystore for Redis instance in your Google Cloud project. This instance is used by the Cloud Run function to store data which you will develop in a later task of the lab.
Set an environment variable for the Memorystore instance name:
To create a 2GiB Memorystore for Redis instance, run the following command:
To view the instance details after it is successfully created, run the following command:
Save the host IP address of Memorystore instance in an environment variable
Save the port of the Memorystore instance in an environment variable:
Click Check my progress to verify the objective.
To send requests from Cloud Run functions to resources in your VPC network and receive the corresponding responses without using the public internet, you must set up Serverless VPC Access.
In the Google Cloud console Navigation menu (), under Networking, click VPC network, and then select Serverless VPC access.
To create a Serverless VPC Access connector, click Create Connector.
For the connector Name, type test-connector
For Region, select
For Subnet, select Custom IP range.
For the IP range, type 10.8.0.0
The Region specified for the connector must be the same region where your Cloud Run functions are hosted.
Leave the other settings as their defaults, and Click Create.
To verify the connector details, in Cloud Shell, run the following command:
Click Check my progress to verify the objective.
Now that the Serverless VPC Access connector is created, you can develop a Cloud Run function to access data in the Memorystore Redis instance using the connector.
In this task, you create a Pub/Sub event-driven function in Python to store and fetch data to and from the Memorystore instance.
You must first create a topic that the function will listen for messages. Set an environment variable for the topic name:
To create a topic, run the following command:
Run the following command to create the folder and files for the function and navigate to the folder:
Click the Open Editor button on the toolbar of Cloud Shell. (You can switch between Cloud Shell and the code editor by using the Open Editor and Open Terminal icons as required).
In the Cloud Shell Editor, select the redis-pubsub
folder.
In the Editor, add the following code to the redis-pubsub/main.py
file:
To specify the function code dependencies, add the following content to the redis-pubsub/requirements.txt
file.
To deploy the Cloud Run function, in Cloud Shell, run the following command:
For the pop-up Bind the role to service account enter n
.
To test the function, publish a message to the topic:
In Google Cloud console, navigate to the Cloud Run functions overview page, and click the python-pubsub-function
name.
Click Logs.
Verify the log entry that indicates that the JSON data payload sent in the Pub/Sub message is added to Redis.
Click Check my progress to verify the objective.
In this task, you create an HTTP function in Python to query the Memorystore for Redis instance and return data based on an identifier that is passed in the request.
Run the following command to create a folder and files for the HTTP function and navigate to the folder:
In the Cloud Shell toolbar, click Open Editor.
In the Cloud Shell Editor, select the redis-http
folder.
In the editor, add the following code to the redis-http/main.py
file:
To specify the function code dependencies, add the following lines to the redis-http/requirements.txt
file:
To deploy the Cloud Run function, in Cloud Shell, run the following command:
After the function is deployed, retrieve the HTTP URI of the function and store it in an environment variable:
To test the function, run the following curl
command:
Verify that the response from the HTTP function matches the JSON data that was previously stored in the Redis instance:
Click Check my progress to verify the objective.
Connecting to resources in your VPC network from Cloud Run functions requires the use of a Serverless VPC Access connector. In this task, you create a VM, and an HTTP function to connect to the VM instance.
Before creating a VM, in Cloud Shell, download a startup script that installs and runs a simple web server:
To view the contents of the VM startup script, run:
Set an environment variable for the zone in which the VM will be created:
To create a VM with the startup script, run the following command:
Add a firewall rule to the VPC network to enable HTTP traffic to reach the VM:
To verify that the VM is created and running, in Google Cloud console, navigate to Compute Engine, and click VM instances.
There should be one VM listed with a green check to indicate that it is running:
Save the internal and external IP addresses of the VM in environment variables. These variables will be used later when testing the function.
In this subtask, you write the HTTP function which when triggered connects to the VM that you created.
Run the following command to create a folder and files for the HTTP function code and navigate to the folder:
In the Cloud Shell toolbar, click Open Editor.
In the Cloud Shell Editor, select the vm-http
folder.
In the editor, add the following code to the vm-http/main.py
file:
In the editor, add the following to the vm-http/requirements.txt
file:
To deploy the Cloud Run function, in Cloud Shell, run the following command:
After the function is deployed successfully, to extract the function's URL, run the following command:
Test the function by providing the VM's external IP address in the function URL:
The command should return the HTML response from the web server running on the VM:
Repeat the test, providing the VM's internal IP address to the function URL:
The function times out after approximately 10 seconds since by default your function cannot access the VM on its internal IP address.
An error is reported:
To access the VM or other internal resources in your VPC network from your Cloud Run function, you must use a Serverless VPC Access connector.
Re-enable the Cloud Function API:
Redeploy the HTTP function setting the VPC connector that you created earlier in this lab:
After the function is redeployed, test the internal connection to the VM:
The function now returns the HTML response from the web server running on the VM:
Click Check my progress to verify the objective.
In this lab, you created a Memorystore for Redis instance and set up Serverless VPC Access to connect Cloud Run functions to the instance in your VPC network. You then created a Pub/Sub event-driven function to connect to the Redis instance and store the contents of the message. You also created an HTTP function to connect to the Redis instance and fetch the data from the instance based on an identifier passed in the request to the function. Finally, you created a VM and used a Serverless VPC Access connector to connect an HTTP function to the VM through its internal IP address.
For more information about Memorystore, view the documentation:
For more information on Serverless VPC Access view the documentation:
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