
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
Install the Functions for Node.js
/ 20
Create HTTP Cloud Function
/ 30
Debug HTTP Function
/ 20
Deploy HTTP Function
/ 30
Google Cloud Functions is an event-driven serverless compute platform. Cloud Functions allows you to write your code without worrying about provisioning resources or scaling to handle changing requirements.
Cloud Functions written in Javascript execute in a Node.js environment on Google Cloud Platform. You can run your Cloud Function in any standard Node.js runtime to enable portability and local testing.
In this lab, you will create a Cloud Function for Node.js that reports whether a specified temperature is acceptable or too hot. You will create, test, and debug your Cloud Function using Visual Studio Code on your local machine. Lastly, you'll deploy your function to Google Cloud Platform.
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:
The lab environment has provisioned an instance of Visual Studio Code for you. You can use this rather than installing and using a local version. This instance can be run completely in your browser, and has the required Cloud SDKs and Node packages installed.
Copy the URL listed under Visual Studio IDE
from Lab Details panel and open in a new tab or browser window.
Within Visual Studio Code, you should check to be sure that you are logged into Google Cloud with the correct credentials.
Open up a terminal session use this command:
You should see an output like this:
If the account listed and selected with the asterisk is not the student account listed in your Lab Details panel, you will need to login to Google Cloud.
To login, type the command:
If prompted, Do you want to continue (Y/n)?. Enter Y.
You will be presented with a URL to login (it may open in your browser automatically) with Google Credentials. Be sure to use the credentials specified in the Lab Details panel instead of your own. This will ensure that you are not charged to run the code in this lab.
If presented with a question to enable Google Cloud SDK to access your account, click Allow.
Finally, you will be presented with a verification code. Copy the code and enter it in the terminal where you were prompted from the login command.
You should now be logged in with the correct credentials for the lab. Verify using the auth list command:
The Functions Framework for Node.js is an open source FaaS (Function as a Service) framework for writing portable Node.js functions that is brought to you by the Google Cloud Functions team.
The Functions Framework lets you write lightweight functions that run in many different environments, including:
Create a app folder.
Create a new node.js app.
Now install the Functions Framework for Node.js.
Click Explorer in left pane and click Open folder and then click OK. Open your package.json
in explorer window. Verify that you see the functions framework listed as a dependency as shown in the example below. (The version shown below may vary. This is ok.)
The Functions Framework has now been successfully installed. You are now ready to create your Cloud Function.
Click Check my progress to verify the objective.
Create a local Cloud Function
In this section, you will create and test a HTTP Function that responds to HTTP requests.
Create a new file called index.js
in the same directory as your package.json file.
Add the following:
You are now ready to test the function.
Test function in Visual Studio Code
From this point on, this lab uses the integrated terminal within VS Code.
In Visual Studio Code, open a terminal window.
Run the following command:
This command starts a local server that is ready to call the validateTemperature
function when the server receives an HTTP request.
You should see the following output in your terminal window:
Create a second terminal window within VS Code by clicking the New Terminal
plus icon in the Terminal window pane. You will switch between these two terminal windows: the first for serving the function and the second for calling the function using curl.
You can switch between terminal windows by using the drop down. If a terminal window is currently serving a function, the drop down list refers to it as node
. Otherwise it is referred to zsh
(or the shell you are using).
In the second terminal window, run the following command to send a temperature payload of 50 to the local server serving the validateTemperature
function.
You should receive the following response from the cloud function:
In the second terminal window, test the function again by sending a "too high" temperature payload as shown below:
You should receive the following response from the cloud function:
Lastly, test the function by calling it with a missing payload.
You should receive the following response from the cloud function:
Ideally, the function should not return "too hot" if no temperature is provided. You have discovered a bug in the code.
Make sure to stop your function from running by pressing Ctrl + C
in the first terminal window serving your function.
Click Check my progress to verify the objective.
Now we'll start node with the inspect flag to enable debugging using the following command:
where the --inspect
flag tells Node.js to listen for a debugging client. For more info, please see the Node documentation on debugging.
You'll now need to attach the debugger to the running node process. Open the Command Palette in Visual Studio Code. If you're on a Mac, use Cmd + Shift + P
. If you're on Windows, use Ctrl + Shift + P.
Type Debug: Attach to Node Process
in the Command Palette and pick the top item in the list.
You will be prompted to select a process to attach to. Select the first node process (it will match the npx command you used to start the process).
This time you should see an orange status bar in VS Code indicating that the debugger is attached.
Set a breakpoint at line 3 by clicking inside the margin to the left of the line number.
The breakpoint icon should illuminate bright red, indicating this line of code is accessible by the debugger.
In the second terminal window, hit the breakpoint by running the following curl command.
You will see a yellow highlight appear over line 3. This highlight indicates that this line is the current statement being evaluated by the debugger.
Mouse-over the temp variable to verify that its contents are undefined
, since the request did not provide a temperature payload.
Click the step-over icon in status bar to execute the next statement.
You will see the current statement jump to the else portion of the if statement.
For this demo, you can assume that the specification requires all requests to send a temperature reading. In the unlikely event a temperature reading is not provided, the function should throw an exception.
Click the Disconnect button to disconnect the debugger.
In your first terminal window, stop serving your function from running by pressing Ctrl + C.
Update your function to add an if statement to throw an exception if temperature is undefined as shown below:
In your first terminal window, start running your cloud function again by running the following command without the --inspect flag to avoid attaching the debugger.
Verify that an exception is thrown by running the following command in your second terminal window:
You should see the following output returned from your request:
In your first terminal window, you'll also see the error logged by your function.
You can now stop running your function by pressing CTRL + C in your first terminal window.
Click Check my progress to verify the objective.
Now that you've created, tested, and debugged a Cloud Function on your local machine, you are ready to deploy it to Google Cloud.
Set the project configuration
In any terminal window, run the following command:
where the parameters are explained as follows:
deploy validateTemperature
- the gcloud subcommand for deploying a Cloud Function with the name validateTemperature
with an entry point named validateTemperature
--trigger-http
- the triggering event type--gen2
- the second generation runtime for this function--runtime nodejs20
- the targeted runtime for this function--allow-unauthenticated
- allows public access to call the function--region
- the region where the function will be deployedYou may be prompted to enable the Cloud Functions APIs. Type y
to enable the APIs.
Once deployment is completed, you will see the following in the output:
Note the value of the httpsTrigger. In your terminal window, use curl to call this public endpoint, replacing <your-region-and-project>
with the appropriate value.
and confirm that your cloud function has been deployed successfully by verifying the appropriate response.
Click Check my progress to verify the objective.
You can learn more about how Cloud Functions supports the Node.js runtime and how local debugging works with Cloud Functions.
...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 October 9, 2024
Lab Last Tested October 9, 2024
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