Publish messages to a PubSub topic.

View on GitHub

Last Updated: Jun. 28, 2021

Access Instructions

Install the Google provider package into your Airflow environment.

Import the module into your DAG file and instantiate it with your desired params.


project_idstrOptional, the Google Cloud project ID in which to work (templated). If set to None or missing, the default project_id from the Google Cloud connection is used.
topicRequiredstrthe topic to which to publish. Do not include the full topic path. In other words, instead of projects/{project}/topics/{topic}, provide only {topic}. (templated)
messagesRequiredlista list of messages to be published to the topic. Each message is a dict with one or more of the following keys-value mappings: * ‘data’: a bytestring (utf-8 encoded) * ‘attributes’: {‘key1’: ‘value1’, …} Each message must contain at least a non-empty ‘data’ value or an attribute dict with at least one key (templated). See
gcp_conn_idstrThe connection ID to use connecting to Google Cloud.
delegate_tostrThe account to impersonate using domain-wide delegation of authority, if any. For this to work, the service account making the request must have domain-wide delegation enabled.
projectstr(Deprecated) the Google Cloud project ID where the topic will be created
impersonation_chainUnion[str, Sequence[str]]Optional service account to impersonate using short-term credentials, or chained list of accounts required to get the access_token of the last account in the list, which will be impersonated in the request. If set as a string, the account must grant the originating account the Service Account Token Creator IAM role. If set as a sequence, the identities from the list must grant Service Account Token Creator IAM role to the directly preceding identity, with first account from the list granting this role to the originating account (templated).


Publish messages to a PubSub topic.

See also

For more information on how to use this operator, take a look at the guide: Publishing PubSub messages

Each Task publishes all provided messages to the same topic in a single Google Cloud project. If the topic does not exist, this task will fail.

m1 = {'data': b'Hello, World!',
'attributes': {'type': 'greeting'}
m2 = {'data': b'Knock, knock'}
m3 = {'attributes': {'foo': ''}}
t1 = PubSubPublishOperator(
messages=[m1, m2, m3],

project , topic, and messages are templated so you can use variables in them.

Was this page helpful?