Search code examples
google-cloud-platformgoogle-cloud-functionsgoogle-cloud-pubsub

GCP Workflow Publishing Pub/Sub Message


I have a GCP Workflow in which I need to publish a pub/sub message that contains data AND some attributes. These are three steps that initialize some parameters, create a map for the attributes, then publish the message:

- initializeParameters:
    assign:
        - projectId: ${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")}
        - outputFileName: '${input.outputFileName}'
        - pubsubMessage:
              filename: ${outputFileName}
        - base64Msg: '${base64.encode(json.encode(pubsubMessage))}'
    next: createNotifyMap
- createNotifyMap:
    assign:
        - notifyMap:
            firstAttribute: "value"
            secondAttribute: "value"
    next: notifyBC
- notifyBC:
    call: googleapis.pubsub.v1.projects.topics.publish
    args:
        topic: ${"projects/" + projectId + "/topics/topicname"}
        body:
            messages:
                - data: '${base64Msg}'
                - attributes: ${notifyMap}
    next: successfulRun

The workflow runs successfully, but publishes two messages, one with the data and one with the attributes. Looking at the documentation, I don't see any examples that send both. The idea would be to have it publish a single message with both the attributes and the data.


Solution

  • Each dash under the messages node is a new message. To publish as a single message, put each attribute under a single dash:

    messages:
        - data: '${base64Msg}'
          attributes: ${notifyMap}