Create an HTTP Google Cloud Function

30 minutes
  • 3 Learning Objectives

About this Hands-on Lab

Google Cloud Functions are a fully managed and serverless way to run event-driven code. They react to demand from zero to planet-scale and come with integrated monitoring, logging, and debugging. All you need to do is plug in your code! In this lab, we will introduce ourselves to Cloud Functions by writing our first function that will simply respond to an HTTP trigger; in other words, our function will run when you send a request to its URL.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Enable APIs and Set Up the Cloud Shell

To help us create our first Cloud Function, we will enable some APIs and get ready to use the Cloud Shell editor and terminal.

  1. Inside the GCP console, click the Activate Cloud Shell icon.
  2. Click the down triangle next to the project ID at the top of the page.
  3. When the dialog opens, copy the current Project ID.
  4. In the Cloud Shell, enter the following command:

    gcloud config set project <PROJECT_ID>

  5. Then, run this command to enable the necessary APIs:

    gcloud services enable

  6. Create a directory for your function and move into that directory.
  7. In the Cloud Shell, click Open Editor. Then choose Open in a new window.
  8. When the editor has opened in a new tab, go back to your original tab and click Open Terminal to get the terminal back.

You are now ready to start building your first Cloud Function.

Write the Hello World Function
  1. In the helloworld directory, create the file using the Cloud Shell editor:

    from flask import escape
    def hello_http(request):
        request_json = request.get_json(silent=True)
        request_args = request.args
        if request_json and 'name' in request_json:
            name = request_json['name']
        elif request_args and 'name' in request_args:
            name = request_args['name']
            name = 'World'
        return 'Hello {}!'.format(escape(name))
  2. Also inside the helloworld directory, create the requirements.txt file, specifying the Flask library and the version of the library. Use Flask==2.0.3

Deploy and Test the Hello World Function
  1. Back in the Cloud Shell terminal, and inside the helloworld directory, deploy the Hello World function.
  2. From the GCP menu, select Cloud Functions from the Compute section. Then select the hello_http function. Observe the various tabs that show you the details of your deployed function.
  3. Under the Trigger tab, click the URL and follow the redirect to trigger your function and see the "Hello World!" response.
  4. To customize the response, add a query parameter to the end of the URL (for example, ?name=Cloud%20Gurus).

Additional Resources

NOTE: In the lab, when editing the requirements.txt file, set the Flask version via: Flask==2.0.3

Log in to Google Cloud Platform by opening in a private browser window. Then, sign in using the credentials provided on the lab page.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?