# Publish and receive events by creating a bus and enrollment (console)

<br />

This quickstart shows you how to publish and receive event messages by creating
an Eventarc Advanced bus and enrollment in your Google Cloud
project.

- A *bus* acts as a central router, receiving messages from event sources or
  published by providers.

- An *enrollment* routes messages received by the bus to one or more
  destinations through a processing *pipeline*.

In this quickstart, you:

1. Create an Artifact Registry standard repository.

2. Deploy an event receiver service to Cloud Run.

3. Create an Eventarc Advanced bus.

4. Create an Eventarc Advanced enrollment.

5. Publish an event message to the bus.

6. View the event data in the Cloud Run logs.

You can complete most of the steps in this quickstart using the Google Cloud console.
To complete all of the steps using the Google Cloud CLI, see
[Publish and receive events by creating a bus and enrollment (gcloud CLI)](https://docs.cloud.google.com/eventarc/advanced/docs/quickstarts/publish-events-create-bus.md.txt).

## Before you begin

Security constraints defined by your organization might prevent you from completing the
following steps. For troubleshooting information, see
[Develop applications in a constrained Google Cloud environment](https://docs.cloud.google.com/resource-manager/docs/organization-policy/develop-apps-constrained-environment).

1. A few steps in this quickstart require that you use the gcloud CLI:
   1. Set up the gcloud CLI in one of the following development environments:

      ### Cloud Shell

      To use an online terminal with the gcloud CLI already set up, activate
      Cloud Shell:

      At the bottom of this page, a Cloud Shell session starts and displays a
      command-line prompt. It can take a few seconds for the session to initialize.

      ### Local shell

      To use a local development environment, follow these steps:
      1. [Install the gcloud CLI.](https://docs.cloud.google.com/sdk/docs/install)
      2. [Initialize the gcloud CLI.](https://docs.cloud.google.com/sdk/docs/initialize.md.txt)
   2. Select your Google Cloud project:

      ```
      gcloud config set project PROJECT_ID
      ```

      Replace `PROJECT_ID` with your Google Cloud project
      name.
   3. Update `gcloud` components:

      ```bash
      gcloud components update
      ```
   4. Sign in using your account:

      ```
      gcloud auth login
      ```
   5. Set the configuration variable used in this quickstart:

      ```
      REGION=us-central1
      ```
2. If you are the project creator, you are granted the
   [basic Owner role](https://docs.cloud.google.com/iam/docs/roles-overview.md.txt#basic)
   (`roles/owner`). By default, this Identity and Access Management (IAM) role
   includes the permissions necessary for full access to most Google Cloud
   resources and you can skip this step.

   If you are not the project creator, required permissions must be granted on
   the project to the appropriate *principal*. For example, a principal can be
   a Google Account (for end users) or a service account (for applications and
   compute workloads).

   Note that by default,
   [Cloud Build
   permissions include permissions to upload and download Artifact Registry
   artifacts](https://docs.cloud.google.com/artifact-registry/docs/access-control#gcp).

   #### Required permissions


   To get the permissions that
   you need to complete this quickstart,

   ask your administrator to grant you the
   following IAM roles on your project:

   - [Cloud Build Editor](https://docs.cloud.google.com/iam/docs/roles-permissions/cloudbuild.md.txt#cloudbuild.builds.editor) (`roles/cloudbuild.builds.editor`)
   - [Cloud Run Admin](https://docs.cloud.google.com/iam/docs/roles-permissions/run.md.txt#run.admin) (`roles/run.admin`)
   - [Eventarc Developer](https://docs.cloud.google.com/iam/docs/roles-permissions/eventarc.md.txt#eventarc.developer) (`roles/eventarc.developer`)
   - [Eventarc Message Bus Admin](https://docs.cloud.google.com/iam/docs/roles-permissions/eventarc.md.txt#eventarc.messageBusAdmin) (`roles/eventarc.messageBusAdmin`)
   - [Logs View Accessor](https://docs.cloud.google.com/iam/docs/roles-permissions/logging.md.txt#logging.viewAccessor) (`roles/logging.viewAccessor`)
   - [Project IAM Admin](https://docs.cloud.google.com/iam/docs/roles-permissions/resourcemanager.md.txt#resourcemanager.projectIamAdmin) (`roles/resourcemanager.projectIamAdmin`)
   - [Service Account Admin](https://docs.cloud.google.com/iam/docs/roles-permissions/iam.md.txt#iam.serviceAccountAdmin) (`roles/iam.serviceAccountAdmin`)
   - [Service Account User](https://docs.cloud.google.com/iam/docs/roles-permissions/iam.md.txt#iam.serviceAccountUser) (`roles/iam.serviceAccountUser`)
   - [Service Usage Admin](https://docs.cloud.google.com/iam/docs/roles-permissions/serviceusage.md.txt#serviceusage.serviceUsageAdmin) (`roles/serviceusage.serviceUsageAdmin`)


   For more information about granting roles, see [Manage access to projects, folders, and organizations](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt).


   You might also be able to get
   the required permissions through [custom
   roles](https://docs.cloud.google.com/iam/docs/creating-custom-roles) or other [predefined
   roles](https://docs.cloud.google.com/iam/docs/roles-overview#predefined).
3. For testing purposes, create a service account, and grant it the roles necessary to complete this quickstart. Use the service account to build and deploy your container image, and attach it to an Eventarc Advanced pipeline to represent the identity of the pipeline.
   1. In the Google Cloud console, go to the **Service accounts** page.  

      [Go to Service accounts](https://console.cloud.google.com/projectselector2/iam-admin/serviceaccounts)
   2. Click **Create service account**.
   3. Enter a **Service account name**.
   4. Click **Create and continue**.
   5. In the **Select a role** list, filter for and select the following roles:
      - [Artifact Registry
        Writer](https://docs.cloud.google.com/iam/docs/roles-permissions/artifactregistry#artifactregistry.writer): to upload Artifact Registry artifacts
      - [Logs Writer](https://docs.cloud.google.com/iam/docs/roles-permissions/logging.md.txt#logging.logWriter): to write logs to Cloud Logging
      - [Storage
        Admin](https://docs.cloud.google.com/iam/docs/roles-permissions/storage#storage.admin): to access Cloud Storage objects
      - [Cloud Run
        Invoker](https://docs.cloud.google.com/iam/docs/roles-permissions/run#run.invoker): to call Cloud Run
   6. Click **Done**.

   Note that you can configure who can access your Cloud Run
   service in either of the following ways:
   - Grant permission to select service accounts or groups to allow access to the service. All requests must have an HTTP Authorization header containing an OpenID Connect token signed by Google for one of the authorized service accounts. This is the way that access is configured in this quickstart.
   - Grant permission to [`allUsers`](https://docs.cloud.google.com/iam/docs/principals-overview.md.txt#all-users) to allow public access.

   For more information, see
   [Access control for Cloud Run](https://docs.cloud.google.com/run/docs/securing/security.md.txt#access-control).

<br />

## Create an Artifact Registry standard repository

Create an
[Artifact Registry standard repository](https://docs.cloud.google.com/artifact-registry/docs/repositories/create-repos.md.txt)
to store your container image.

1. In the Google Cloud console, go to the **Repositories** page.


   [Go to Repositories](https://console.cloud.google.com/artifacts)

   <br />

2. Click
   **Create repository**.

3. Enter a **Name** ---for example, `my-repo`.

   For each repository location in a project, repository names must be unique.
4. For the repository **Format** , select **Docker**.

5. In the **Region** list, select **us-central1 (Iowa)**.

6. Accept all the other defaults.

7. Click **Create**.

## Deploy an event receiver service to Cloud Run

Deploy a [Cloud Run service](https://docs.cloud.google.com/run/docs/overview/what-is-cloud-run.md.txt)
that logs the contents of an event. Other event destinations are supported such
as a Pub/Sub topic, Workflows, or an HTTP endpoint. For more
information, see
[Event providers and destinations](https://docs.cloud.google.com/eventarc/advanced/docs/event-providers-targets.md.txt).

1. In your terminal, clone the GitHub repository:

   ```bash
   git clone https://github.com/GoogleCloudPlatform/eventarc-samples.git
   ```
2. Change to the directory that contains the Cloud Run sample
   code:

   ```bash
   cd eventarc-samples/eventarc-advanced-quickstart/
   ```
3. Build a Docker container image and push the image to your repository:

   ```bash
   gcloud builds submit \
       --tag $REGION-docker.pkg.dev/PROJECT_ID/REPOSITORY/log-events:v1 \
       --service-account=projects/PROJECT_ID/serviceAccounts/SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com \
       --default-buckets-behavior=regional-user-owned-bucket
   ```

   Replace the following:
   - `REPOSITORY`: the name of your Artifact Registry repository.
   - `SERVICE_ACCOUNT_NAME`: the name of the service account you created previously.
4. Deploy the container image to Cloud Run:

   1. In the Google Cloud console, go to the **Cloud Run** page.


      [Go to Cloud Run](https://console.cloud.google.com/run)

      <br />

   2. Click
      **Deploy container** \> **Service**.

   3. Select **Deploy one revision from an existing container image**.

   4. For the **Container image URL** , click **Select** to specify the
      Artifact Registry container image you previously created.

   5. Optionally, you can change the **Service name** ---for example, to `my-service`.

   6. In the **Region** list, select **us-central1 (Iowa)**.

   7. For **Authentication** , select **Require authentication**.

      This configures the service to only allow authenticated invocations.
   8. For **Ingress** , select **All**.

      This allows all requests, including requests directly from the internet
      to the `run.app` URL. For more information, see
      [Restrict network ingress for Cloud Run](https://docs.cloud.google.com/run/docs/securing/ingress.md.txt).
   9. Accept all the other defaults.

   10. Click **Create** and wait for the deployment to finish.

## Create an Eventarc Advanced bus

A bus receives event messages from a message source or published by a provider
and acts as a message router.

For more information, see
[Create a bus to route messages](https://docs.cloud.google.com/eventarc/advanced/docs/publish-events/create-bus.md.txt).

> [!NOTE]
> **Note:** You can create one bus per Google Cloud project per [supported region](https://docs.cloud.google.com/eventarc/docs/locations.md.txt#advanced-regions).

1. In the Google Cloud console, go to the **Eventarc** \>
   **Bus** page.


   [Go to Bus](https://console.cloud.google.com/eventarc/buses)

   <br />

2. Click
   **Create bus**.

3. On the **Create a bus** page, do the following:

   1. Enter a **Bus name** ---for example, `my-bus`.
   2. In the **Region** list, select **us-central1 (Iowa)**.
4. Accept all the other defaults.

5. Click **Create**.

## Create an Eventarc Advanced enrollment

An enrollment determines which messages are routed to a destination and it also
specifies the pipeline that is used to configure a destination for the event
messages.

For more information, see
[Create an enrollment to receive events](https://docs.cloud.google.com/eventarc/advanced/docs/receive-events/create-enrollment.md.txt).

When using the Google Cloud console, you can create an enrollment and a
pipeline at the same time.

> [!NOTE]
> **Note:** Creating an enrollment and pipeline might take more than 10 minutes.

1. To create an enrollment, in the Google Cloud console, go to the
   **Eventarc** \> **Pipelines** page.


   [Go to Pipelines](https://console.cloud.google.com/eventarc/pipelines)

   <br />

2. Click
   **Create pipeline**.

3. In the **Pipeline details** pane, do the following:

   1. Enter a **Pipeline name** ---for example, `my-pipeline`.
   2. In the **Region** list, select **us-central1 (Iowa)**. The pipeline must be created in the same region as the bus.
   3. Accept all the other defaults.
   4. Click **Continue**.
4. In the **Enrollments** pane, do the following:

   1. Click **Add an enrollment**.
   2. Enter an **Enrollment name** ---for example, `my-enrollment`.
   3. In the **Eventarc Advanced Bus** list, select the bus you previously created.
   4. In the **CEL expression** field, write an evaluation expression
      [using CEL](https://docs.cloud.google.com/eventarc/advanced/docs/receive-events/use-cel.md.txt). For example:

      ```bash
      message.type == "hello-world-type"
      ```
   5. Click **Done**.

   6. Click **Continue**.

   7. In the **Event mediation** pane, click **Continue** again.

5. In the **Destination** pane, do the following:

   1. In the **Destination type** list, select **Cloud Run service
      (via HTTP)** and then select the Cloud Run service you previously
      created.

   2. Select the **Enable authentication** checkbox.

      1. In the **Auth header** list, select **OIDC token**.

      2. In the **Service account** list, select the service account that you
         previously created and that will invoke your destination service. This
         service account email is used to generate the
         [OIDC token](https://developers.google.com/identity/protocols/OpenIDConnect).

6. Click **Create**.

## Publish an event message to the bus

To directly publish a message to your bus, you can use the
[`gcloud eventarc message-buses publish`](https://docs.cloud.google.com/sdk/gcloud/reference/eventarc/message-buses/publish)
command or send a request to the Eventarc Publishing REST API. For more
information, see
[Publish events directly](https://docs.cloud.google.com/eventarc/advanced/docs/publish-events/publish-events-direct-format.md.txt).

The message must be in a CloudEvents format which is a specification for
describing event data in a common way. The `data` element is the payload of your
event. Any well-formed JSON can go in this field. For more information about
CloudEvents context attributes, see
[Event format](https://docs.cloud.google.com/eventarc/advanced/docs/event-format.md.txt).

Publish an event to your Eventarc Advanced bus using the
gcloud CLI and an `--event-data` and other event attribute
flags:

```bash
gcloud eventarc message-buses publish BUS_NAME \
    --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" \
    --location=$REGION
```

Or, publish an event to your Eventarc Advanced bus as a JSON
message using the gcloud CLI and a `--json-message` flag:

```bash
gcloud eventarc message-buses publish BUS_NAME \
    --location=$REGION \
    --json-message='{"id": "hello-world-id-1234", "type":
 "hello-world-type", "source":
 "hello-world-source", "specversion": "1.0", "data":
 {"key": "hello-world-data"}}'
```

After publishing an event, you should receive an "Event published successfully"
message.

## View the event data in the Cloud Run logs

After publishing an event to your Eventarc Advanced bus, you can
check the logs of your Cloud Run service to verify that the event
was received as expected.

1. In the Google Cloud console, go to the **Cloud Run** page.


   [Go to Cloud Run](https://console.cloud.google.com/run)

   <br />

2. On the **Services** page, click the name of your service.

3. Click the **Logs** tab.

4. You can filter the log entries and return the output. For example, you can
   search for `hello-world-data`.

5. Look for a log entry similar to the following:

   ```
   2025-02-27 08:36:44.350 EST [2025-02-27 13:36:44,352] INFO in server: Body: b'{"key": "hello-world-data"}'
   ```

   > [!NOTE]
   > **Note:** If you don't see a log entry, wait a few minutes and retry the search.

You have successfully created an Eventarc Advanced bus and
enrollment, published an event message to the bus, and verified the expected
outcome in the logs of the event receiver service.

## Clean up

When you finish the tasks that are described in this quickstart, you can avoid
continued billing by deleting the resources that you created:

1. [Delete an Artifact Registry repository](https://docs.cloud.google.com/artifact-registry/docs/repositories/delete-repos.md.txt).

2. [Delete a Cloud Run service](https://docs.cloud.google.com/run/docs/managing/services.md.txt#delete).

3. Delete Eventarc Advanced resources:

   1. [Delete an enrollment](https://docs.cloud.google.com/eventarc/advanced/docs/receive-events/create-enrollment.md.txt#delete-enrollment).

   2. [Delete a pipeline](https://docs.cloud.google.com/eventarc/advanced/docs/receive-events/create-enrollment.md.txt#delete-pipeline).

   3. [Delete a bus](https://docs.cloud.google.com/eventarc/advanced/docs/publish-events/create-bus.md.txt#delete-bus).

Alternatively, you can delete your Google Cloud project to avoid incurring charges.
Deleting your Google Cloud project stops billing for all the resources used
within that project.

> [!CAUTION]
> **Caution** : Deleting a project has the following effects:
>
> - **Everything in the project is deleted.** If you used an existing project for the tasks in this document, when you delete it, you also delete any other work you've done in the project.
> - **Custom project IDs are lost.** When you created this project, you might have created a custom project ID that you want to use in the future. To preserve the URLs that use the project ID, such as an `appspot.com` URL, delete selected resources inside the project instead of deleting the whole project.
>
>
> If you plan to explore multiple architectures, tutorials, or quickstarts, reusing projects
> can help you avoid exceeding project quota limits.

1. In the Google Cloud console, go to the **Manage resources** page.

   [Go to Manage resources](https://console.cloud.google.com/iam-admin/projects)
2. In the project list, select the project that you want to delete, and then click **Delete**.
3. In the dialog, type the project ID, and then click **Shut down** to delete the project.

## What's next

- [Publish events from Google sources](https://docs.cloud.google.com/eventarc/advanced/docs/publish-events/publish-events-google-sources.md.txt)
- [Publish events directly](https://docs.cloud.google.com/eventarc/advanced/docs/publish-events/publish-events-direct-format.md.txt)
- [Transform received events](https://docs.cloud.google.com/eventarc/advanced/docs/receive-events/transform-events.md.txt)
- [Format received events](https://docs.cloud.google.com/eventarc/advanced/docs/receive-events/configure-format-events.md.txt)