Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Labs

Create an HTTP Google Cloud Function

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.

Labs

Path Info

Level
Clock icon Beginner
Duration
Clock icon 30m
Published
Clock icon Apr 09, 2021

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    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>

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

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

    1. Create a directory for your function and move into that directory.
    2. In the Cloud Shell, click Open Editor. Then choose Open in a new window.
    3. 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.

  2. Challenge

    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. Use Flask==2.0.3 and werkzeug==2.2.2.

  3. Challenge

    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).

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans