Trigger Workflows using Pub/Sub messages (gcloud CLI)

This quickstart shows you how to execute a workflow using an Eventarc trigger that receives events using Pub/Sub. The trigger executes the workflow by passing events delivered through Pub/Sub as runtime arguments to a destination workflow.

In this quickstart, you will:

  1. Use Workflows to create and deploy a workflow that decodes and returns Pub/Sub messages.
  2. Create an Eventarc trigger that connects a Pub/Sub topic to a Workflows event receiver.
  3. Publish a message to the Pub/Sub topic to generate an event. This event is passed as a runtime argument to the destination workflow.
  4. View the Pub/Sub message as a result of the workflow execution.

Before you begin

Make sure that you have access to this feature by requesting to have your project added to private previews for Google Cloud Workflows.

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.

  6. Open a terminal or Cloud Shell.
  7. Update gcloud components:
    gcloud components update
  8. Enable the Eventarc, Pub/Sub, and Eventarc APIs.

    gcloud services enable eventarc.googleapis.com pubsub.googleapis.com workflows.googleapis.com workflowexecutions.googleapis.com
  9. Set the configuration variables used in this tutorial:
    export WORKFLOW_LOCATION=us-central1
    export TRIGGER_LOCATION=us-central1
    export PROJECT_ID=PROJECT_ID
    gcloud config set project ${PROJECT_ID}
    gcloud config set workflows/location ${WORKFLOW_LOCATION}
    gcloud config set eventarc/location ${TRIGGER_LOCATION}
    
    Replace PROJECT_ID with your Google Cloud project ID.
  10. Create a service account and give it a name; for example, my-service-account.
    export MY_SERVICE_ACCOUNT=my-service-account
    gcloud iam service-accounts create ${MY_SERVICE_ACCOUNT}
  11. Grant the roles/workflows.invoker role to the service account:

    gcloud projects add-iam-policy-binding ${PROJECT_ID} \
      --member="serviceAccount:${MY_SERVICE_ACCOUNT}@${PROJECT_ID}.iam.gserviceaccount.com" \
      --role="roles/workflows.invoker"

  12. Grant the roles/iam.serviceAccountTokenCreator role to the user:

    gcloud projects add-iam-policy-binding ${PROJECT_ID} \
      --member="PRINCIPAL" \
      --role='roles/iam.serviceAccountTokenCreator'

    Replace PRINCIPAL with a valid identity to which you want to grant the role. For example:

    • Google account email: user:test-user@gmail.com
    • Google group: group:admins@example.com
    • Service account: serviceAccount:test123@example.domain.com
    • Google Workspace or Cloud Identity domain: domain:example.domain.com

Create and deploy a workflow

Create and deploy a workflow that gets executed when a message published to a Pub/Sub topic triggers a workflow with an HTTP request.
  1. Open a terminal or Cloud Shell.
  2. In your home directory, create a new file called myFirstWorkflow.yaml or myFirstWorkflow.json.
  3. Copy and paste the following into the new file and save it:

    YAML

    main:
      params: [event]
      steps:
        - decode_pubsub_message:
            assign:
               - base64: ${base64.decode(event.data.message.data)}
               - message: ${text.decode(base64)}
        - return_pubsub_message:
            return: ${message}
          

    JSON

    {
      "main": {
        "params": [
          "event"
        ],
        "steps": [
          {
            "decode_pubsub_message": {
              "assign": [
                {
                  "base64": "${base64.decode(event.data.message.data)}"
                },
                {
                  "message": "${text.decode(base64)}"
                }
              ]
            }
          },
          {
            "return_pubsub_message": {
              "return": "${message}"
            }
          }
        ]
      }
    }
  4. Deploy the workflow:
    export MY_WORKFLOW=myFirstWorkflow
    gcloud workflows deploy ${MY_WORKFLOW} --source=myFirstWorkflow.yaml
    
    Replace .yaml with .json if you copied the JSON version of the example workflow.

Create an Eventarc trigger

