Checkpoints
Googleapis have been enabled
/ 25
Number generator Cloud Function deployed
/ 25
Multiplier Cloud Function deployed
/ 25
Floor Cloud Run service deployed
/ 25
Serverless Orchestration with Workflows
GSP853
Overview
Workflows is used to create serverless workflows that link a series of serverless tasks together in an order you define. You can combine the power of Google Cloud's APIs, serverless products like Cloud Functions and Cloud Run, and calls to external APIs to create flexible serverless applications.
A workflow is made up of a series of steps described using the Workflows YAML-based syntax. This is the workflow's definition. For a detailed explanation of the Workflows YAML syntax, see the Syntax reference page.
When a workflow is created, it is deployed, which makes the workflow ready for execution. An execution is a single run of the logic contained in a workflow's definition. All workflow executions are independent and the product supports a high number of concurrent executions.
Workflows requires no infrastructure management and scales seamlessly with demand, including scaling down to zero. With its pay-per-use pricing model, you only pay for execution time.
In this lab you will connect Cloud Functions and Cloud Run services with Workflows and connect two public Cloud Functions services, one private Cloud Run service, and an external public HTTP API into a workflow.
What you'll learn
-
Basics of Workflows.
-
How to connect public Cloud Functions with Workflows.
-
How to connect private Cloud Run services with Workflows.
-
How to connect external HTTP APIs with Workflows.
Prerequisites
Based on the content, it is recommended to have some familiarity with:
Setup and requirements
Before you click the Start Lab button
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.
This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
- Access to a standard internet browser (Chrome browser recommended).
- Time to complete the lab---remember, once you start, you cannot pause a lab.
How to start your lab and sign in to the Google Cloud Console
-
Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:
- The Open Google Console button
- Time remaining
- The temporary credentials that you must use for this lab
- Other information, if needed, to step through this lab
-
Click Open Google Console. The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
Note: If you see the Choose an account dialog, click Use Another Account. -
If necessary, copy the Username from the Lab Details panel and paste it into the Sign in dialog. Click Next.
-
Copy the Password from the Lab Details panel and paste it into the Welcome dialog. Click Next.
Important: You must use the credentials from the left panel. Do not use your Google Cloud Skills Boost credentials. Note: Using your own Google Cloud account for this lab may incur extra charges. -
Click through the subsequent pages:
- Accept the terms and conditions.
- Do not add recovery options or two-factor authentication (because this is a temporary account).
- Do not sign up for free trials.
After a few moments, the Cloud Console opens in this tab.
Activate Cloud Shell
Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.
- Click Activate Cloud Shell
at the top of the Google Cloud console.
When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. The output contains a line that declares the PROJECT_ID for this session:
gcloud
is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
-
(Optional) You can list the active account name with this command:
-
Click Authorize.
-
Your output should now look like this:
Output:
-
(Optional) You can list the project ID with this command:
Output:
Example output:
gcloud
, in Google Cloud, refer to the gcloud CLI overview guide.
Task 1. Enable services
-
Connect Cloud Shell to the lab's project:
-
Enable all necessary services:
Click Check my progress to verify that you've performed the above task.
In the next step, you will connect two Cloud Functions together in a workflow.
Task 2. Deploy a random number generator Cloud Function
The first function is a random number generator in Python.
-
Create and navigate to a directory for the function code:
-
Create a
main.py
file in the directory then add the following contents:
-
Dependencies in Python are managed with
pip
and expressed in a metadata file calledrequirements.txt
. Create arequirements.txt
file in the same directory then add the following contents:
-
Deploy the function with an HTTP trigger and with unauthenticated requests allowed with this command:
The service has encountered an error during container import
, please wait for a minute and run the command again.-
The deployed Cloud Function relies on Flask for HTTP processing. Cloud Function information can be viewed using the
gcloud
commands:
-
View the deployed Cloud Function URL:
- Using the
httpsTrigger.url
property - Using the
gcloud functions describe
command
- Using the
-
The function is ready for the workflow.
Click Check my progress to verify that you've performed the above task.
Task 3. Deploy a number multiplier Cloud Function
The second Cloud Function is a multiplier. It multiplies the received input by 2.
-
Create and navigate to a directory for the function code:
-
Create a
main.py
file in the directory then add the following contents:
-
When the application receives an HTTP request, this function extracts the
input
from the JSON body, multiplies it by 2 and returns in JSON format back to the caller. -
Create a new
requirements.txt
file in the same directory with the following contents:
-
Deploy the function with an HTTP trigger and with unauthenticated requests allowed with this command:
-
Once the Cloud Function is deployed, visit the URL of the function with the following
curl
command:
- The function is ready for the workflow.
Click Check my progress to verify that you've performed the above task.
Task 4. Connect the two Cloud Functions
-
In the first workflow, connect the two functions together. You will need the URLs for the functions you deployed earlier. To get the URLs, run the following commands:
-
Create a
workflow.yaml
file then add the following contents:
- Now update the
workflow.yaml
file. Add the URLs for the functions deployed earlier in the lab, the<region>
and<project-id>
.
In your first workflow, you used the first Cloud Function (Randomgen) to generate a random number. The result of this function is passed to the second Cloud Function (Multiply).
The second Cloud Function will then return a multiplier of the random number passed to it.
randomFunction
is called by multipleFunction
so randomFunction
needs to be declared first.-
Deploy the first workflow:
-
Execute the first workflow:
The command will return the workflow execution id.
-
Once the workflow is executed, you can see the result by passing in the execution ID given in the previous step:
The output will include result
and state
:
Task 5. Explore the Workflow interface
Use Cloud Console to update your workflow.
-
Find
Workflows
in Google Cloud Console under theApplication Integration
category: -
Click on
workflow
. -
Find your workflow and click on the SOURCE tab:
In this section you can see both the source code and a visualisation of the functions associated with the workflow.
Next, you will connect the external web service math.js
to this workflow.
Task 6. Connect to an external HTTP API
Math.js is an extensive math library for JavaScript and Node.js. In this section use the API to evaluate mathematical expressions derived from the Workflow.
-
The
math.js
API can be used to evaluate mathematical expressions like this:
-
Run the following to delete the existing workflow:
-
Replace the original workflow definition in
workflow.yaml
with the below code:
-
Deploy the updated workflow:
-
Return to the
Workflows
Console UI. -
Find your workflow and click on SOURCE tab:
-
The workflow now includes the
logFunction
. -
Click on Execute to execute the workflow. You'll see the details of the execution:
-
Notice the status code
200
and abody
with the output of log function.
Task 7. Deploy a Cloud Run service
Finalize the workflow with a call to a private Cloud Run
service. This means that the workflow needs to be authenticated to call the Cloud Run service. The Cloud Run service returns the math.floor
of the passed in number.
-
Create and navigate to a directory for the service code:
-
Create a
app.py
file in the directory then add the following contents:
Cloud Run deploys containers, so you need a Dockerfile
and your container needs to bind to 0.0.0.0
and PORT
env variable, which was done in the code above.
When it receives an HTTP request, this function extracts the input
from the JSON body, calls math.floor
and returns the result back to the caller.
-
In the same directory, create the a
Dockerfile
and add the following contents:
-
Run the following command to build the container:
-
Once the container is built, deploy to Cloud Run. Notice the
no-allow-unauthenticated
flag. This makes sure the service only accepts authenticated calls:
Once deployed, the service is ready for the workflow.
Click Check my progress to verify that you've performed the above task.
Task 8. Connect the Cloud Run service
Before you can configure Workflows to call the private Cloud Run service, you need to create a service account for Workflows to use.
-
Define an environment variable to hold the service account name:
-
Create a service account:
-
Grant
run.invoker
role to the service account; this will allow the service account to call authenticated Cloud Run services:
- Create a workflow definition file as
workflow.yaml
to include the Cloud Run service. Notice that now you are includingauth
field to make sure Workflows passes in the authentication token in its calls to the Cloud Run service:
-
Update the workflow, this time passing in the service-account:
-
Execute the workflow:
-
In a few seconds, run the following to take a look at the workflow execution to see the result:
The output will include an integer result
and state
:
Congratulations!
You have successfully completed the lab and demonstrated your knowledge of Workflows on Google Cloud infrastructure.
Google Cloud training and certification
...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.
Manual Last Updated February 13, 2023
Lab Last Tested February 13, 2023
Copyright 2023 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.