You can publish a CloudEvents event directly to an Eventarc Advanced bus in a supported format using the Google Cloud CLI or by sending a request to the Eventarc Publishing REST API. You can also use the Eventarc client libraries to access the Eventarc APIs from a supported language.
Before you begin
Before you start, make sure that you have completed the following steps:
- 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.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
Create or select a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Eventarc and Eventarc Publishing APIs:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.gcloud services enable eventarc.googleapis.com
eventarcpublishing.googleapis.com -
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
Create or select a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Eventarc and Eventarc Publishing APIs:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.gcloud services enable eventarc.googleapis.com
eventarcpublishing.googleapis.com
Publish directly
The event message you publish directly must conform to the CloudEvents specification.
gcloud
Open a terminal.
You can publish events to a bus by using the
gcloud eventarc message-buses publishcommand. For example:Or:gcloud eventarc message-buses publish BUS_NAME \ --avro-message=AVRO_MESSAGE
Or:gcloud eventarc message-buses publish BUS_NAME \ --json-message=JSON_MESSAGE
gcloud eventarc message-buses publish BUS_NAME \ --event-data=DATA_PAYLOAD \ --event-id=EVENT_ID \ --event-source=EVENT_SOURCE \ --event-type=EVENT_TYPE \ --event-attributes=EVENT_ATTRIBUTE
Replace the following:
BUS_NAME: the ID or fully qualified identifier of the bus to which to publish the event.
You must use only one of the following:
AVRO_MESSAGE: the event message in an Avro format according to this specification.JSON_MESSAGE: the event message in a JSON format according to this specification.DATA_PAYLOAD: the data of a published event.
If using the
--event-dataflag, you must also use the following:EVENT_ID: the event identifier. Event producers must ensure thatsource+idis unique for each distinct event.EVENT_SOURCE: the event source of a published event.EVENT_TYPE: the type of event related to the originating occurrence.
If using the
--event-dataflag, you can optionally use the following:EVENT_ATTRIBUTE: the attributes of a published event. You can repeat the--event-attributesflag to add more attributes.Note that events might include any number of additional custom CloudEvents attributes with distinct names (also known as extension attributes).
Examples:
gcloud eventarc message-buses publish my-bus \ --event-data='{"key": "hello-world-data"}' \ --event-id=hello-world-id-1234 \ --event-source=hello-world-source \ --event-type=hello-world-type \ --event-attributes="datacontenttype=application/json"
gcloud eventarc message-buses publish my-bus --json-message='{ "specversion" : "1.0", "type" :"com.example.someevent", "source" : "google.cloud.storage.object.v1.finalized", "id" : "A234-1234-1234", "time" : "2024-04-05T17:31:00Z", "bucket" : "bucketName", "datacontenttype" : "application/json", "data":{"key": "value"} }'
REST API
To publish an event to a bus, use the
projects.locations.messageBuses.publish
method.
Before using any of the request data, make the following replacements:
- The full resource name of the bus in the format
projects/PROJECT_ID/locations/LOCATION/messageBuses/BUS_NAMEReplace the following:
PROJECT_ID: the Google Cloud project ID for the bus project.LOCATION: the region in which the bus is deployed—for example,us-central1.BUS_NAME: the name of the bus to which to publish the event.
SPEC_VERSION: the version of the CloudEvents specification which the event uses—for example,1.0.EVENT_TYPE: the type of event related to the originating occurrence.EVENT_SOURCE: the event source of a published event.EVENT_ID: the event identifier. Producers must ensure thatsource+idis unique for each distinct event.CONTENT_TYPE(optional): the content type ofdatavalue. If a JSON format event has nodatacontenttypeattribute, then it is assumed that the data is a JSON value conforming to theapplication/jsonmedia type.DATA_PAYLOAD(optional): the event payload encoded into the media format specified bydatacontenttypeand adhering todataschemawhen those attributes are present.
Request JSON body:
{
"jsonMessage":
"{\"specversion\":\"SPEC_VERSION\",
\"type\":\"EVENT_TYPE\",
\"source\":\"EVENT_SOURCE\",
\"id\":\"EVENT_ID\",
\"datacontenttype\":\"CONTENT_TYPE\",
\"data\":\"DATA_PAYLOAD\"}"
}
To send your request, expand one of these options:
If successful, the server returns an HTTP 200 OK status code and the empty response
body in JSON format:
200 OK
{}