This document explains how to create a Pub/Sub topic with Single Message Transforms (SMTs).
Topic SMTs allow for lightweight modifications to message data and attributes directly within Pub/Sub. This feature enables data cleaning, filtering, or format conversion before the messages are published to the topic.
To create a topic with SMTs, you can use the Google Cloud console, the Google Cloud CLI, the client library, or the Pub/Sub API.
Before you begin
-
Learn about the Pub/Sub service and its terminology .
-
Learn about SMTs .
Required roles and permissions
To get the permissions that
you need to create a topic with SMTs,
ask your administrator to grant you the Pub/Sub Editor
( roles/pubsub.editor
)
IAM role on your project.
For more information about granting roles, see Manage access to projects, folders, and organizations
.
This predefined role contains the permissions required to create a topic with SMTs. To see the exact permissions that are required, expand the Required permissionssection:
Required permissions
The following permissions are required to create a topic with SMTs:
- Grant the create a topic permission on the project:
pubsub.topics.create
You might also be able to get these permissions with custom roles or other predefined roles .
You can configure access control at the project level and at the individual resource level.
Create a topic with SMTs
Before you create a topic with SMTs, review the documentation for Properties of a topic .
To create a Pub/Sub with one or more SMTs, perform the following steps. You can enable up to 5 SMTs per topic.
Console
-
In the Google Cloud console, go to the Pub/Sub Topicspage.
-
Click Create topic.
-
In the Topic IDfield, enter an ID for your topic. For more information about naming topics, see the naming guidelines .
-
Under Transforms, click Add a transform.
-
Select the Transform type. For more information about the supported SMT types, see Types of SMTs .
-
Set the configuration properties for the SMT. The set of properties depends on the type of SMT. For more information, see the documentation for that SMT type.
-
Optional. To validate the SMT, click Validate. If the SMT is valid, the message
"Validation passed"is displayed. Otherwise, an error message is displayed. -
To add another transform, click Add a transformand repeat the previous steps.
To arrange the SMTs in a specific order, click Move upor Move down. To remove an SMT, click Delete.
-
Optional. To test an SMT on a sample message, perform the following steps:
-
Click Test transforms.
-
In the Test transformwindow, select the function that you want to test.
-
In the Input messagewindow, enter a sample message.
-
To add an attribute to the message, click Add an attributeand enter the attribute's key and value. You can add multiple attributes.
-
Click Test. The result of applying the SMT on the message is displayed under Output message.
-
To close the Test transformswindow, click Close.
If you create more than one SMT, you can test the entire sequence of transforms as follows:
- Test the first SMT in the sequence, as described in the previous steps.
- Select the next SMT. The input message is pre-populated with the output message from the previous test.
- Continue testing the SMTs in order, to make sure the entire sequence works as expected.
-
-
To create the topic, click Create.
gcloud
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
-
Create a YAML or JSON file that defines one or more SMTs. The YAML or JSON definition depends on the type of SMT. For more information, see Types of SMTs .
If the file includes more than one SMT, Pub/Sub executes them in the order listed.
-
Optional. To validate an SMT, run the
gcloud pubsub message-transforms validatecommand:gcloud pubsub message-transforms validate \ --message-transform-file = TRANSFORM_FILEReplace the following:
- TRANSFORM_FILE : The path to a YAML or JSON file that defines a single SMT. If you are creating multiple SMTs, you must validate them individually.
-
Optional. To test one or more SMTs on a sample Pub/Sub message, run the
gcloud pubsub message-transforms testcommand:gcloud pubsub message-transforms test \ --message-transforms-file = TRANSFORMS_FILE \ --message = MESSAGE \ --attribute = ATTRIBUTESReplace the following:
- TRANSFORMS_FILE : The path to a YAML or JSON file that defines one or more SMTs.
- MESSAGE : The body of the sample message.
- ATTRIBUTES
: Optional. A comma-separated list of
message attributes. Each attribute is a key-value pair formatted as
KEY =" VALUE ".
The command executes the SMTs in order, using the output from each SMT as the input to the next. The command outputs the results of each step.
-
To create the topic, run the
gcloud pubsub topics createcommand:gcloud pubsub topics create TOPIC_ID \ --message-transforms-file = TRANSFORMS_FILEReplace the following:
- TOPIC_ID : The ID or name of the topic you want to create. For guidelines on how to name a topic, see Resource names . The name of a topic is immutable.
- TRANSFORMS_FILE : The path to a YAML or JSON file that defines one or more SMTs.
Java
Before trying this sample, follow the Java setup instructions in Quickstart: Using Client Libraries . For more information, see the Pub/Sub Java API reference documentation .
Python
Before trying this sample, follow the Python setup instructions in Quickstart: Using Client Libraries . For more information, see the Pub/Sub Python API reference documentation .
Go
The following sample uses the major version of the Go Pub/Sub client library (v2). If you are still using the v1 library, see the migration guide to v2 . To see a list of v1 code samples, see the deprecated code samples .
Before trying this sample, follow the Go setup instructions in Quickstart: Using Client Libraries . For more information, see the Pub/Sub Go API reference documentation .

