Publish and receive events by creating a bus and enrollment (Terraform)
This quickstart shows you how to use Terraform to create an Eventarc Advanced bus and enrollment in your Google Cloud project so that you can publish and receive event messages.
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:
Deploy an event receiver service to Cloud Run.
Create an Eventarc Advanced bus.
Enable events from Google sources.
Create an Eventarc Advanced pipeline and enrollment.
Publish an event message to the bus by creating a workflow.
View the event data in the Cloud Run logs.
You can complete most of the steps in this quickstart using Terraform. To complete all of the steps using the Google Cloud CLI, see Publish events from a Google source.
For more information about using Terraform, see the Terraform on Google Cloud documentation.
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.
- 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.
-
In the Google Cloud console, on the project selector page, select or create 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.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Resource Manager and Identity and Access Management (IAM) 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. -
In the Google Cloud console, on the project selector page, select or create 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.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Resource Manager and Identity and Access Management (IAM) 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. -
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.
Terraform is integrated into the Cloud Shell environment and you can use Cloud Shell to deploy your Terraform resources without having to install Terraform.
Required roles
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 Run Admin (
roles/run.admin) -
Eventarc Developer (
roles/eventarc.developer) -
Eventarc Message Bus Admin (
roles/eventarc.messageBusAdmin) -
Logs View Accessor (
roles/logging.viewAccessor) -
Project IAM Admin (
roles/resourcemanager.projectIamAdmin) -
Service Account Admin (
roles/iam.serviceAccountAdmin) -
Service Account User (
roles/iam.serviceAccountUser) -
Workflows Editor (
roles/workflows.editor)
For more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
Prepare to deploy Terraform
Before deploying any Terraform resources, you must create a Terraform configuration file. A Terraform configuration file lets you define your preferred end state for your infrastructure using the Terraform syntax.
In Cloud Shell, set the default Google Cloud project where you want to apply your Terraform configuration. You need to run this command only once per project, and you can run it in any directory:
export GOOGLE_CLOUD_PROJECT=PROJECT_ID
Replace
PROJECT_IDwith the ID of your Google Cloud project.Note that environment variables are overridden if you set explicit values in the Terraform configuration file.
Each Terraform configuration file must have its own directory (also called a root module). In Cloud Shell, create a directory and a new file within that directory:
mkdir DIRECTORY && cd DIRECTORY && touch main.tf
Replace
DIRECTORYwith the name of your Terraform directory.The filename must have the
.tfextension—for example, in this quickstart, the configuration file ismain.tf.
Define your Terraform configuration
Copy the following Terraform code snippets into your main.tf file. Or, to copy
the entire code sample from GitHub, in the top right corner of a code snippet,
click
>
View on GitHub.
Enable APIs
Use the
google_project_service
Terraform resource to enable the APIs required to apply the Terraform
configuration:
Create a service account
For testing purposes, create a dedicated service account, and grant it specific IAM roles.
Use the
google_service_account
and google_project_iam_member
Terraform resources to create the service account and grant it the necessary
roles to publish and receive events:
Create an event destination
Use the
google_cloud_run_v2_service
Terraform resource to create a
Cloud Run service
as an event destination that logs the contents of an event:
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.
Use the
google_eventarc_message_bus
Terraform resource to create an Eventarc Advanced bus:
Enable events from Google sources
To publish events from Google sources, you must create a
GoogleApiSource
resource that represents a subscription to Google API events for a particular
Eventarc Advanced bus. For more information, see
Publish events from Google sources.
Use the
google_eventarc_google_api_source
Terraform resource to enable events from Google sources:
All supported Google event types sent directly from a Google source are now collected and published to your bus.
Create an Eventarc Advanced pipeline
A pipeline lets you configure a target destination and also provides the option of transforming any matched events prior to delivering them to the destination.
Use the
google_eventarc_pipeline
Terraform resource to create a pipeline:
The event destination is the fully qualified URL of your Cloud Run
service—for example,
https://SERVICE_NAME-abcdef-uc.a.run.app. The service
account email address is used to generate an
OIDC token.
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.
Use the
google_eventarc_enrollment
Terraform resource to create an enrollment:
The matching expression for the enrollment uses Common Expression Language (CEL) to publish event messages whenever a workflow is created. In a subsequent step, you will create a workflow.
Apply the Terraform configuration
Use the Terraform CLI to provision infrastructure based on the configuration file. For more information, see Basic Terraform commands.
Initialize Terraform. You need to do this only once per directory.
terraform init
Optionally, to use the latest Google provider version, include the
-upgradeoption:terraform init -upgrade
Review the configuration and verify that the resources that Terraform will create or update match your expectations:
terraform plan
Correct the configuration as necessary.
Apply the Terraform configuration by running the following command and entering
yesat the prompt:terraform apply
Typically, you apply the entire configuration at once. However, you can also target a specific resource. For example:
terraform apply -target="google_eventarc_message_bus.default"
Wait until Terraform displays the "Apply complete!" message.
Publish an event message to the bus by creating a workflow
Workflows is a fully managed orchestration platform that executes services in an order that you define: a workflow. Create a workflow to generate a supported event type from a Google source.
In your home directory, create a new file called
myWorkflow.yaml.Copy and paste the following workflow into the new file, then save it:
main: params: [input] steps: - checkSearchTermInInput: switch: - condition: '${"searchTerm" in input}' assign: - searchTerm: '${input.searchTerm}' next: readWikipedia - getLocation: call: sys.get_env args: name: GOOGLE_CLOUD_LOCATION result: location - setFromCallResult: assign: - searchTerm: '${text.split(location, "-")[0]}' - readWikipedia: call: http.get args: url: 'https://en.wikipedia.org/w/api.php' query: action: opensearch search: '${searchTerm}' result: wikiResult - returnOutput: return: '${wikiResult.body[1]}'This workflow passes the region where the workflow is deployed to the Wikipedia API and returns a list of related Wikipedia articles.
Deploy the workflow by using the
gcloud workflows deploycommand:gcloud workflows deploy example-workflow --source=myWorkflow.yaml \ --service-account=eventarc-advanced-sa@PROJECT_ID.iam.gserviceaccount.com \ --location=us-central1
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.
Filter the log entries created by your service:
gcloud logging read 'jsonPayload.message: "Received event of type google.cloud.workflows.workflow.v1.created."'Look for a log entry similar to the following:
message: 'Received event of type google.cloud.workflows.workflow.v1.created. Event data: {"@type":"type.googleapis.com/google.events.cloud.workflows.v1.WorkflowEventData","payload":{"name":"projects/PROJECT_ID/locations/us-central1/workflows/example-workflow","state":"ACTIVE"...
You have successfully created an Eventarc Advanced bus and enrollment, enabled the publishing of events from Google sources, created a workflow to generate a supported event type from a Google provider, and then verified the expected outcome in the logs of the event receiver service.
Clean up
Remove resources previously applied with your Terraform configuration by running the following
command and entering yes at the prompt:
terraform destroy
You can also delete your Google Cloud project to avoid incurring charges. Deleting your Google Cloud project stops billing for all the resources used within that project.
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.