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
- Use the API Library to find and enable the Google Cloud Source Repository API.
- Use the API Library to find and enable the Google Cloud Functions API.
- Use the API Library to find and enable the Google Build API.
- Create a Source Repository
- From the console, visit the Source Repositories dashboard.
- Click Get Started.
- Click Create Repository.
- Click Create New Repository and then Continue.
- On the Create New Repository page, complete these fields:
- Repository Name: acg-cf-repo.
- Project: Select the current project.
- Click Create.
- Clone the Source Repository
- Return to the main console page and, from the top right, click Activate Cloud Shell.
- Expand the Projects panel by clicking the triangle next to the current project name.
- Copy the current project ID.
- In the Cloud Shell, enter the following command:
gcloud config set project <PROJECT_ID>
- Enter the following command to clone the repo in the shell environment:
gcloud source repos clone acg-cf-repo
- Clone the GitHub Repository
- Clone an existing Github repository with the following command:
git clone https://github.com/linuxacademy/content-gcpro-developer
- Clone an existing Github repository with the following command:
- Copy and Push the Files to the Source Repository Repo
- Copy the files to the source repository with the following command:
cp content-gcpro-developer/cloud-functions-lab/* acg-cf-repo
- Change to the repo directory:
cd acg-cf-repo
- Configure two variables:
user.email
anduser.name
with the following code:git config user.name “<USER_NAME>” git config user.email “<USER_EMAIL>”
- Push the files to the Source Repository repository:
git add .
git commit -m "Initial commit" .
git push origin master - Refresh the Source Repository page to confirm the files.
- Copy the files to the source repository with the following command:
- Create a Cloud Function
- Using the main navigation menu, under SERVERLESS, click Cloud Functions.
- Click CREATE FUNCTION.
- Apply the following settings:
- Name: acg-cf-greetings
- Region: us-central1
- Trigger: HTTP
- Enable Allow unauthenticated invocations and click SAVE.
- 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).
- Click NEXT.
- For the Runtime, select Python 3.9.
- Change the Source code to Cloud Source repository.
- Under Cloud Source repository, change the Repository name to "acg-cf-repo".
- For the Entry point, enter "greetings_http".
- Click DEPLOY.
- Using the main navigation menu, under SERVERLESS, click Cloud Functions.
- Test the Cloud Function
- Open the Cloud Function and, from the Trigger tab, click the provided URL.
- Confirm the expected result appears.
- Add the following argument to the URL:
?name=Joe
. - Choose the Testing tab.
- In the Triggering event section, insert the following code:
{"name":"Joey"} - Click Test the function.
- Review the output results.
- Add the following argument to the URL: