Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Eventarc (Pub/Sub) and Workflows

Note: Eventarc Workflows destination is currently a feature in preview. Only allow-listed projects can currently take advantage of it. Please fill out this form to get your project allow-listed before attempting this sample.

In this sample, you will see how to connect Eventarc events to Workflows directly.

More specifically, you will create an Eventarc Pub/Sub trigger to listen for messages to a Pub/Sub topic and pass them onto a workflow.

Deploy a workflow

First, create a workflow.yaml. It logs the received CloudEvent and decodes the Pub/Sub message inside.

main:
    params: [event]
    steps:
        - log_event:
            call: sys.log
            args:
                text: ${event}
                severity: INFO
        - decode_pubsub_message:
            assign:
            - base64: ${base64.decode(event.data.data)}
            - message: ${text.decode(base64)}
        - return_pubsub_message:
                return: ${message}

Deploy the workflow:

WORKFLOW_NAME=eventarc-pubsub-workflow

gcloud workflows deploy $WORKFLOW_NAME --source=workflow.yaml

Create a service account

Create a service account for Eventarc trigger to use to invoke Workflows.

PROJECT_ID=$(gcloud config get-value project)
SERVICE_ACCOUNT=eventarc-pubsub-workflow-sa

gcloud iam service-accounts create $SERVICE_ACCOUNT

Assign the service account Workflows Invoker role:

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

Create an Eventarc Pub/Sub trigger

Connect a Pub/Sub topic to the workflow by creating an Eventarc Pub/Sub trigger:

TRIGGER_NAME=trigger-pubsub-workflow

gcloud eventarc triggers create $TRIGGER_NAME \
  --location=us-central1 \
  --destination-workflow=$WORKFLOW_NAME \
  --destination-workflow-location=us-central1 \
  --event-filters="type=google.cloud.pubsub.topic.v1.messagePublished" \
  --service-account=$SERVICE_ACCOUNT@$PROJECT_ID.iam.gserviceaccount.com

Find out the Pub/Sub topic that Eventarc created:

TOPIC=$(basename $(gcloud eventarc triggers describe $TRIGGER_NAME --format='value(transport.pubsub.topic)'))

Trigger the workflow

Send a message to the Pub/Sub topic to trigger the workflow:

gcloud pubsub topics publish $TOPIC --message="Hello Workflows"

In the logs, you should see that the workflow received the Pub/Sub message, decoded it and returned as output.