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. Navigate to the Cloud Functions dashboard.
  2. Click Create function.
  3. Apply the following settings:
    • Name: acg-cf-greetings
    • Region: us-central1
    • Trigger: HTTP
    • Allow unauthenticated invocations: Checked
  4. Expand Runtime, build, connections and security settings and, from the Runtime service account list, choose Compute Engine default service account.
  5. Click Save and then Next.
    • Runtime: Python 3.9
    • Source code: Cloud Source repository
    • Repository: acg-cf-repo
    • Entry point: greetings_http
  6. 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?