Welcome back! In the last lesson, you learned how to build and deploy simple serverless APIs using Google Cloud Functions and API Gateway. You saw how a single Cloud Function can handle a request and return a response.
However, many real-world applications require more than just a single step. For example, when processing an online order, you might need to:
- Validate the order details
- Charge the customer
- Record the order in a database
Each of these steps could be handled by a separate Cloud Function. But how do you make sure they run in the right order and that data flows smoothly from one step to the next? This is where Google Cloud Workflows come in. Workflows let you organize and manage multi-step processes in your serverless applications.
Before we dive into Workflows, let's quickly remind ourselves how Cloud Functions fit into serverless applications.
A Cloud Function is a small piece of code that runs in response to an event, such as an API call or a message. In the previous lesson, you learned how to:
- Write a
Cloud Functionthat processes input and returns output - Connect a
Cloud Functionto anAPI Gatewayendpoint
In this lesson, we will see how to connect several Cloud Functions together to handle a more complex process.
Google Cloud Workflows help you coordinate multiple Cloud Functions (or other Google Cloud services) into a single workflow.
A workflow is like a flowchart: it defines a series of steps and the order in which they run. Each step can perform a task, make a choice, or wait for something to happen.
Let's use a real-world example: processing an online order. Imagine you have three steps:
- Validate the order (check if the amount is valid)
- Charge the customer (simulate payment)
- Record the order (save it to a database)
With Workflows, you can define a process that runs these steps one after another, passing the result from one step to the next.
Let's build up the order processing workflow step by step. We will use three Cloud Functions: one for each step in the process.
First, we need a Cloud Function to check if the order amount is valid.
- This function receives a request with an
orderobject. - It checks if the
amountis greater than 0. If not, it returns an error. - If the amount is valid, it adds a
"validated": Truefield to the order and returns it.
Example input:
Example output:
Next, we need a Cloud Function to simulate charging the customer.
- This function takes the output from the previous step.
- It creates a
payment_idbased on the order ID and adds achargedflag. - It returns the updated event.
Example input:
Example output:
Finally, we need a Cloud Function to save the order to a Firestore collection.
- This function connects to a Firestore collection called
orders. - It creates a document with the order ID, amount, payment ID, and a timestamp.
- It saves the document to the collection and returns a status.
Example input:
Example output:
Now, let's see how these functions are connected using Google Cloud Workflows in a YAML definition file.
Here is the relevant part of the workflow definition:
- The workflow starts by calling the
validate_orderfunction. - Each step calls the next
Cloud Function, passing along the output from the previous step. - The workflow ends by returning the final output from the
record_orderfunction.
This setup ensures that the order is validated, charged, and recorded in sequence.
To deploy and test this workflow, you can use the gcloud CLI and the Google Cloud Console. Here’s a high-level overview of the process:
-
Deploy the Cloud Functions:
-
Deploy the Workflow:
-
Execute the Workflow: You can trigger the workflow with a sample order:
Expected output:
If everything works, the workflow will process the order through all three steps. The final output will look like this:
This shows that the order was validated, charged, and recorded successfully.
In this lesson, you learned how Google Cloud Workflows help you organize and run multi-step workflows in your serverless applications. You saw how to:
- Break a process into separate
Cloud Functions - Connect those functions in a workflow using Workflows
- Deploy and test the workflow using Google Cloud tools
Next, you'll get hands-on practice building and running your own Workflows. This will help you reinforce what you've learned and prepare you for more advanced serverless patterns. Good luck!
