This tutorial shows you how to use Cloud Scheduler to automatically execute Workflows so that a workflow runs on a particular schedule, in this case every 5 minutes.
Objectives
- Create and deploy a workflow that can receive runtime arguments.
- Create a Cloud Scheduler job that triggers your workflow and executes it every 5 minutes, passing in JSON arguments in the correct format.
Costs
In this document, you use the following billable components of Google Cloud:
To generate a cost estimate based on your projected usage,
use the pricing calculator.
Before you begin
- Set up your environment for Cloud Scheduler, including creating an App Engine app.
Enable the Workflows API.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.- Set the default location used in this tutorial:
Replacegcloud config set workflows/location REGION
REGIONwith the supported Workflows location of your choice. - Create a service account for Workflows to use; for example,
sa-name.gcloud iam service-accounts create sa-name
- Grant your service account the
workflows.invokerrole so that the account has permission to trigger your workflow:gcloud projects add-iam-policy-binding PROJECT_ID \ --member "serviceAccount:sa-name@PROJECT_ID.iam.gserviceaccount.com" \ --role "roles/workflows.invoker"
Replace - Grant the
logging.logWriterrole to the service account.gcloud projects add-iam-policy-binding PROJECT_ID \ --member "serviceAccount:sa-name@PROJECT_ID.iam.gserviceaccount.com" \ --role "roles/logging.logWriter"
PROJECT_ID with your Google Cloud project ID.
Create a workflow that receives runtime arguments
A workflow definition is made up of a series of steps described using the Workflows syntax, which can be written in either YAML or JSON format. After creating a workflow, you deploy it to make it available for execution.
Console
In the Google Cloud console, go to the Workflows page:
Click Create.
Enter
myFirstWorkflowas a name for your new workflow.Select us-east1 for the region.
Select the service account you created previously.
Click Next.
In the workflow editor, enter the following definition for your workflow.
YAML
main: params: [args] steps: - step1: assign: - outputVar: ${"Hello, " + args.firstName + " " + args.lastName + "!"} - step2: return: ${outputVar}
JSON
{ "main": { "params": [ "args" ], "steps": [ { "step1": { "assign": [ { "outputVar": "${\"Hello \" + args.firstName + \" \" + args.lastName}" } ] } }, { "step2": { "return": "${outputVar}" } } ] } }
This workflow returns a "Hello" greeting to a person whose first and last name you pass as runtime arguments.
Click Deploy.
gcloud
- Open a terminal.
Save the following workflow definition as a YAML or JSON file, such as myFirstWorkflow.yaml or myFirstWorkflow.json.
YAML
main: params: [args] steps: - step1: assign: - outputVar: ${"Hello, " + args.firstName + " " + args.lastName + "!"} - step2: return: ${outputVar}
JSON
{ "main": { "params": [ "args" ], "steps": [ { "step1": { "assign": [ { "outputVar": "${\"Hello \" + args.firstName + \" \" + args.lastName}" } ] } }, { "step2": { "return": "${outputVar}" } } ] } }
This workflow returns a "Hello" greeting to a person whose first and last name you pass as runtime arguments.
Deploy the workflow by entering the following command:
gcloud workflows deploy myFirstWorkflow \ --location=us-east1 \ --source=myFirstWorkflow.yaml_OR_json \ --service-account=sa-name@PROJECT_ID.iam.gserviceaccount.com
Replace
yaml_OR_jsonwithyamlorjsondepending on the format of the Workflows definition file that you created previously.
Schedule the workflow
Create a Cloud Scheduler job that triggers your workflow, using the service account you previously created.
Console
In the Google Cloud console, go to the Cloud Scheduler page:
Click Create Job.
Set the Name to
my-workflow-job.For Region, select us-east1 (South Carolina).
For Frequency, enter:
This will execute the job every 5 minutes. The interval is defined using unix-cron format.*/5 * * * *For Timezone, select a country and timezone.
For example, select United States and Los Angeles or Pacific Daylight Time (PDT).
Click Continue.
For Target type, select HTTP.
For URL, enter:
https://workflowexecutions.googleapis.com/v1/projects/PROJECT_ID/locations/us-east1/workflows/myFirstWorkflow/executions
Leave the HTTP method at the default of POST.
Add the following two HTTP headers:
- Name:
Content-Typeand Value:application/octet-stream - Name:
User-Agentand Value:Google-Cloud-Scheduler
- Name:
For Body, enter:
{"argument": "{\"firstName\":\"Sherlock\", \"lastName\":\"Holmes\"}"}