
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
Deploy the function
/ 100
A Cloud Run function is a piece of code that runs in response to an event, such as an HTTP request, a message from a messaging service, or a file upload. Cloud events are things that happen in your cloud environment. These might be things like changes to data in a database, files added to a storage system, or a new virtual machine instance being created.
Since Cloud Run functions are event-driven, they only run when something happens. This makes them a good choice for tasks that need to be done quickly or that don't need to be running all the time.
For example, you can use a Cloud Run function to:
You can write your code in any language that supports Node.js, and you can deploy your code to the cloud with a few clicks. Once your Cloud Run function is deployed, it will automatically start running in response to events.
This hands-on lab shows you how to create, deploy, and test a Cloud Run function using the Google Cloud console.
This hands-on lab shows you how to create, deploy, and test a Cloud Run function using the Google Cloud Shell command line.
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources are made available to you.
This hands-on lab lets you do the lab activities in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials 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 dialog opens for you to select your payment method. On the left is the Lab Details pane with the following:
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
If necessary, copy the Username below and paste it into the Sign in dialog.
You can also find the Username in the Lab Details pane.
Click Next.
Copy the Password below and paste it into the Welcome dialog.
You can also find the Password in the Lab Details pane.
Click Next.
Click through the subsequent pages:
After a few moments, the Google Cloud console opens in this tab.
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. Cloud Shell provides command-line access to your Google Cloud resources.
Click Activate Cloud Shell at the top of the Google Cloud console.
Click through the following windows:
When you are connected, you are already authenticated, and the project is set to your Project_ID,
gcloud
is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
Output:
Output:
gcloud
, in Google Cloud, refer to the gcloud CLI overview guide.
First, you're going to create a simple function named helloWorld
. This function writes a message to the Cloud Run functions logs. It is triggered by Cloud Run function events and accepts a callback function used to signal completion of the function.
For this lab the Cloud Run function event is a pub/sub topic event. A pub/sub is a messaging service where the senders of messages are decoupled from the receivers of messages. When a message is sent or posted, a subscription is required for a receiver to be alerted and receive the message. To learn more about pub/subs, in Pub/Sub Guides, see Pub/Sub: A Google-Scale Messaging Service.
To learn more about the event parameter and the callback parameter, in Cloud Run functions Documentation, see Background Functions.
To create a Cloud Run function:
In Cloud Shell, run the following command to set the default region:
Create a directory for the function code:
Create and open index.js
to edit:
Copy the following into the index.js
file:
Exit nano (Ctrl+x) and save (Y) the file.
Create and open package.json
to edit:
Copy the following into the package.json
file:
Exit nano (Ctrl+x) and save (Y) the file.
Install the package dependencies
Expected Output:
For this lab, you'll set the --trigger-topic
as cf_demo
.
Deploy the nodejs-pubsub-function function to a pub/sub topic named cf-demo
Verify the status of the function:
An ACTIVE status indicates that the function has been deployed.
Expected Output:
Every message published in the topic triggers function execution, the message contents are passed as input data.
Click Check my progress to verify your performed task. If you have completed the task successfully you will receive an assessment score.
After you deploy the function and know that it's active, test that the function writes a message to the cloud log after detecting an event.
Invoke the PubSub with some data.
Example output:
View logs to confirm that there are log messages with that execution ID.
Check the logs to see your messages in the log history:
The Cloud Run function will output information similar to below:
Your application is deployed, tested, and you can view the logs.
Below are multiple-choice questions to reinforce your understanding of this lab's concepts. Answer them to the best of your abilities.
You used the Google Cloud console to create, deploy, and test a Cloud Run function using the command line.
This lab is part of a series of labs called Qwik Starts. These labs are designed to give you a little taste of the many features available with Google Cloud. Search for "Qwik Starts" in the lab catalog to find the next lab you'd like to take!
...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.
Manual Last Updated January 20, 2025
Lab Last Tested January 20, 2025
Copyright 2025 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