When a message is published to the Pub/Sub topic, the event triggers the workflow.
  1. Create a trigger to listen for Pub/Sub messages:

    New Pub/Sub topic

    gcloud eventarc triggers create events-pubsub-trigger \
      --destination-workflow=${MY_WORKFLOW} \
      --destination-workflow-location=${WORKFLOW_LOCATION} \
      --event-filters="type=google.cloud.pubsub.topic.v1.messagePublished" \
      --service-account="${MY_SERVICE_ACCOUNT}@${PROJECT_ID}.iam.gserviceaccount.com"

    This creates a new Pub/Sub topic and a trigger for it called events-pubsub-trigger.

    Existing Pub/Sub topic

    gcloud eventarc triggers create events-pubsub-trigger \
      --destination-workflow=${MY_WORKFLOW} \
      --destination-workflow-location=${WORKFLOW_LOCATION} \
      --event-filters="type=google.cloud.pubsub.topic.v1.messagePublished" \
      --service-account="${MY_SERVICE_ACCOUNT}@${PROJECT_ID}.iam.gserviceaccount.com" \
      --transport-topic=TOPIC_ID

    Replace TOPIC_ID with the ID of the existing Pub/Sub topic.

    This creates a trigger called events-pubsub-trigger for an existing Pub/Sub topic.

  2. Confirm the trigger was successfully created:
    gcloud eventarc triggers describe events-pubsub-trigger --location=${TRIGGER_LOCATION}

Generate and view an event

Publish a message to the Pub/Sub topic to generate an event and trigger the workflow. The generated event is passed as a runtime argument to the workflow which returns the Pub/Sub message as a result of the workflow execution. Make sure that the size of the events you pass to the workflow does not exceed 256 kilobytes.

  1. If you created a trigger for a new Pub/Sub topic, find and set the Pub/Sub topic created, as an environment variable:

    export TOPIC_ID=$(basename $(gcloud eventarc triggers describe events-pubsub-trigger --format='value(transport.pubsub.topic)'))
    
  2. To trigger the workflow, send a message to the Pub/Sub topic:

    gcloud pubsub topics publish $TOPIC_ID --message "Hello there"
    

    The generated event is passed as a runtime argument to the workflow which returns a "Hello there" message.

  3. To verify that a workflows execution was triggered, list the last five executions:

    gcloud workflows executions list ${MY_WORKFLOW} --limit=5
    

    The output should be similar to the following, listing a NAME and STATE equal to SUCCEEDED for each workflow execution.

    NAME: projects/606789101455/locations/us-central1/workflows/myFirstWorkflow/executions/8c02b8f1-8836-4a6d-99d9-fc321eb9668f
    STATE: SUCCEEDED
    START_TIME: 2021-09-13T19:15:10.275677049Z
    END_TIME: 2021-09-13T19:15:10.963136883Z
    NAME: projects/606789101455/locations/us-central1/workflows/myFirstWorkflow/executions/a6319d9d-36a6-4117-904e-3d1118bdc90a
    STATE: SUCCEEDED
    START_TIME: 2021-09-13T17:28:51.492864252Z
    END_TIME: 2021-09-13T17:28:52.227212414Z
    
    Note that in the NAME field in the preceding example, a6319d9d-36a6-4117-904e-3d1118bdc90a is the ID of the workflow execution. Copy your execution ID to use in the next step.

  4. To view the event message:

    1. To view the execution status:

      gcloud workflows executions describe WORKFLOW_EXECUTION_NAME
      Replace WORKFLOW_EXECUTION_NAME with the name of the workflow execution that corresponds to the publishing time of the Pub/Sub topic. The output is similar to the following:
      argument: {"data":{"message":{"data":"aGVsbG8gd29ybGQ=","messageId":"1234567","publishTime":"2022-01-24T17:42:49.133Z"},"subscription":"projects/my-project/subscriptions/my-subscription"},"datacontenttype":"application/json","id":"1234567","source":"//pubsub.googleapis.com/projects/my-project/topics/my-topic","specversion":"1.0","time":"2022-01-24T17:42:49.133Z","type":"google.cloud.pubsub.topic.v1.messagePublished"}
      endTime: '2021-09-13T17:28:47.301012152Z'
      name: projects/1234567/locations/us-central1/workflows/myFirstWorkflow/executions/f72bc6d4-5ea0-4dfb-bb14-2dae82303120
      result: 'Hello there'
      startTime: '2021-09-13T17:28:51.492864252Z'
      state: SUCCEEDED
      workflowVersionId: '1'
      Verify that the "publish_time": "2021-09-13T17:28" at which the Pub/Sub message was published and the startTime of the workflow execution correspond to each other.

    2. Look for the result: "Hello there!" event message.

Congratulations, you have successfully generated an event using a Pub/Sub topic that has triggered a Workflows event receiver using Eventarc.

Clean up

  1. Delete the workflow you created:
    gcloud workflows delete ${MY_WORKFLOW}

    When asked if you want to continue, enter y.

  2. Delete the trigger created in this tutorial:
    gcloud eventarc triggers delete events-pubsub-trigger
    

What's next