Integrating Google Cloud Functions and Cloud Source Repositories

30 minutes
  • 7 Learning Objectives

About this Hands-on Lab

Increasingly, organizations find that breaking down their monolithic applications into a series of microservices — which can scale effortlessly — is a viable programming model. Google Cloud Functions are event-driven, serverless functions that can be triggered either directly by an HTTP call (with or without arguments, in a variety of formats) or by specific occurrences, such as a file being uploaded to a targeted Cloud Storage bucket. In this hands-on lab, you’ll lay the groundwork for managing the Cloud Functions source code in a Google Cloud Source Repository and then deploy and test that Cloud Function.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Enable Required APIs
  1. Use the API Library to find and enable the Google Cloud Source Repository API.
  2. Use the API Library to find and enable the Google Cloud Functions API.
  3. Use the API Library to find and enable the Google Build API.
Create a Source Repository
  1. From the console, visit the Source Repositories dashboard.
  2. Click Get Started.
  3. Click Create Repository.
    1. Click Create New Repository and then Continue.
  4. On the Create New Repository page, complete these fields:
    • Repository Name: acg-cf-repo.
    • Project: Select the current project.
  5. Click Create.
Clone the Source Repository
  1. Return to the main console page and, from the top right, click Activate Cloud Shell.
  2. Expand the Projects panel by clicking the triangle next to the current project name.
  3. Copy the current project ID.
  4. In the Cloud Shell, enter the following command:

    gcloud config set project <PROJECT_ID>

  5. Enter the following command to clone the repo in the shell environment:
    gcloud source repos clone acg-cf-repo
Clone the GitHub Repository
  1. Clone an existing Github repository with the following command:
    git clone https://github.com/linuxacademy/content-gcpro-developer
Copy and Push the Files to the Source Repository Repo
  1. Copy the files to the source repository with the following command:

    cp content-gcpro-developer/cloud-functions-lab/* acg-cf-repo

  2. Change to the repo directory:

    cd acg-cf-repo

  3. Configure two variables: user.email and user.name with the following code:
    git config user.name “<USER_NAME>”
    git config user.email “<USER_EMAIL>”
  4. Push the files to the Source Repository repository:
    git add .
    git commit -m "Initial commit" .
    git push origin master
  5. Refresh the Source Repository page to confirm the files.
Create a Cloud Function
  1. Using the main navigation menu, under SERVERLESS, click Cloud Functions.
    1. Click CREATE FUNCTION.
    2. Apply the following settings:
      • Name: acg-cf-greetings
      • Region: us-central1
      • Trigger: HTTP
    3. Enable Allow unauthenticated invocations and click SAVE.
    4. In the "Runtime, build, connections and security settings" section, under the RUNTIME tab, choose the "Maximum number of instances" to be 1 (Instead of 3,000).
    5. Click NEXT.
    6. For the Runtime, select Python 3.9.
    7. Change the Source code to Cloud Source repository.
    8. Under Cloud Source repository, change the Repository name to "acg-cf-repo".
    9. For the Entry point, enter "greetings_http".
    10. Click DEPLOY.
Test the Cloud Function
  1. Open the Cloud Function and, from the Trigger tab, click the provided URL.
  2. Confirm the expected result appears.
    1. Add the following argument to the URL: ?name=Joe.
    2. Choose the Testing tab.
    3. In the Triggering event section, insert the following code:
      {"name":"Joey"}
    4. Click Test the function.
    5. Review the output results.

Additional Resources

You’ve been asked to lay the groundwork for your company's new microservice by deploying an example Cloud Function. Because the Cloud Function will be worked on by the development team, the source code will need to be stored in a source repository. The microservice will be triggered by an HTTP call that passes in an argument contained in either a JSON file or passed as a URL argument, so your work will need to demonstrate both capabilities.

To accomplish this task, you’ll need to complete the following steps:

  1. Enable the necessary APIs.
  2. Create a new source repository.
  3. Clone an existing repository.
  4. Push the code from the cloned repository into the new repository.
  5. Create the Cloud Function.
  6. Test the Cloud Function.

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?