Introduction
A workflow manages repetitive processes and tasks that occur in a particular order. In GCP, Workflows is a fully-managed orchestration platform that executes services in a specific order. These workflows combine custom services hosted on other Google Cloud services and APIs. Workflows are serverless, can be easily scaled, and incurs no charges when idle. Since workflows contain no code or dependencies, it does not require security patches. They can reliably execute without maintenance. A workflow can hold state and wait for up to a year. Let us cover some of the basic and advanced concepts of Workflow in GCP.
Workflow Basics
Workflows support various use cases, including IT process automation, business processes, batch jobs and Service orchestration. A workflow consists of a series of steps as described in the Workflow syntax. This is written in either YAML or JSON and is known as the workflow's definition. Deploying a workflow makes it ready for execution. An execution involves running the logic contained in a workflow's definition.
Creating Workflows
Step 1: Enable the Workflows API.
Step 2: Create a Service account and assign the necessary roles to create and manage workflows.
Step 3: Go to the Workflows page and select Create.
Step 4: Enter the name for a new workflow, and select an appropriate region and a service account. Click on Next.
Step 5: Enter the Workflow definition in the editor and click on Deploy.
Select the workflow's name on the Workflows page to update an existing workflow. Navigate to the Edit Workflow page and make the required changes. Note that the workflow's name can not be edited. Click on Next and the Deploy.
Executing Workflows
Users can execute a workflow using the client libraries using the Google Cloud CLI or by sending a request to the Workflows REST API. To execute using REST APIs, follow the steps:
Step 1: Get the list of workflows in the terminal and find the one required to be executed.
Step 2: Run the following command to execute the workflow.
curl \
--request POST \
--header "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
--header 'Content-Type: application/json' \
"https://workflowexecutions.googleapis.com/v1/projects/PROJECTID/locations/WORKFLOWREGION/workflows/WORKFLOWNAME/executions"
If the workflow takes additional arguments, include the following flag to the command.
--data '{"argument":"{\"PARAMETER\":\"VALUE\"}"}'
Executing a workflow runs the workflow definition associated with the workflow. After execution, the history and results are retained for a limited time. We can retrieve the execution attempts and ID by running this command.
gcloud workflows executions list WORKFLOW_NAME
The following command is used to check the status of an execution attempt.
gcloud workflows executions wait EXECUTION_ID
The status of the last execution attempt can be retrieved using this command.
gcloud workflows executions describe-last
Passing Runtime arguments
We can pass runtime arguments in a workflow execution request and access them using workflow variables.
Step 1: Create a new workflow or choose an existing one from the Workflows page.
Step 2: Add a params field in the workflow definition as shown.
main:
params: [ARG_NAME]
steps:
...
Step 3: Deploy the workflow.
Scheduling Workflows
Cloud Scheduler is used to run workflows on a particular schedule. This can be done from the Google Cloud Console or in the CLI.
Step 1: Enable the Cloud Scheduler API.
Step 2: Create a service account to make requests to the Workflow API. Assign the account a workflows.invoker role to permit triggering a workflow.
Step 3: Create a scheduler job to trigger the workflow.
Step 4: Go to the Edit Workflow page and click Add new Trigger > Scheduler.
Step 5: Define the schedule and configure the execution. Click on Create.