Publish events directly

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:

  1. 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.
  2. Install the Google Cloud CLI.

  3. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  4. To initialize the gcloud CLI, run the following command:

    gcloud init
  5. 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 the resourcemanager.projects.create permission. Learn how to grant roles.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with 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_ID with your Google Cloud project name.

  6. Verify that billing is enabled for your Google Cloud project.

  7. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable eventarc.googleapis.com eventarcpublishing.googleapis.com
  8. Install the Google Cloud CLI.

  9. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  10. To initialize the gcloud CLI, run the following command:

    gcloud init
  11. 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 the resourcemanager.projects.create permission. Learn how to grant roles.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with 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_ID with your Google Cloud project name.

  12. Verify that billing is enabled for your Google Cloud project.

  13. 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 the serviceusage.services.enable permission. 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

  1. Open a terminal.

  2. You can publish events to a bus by using the gcloud eventarc message-buses publish command. For example:

    gcloud eventarc message-buses publish BUS_NAME \
        --avro-message=AVRO_MESSAGE
    Or:
    gcloud eventarc message-buses publish BUS_NAME \
        --json-message=JSON_MESSAGE
    Or:
    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-data flag, you must also use the following:

    • EVENT_ID: the event identifier. Event producers must ensure that source + id is 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-data flag, you can optionally use the following:

    • EVENT_ATTRIBUTE: the attributes of a published event. You can repeat the --event-attributes flag 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_NAME

    Replace 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 that source + id is unique for each distinct event.
  • CONTENT_TYPE (optional): the content type of data value. If a JSON format event has no datacontenttype attribute, then it is assumed that the data is a JSON value conforming to the application/json media type.
  • DATA_PAYLOAD (optional): the event payload encoded into the media format specified by datacontenttype and adhering to dataschema when 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

{}

What's next