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. When the terminal is ready, run this command to enable the necessary APIs:

    gcloud services enable cloudbuild.googleapis.com cloudfunctions.googleapis.com

  3. Create a directory for your function and move into that directory.
  4. In the Cloud Shell, click Open Editor. Then choose Open in a new window.
  5. 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 main.py 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']
        else:
            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.

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

Log in to Google Cloud Platform by opening https://console.cloud.google.com/ 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.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


$2,495.00

Checkout
Sign In
Welcome Back!

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