
Before you begin
- Labs create a Google Cloud project and resources for a fixed time
- Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
- On the top left of your screen, click Start lab to begin
Deploy the event logging service to Cloud Run
/ 25
Create a Pub/Sub trigger
/ 25
Create the service account for the Audit Logs trigger
/ 25
Create an Audit Logs trigger
/ 25
Serverless computing on Google Cloud lets you develop and deploy highly scalable applications on a fully managed serverless platform. Services are automatically scaled up and down depending on traffic.
Eventarc lets you build event-driven architectures without having to implement, customize, or maintain the underlying infrastructure. Eventarc offers a standardized solution to manage the flow of state changes, called events, between decoupled services.
An Eventarc trigger is a declaration that a specific destination service is interested in a chosen event from an event provider. Eventarc manages the routing and delivery of events to the destination service.
In this lab, you create a trigger that sends an event when a message is sent to a Pub/Sub topic. You create a second trigger that sends an event when an IAM service account is created.
The events are routed to a Cloud Run service that logs CloudEvents-formatted event data to Cloud Audit Logs.
In this lab, you will learn to:
This Qwiklabs hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is a panel populated with the temporary credentials that you must use for this lab.
Copy the username, and then click Open Google Console. The lab spins up resources, and then opens another tab that shows the Choose an account page.
On the Choose an account page, click Use Another Account. The Sign in page opens.
Paste the username that you copied from the Connection Details panel. Then copy and paste the password.
After a few moments, the Cloud console opens in this tab.
Google Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud.
Google Cloud Shell provides command-line access to your Google Cloud resources.
In Cloud console, on the top right toolbar, click the Open Cloud Shell button.
Click Continue.
It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. For example:
gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
Output:
Example output:
Output:
Example output:
In this task, you enable APIs that are required for the event-driven architecture for this lab.
To enable the required APIs, run the following command:
This application uses several Google Cloud services, and you must enable each of the APIs for these services.
The APIs being enabled are:
In this task, you build and deploy the Cloud Run service that will be used to log events.
In Cloud Shell, clone the git repository:
Create a soft link shortcut to the root directory for this service:
Artifact Registry is the next generation of Container Registry. You can store build artifacts inside an Artifact Registry repository.
To create an Artifact Registry repository for Docker images, in Cloud Shell, run the following command:
To retrieve the repository details, run the following command:
The Eventarc event logger service logs an incoming HTTP request. The log entries created by this service will be used to explore the contents of the Eventarc event request.
In Cloud Shell, click Open Editor.
Click Open in new window.
The editor will open in a new tab.
In the previous tab, click Open Terminal to reopen Cloud Shell.
Return to the Cloud Editor tab, navigate to ~/eventarc-event-logger
, and then examine the files for the eventarc-event-logger service.
The directory contains three files:
To build the Docker image of the eventarc-event-logger service, in Cloud Shell, run the following command:
Cloud Build builds the Docker image and stores it in the specified Artifact Registry repository.
To deploy the service to Cloud Run, in Cloud Shell, run the following commands:
The Docker image you stored in the Artifact Registry repository is now being deployed to Cloud Run.
In the Google Cloud console, navigate to Cloud Run.
You should see that the service is deployed or is in the process of deploying.
Wait for the deployment to finish.
Click Check my progress to verify the objective.
In this task, you create and test a trigger that delivers an event when a message is published to a Pub/Sub topic.
In the Google Cloud console, navigate to Cloud Run, and then click on eventarc-event-logger service.
On the Service details page, navigate to the Triggers tab.
Click + Add Triggers and select Other Eventarc trigger from the dropdown.
In the Eventarc trigger window, specify the following:
Property | Value |
---|---|
Trigger name | pubsub-trigger |
Trigger type | select Google sources |
Event provider | select Cloud Pub/Sub |
Event type | select google.cloud.pubsub.topic.v1.messagePublished |
Select a Cloud Pub/Sub topic | leave as None |
Region |
select |
By leaving the Pub/Sub topic set to None, you have indicated that Eventarc should automatically create and manage the Pub/Sub topic for this trigger. If your Eventarc trigger is deleted, the corresponding Pub/Sub topic will also be automatically deleted.
On the same page, select Service account > Create New Service Account.
On the Create new service account pane, set the Service account name to pubsub-trigger-sa
and then click Create.
Click Done.
If you're notified that Pub/Sub needs the role roles/iam.serviceAccountTokenCreator granted to the Pub/Sub service account, click Grant.
You can also add the role by running the following command in Cloud Shell:
Click Save Trigger.
After the trigger has been created, you're automatically taken to the Service details page.
The Pub/Sub topic has already been created for you. Your topic should resemble this:
On the Service details page, click the Logs tab.
Log entries from the eventarc-event-logger service are shown here. You should see that the service has been deployed, but there are no events logged yet.
In Cloud Shell, run the following commands:
The gcloud pubsub topics publish
command sends a message with attached attributes to the topic.
On the Logs page for eventarc-event-logger, drag the scroll bar to search for newer entries in the log.
The log entries for the eventarc-event-logger service are shown, but the Pub/Sub event has not been logged!
Instead, you will see one or more POST 403 entries.
Expand one of the POST 403 entries.
The textPayload field in the log shows an error that looks like this:
The error message indicates that you can solve the problem by allowing unauthenticated invocations. It is not a best practice to allow unauthenticated calls to your event receivers.
The HTTP request from Eventarc was unauthorized. Do you know why that might be?
The eventarc-event-logger service is being called, but the error indicates that the service account can't call the Cloud Run service.
Follow the principle of least privilege when creating service accounts. The service account should only be able to access services necessary for the specific trigger.
To give the service account permission to call the Cloud Run service, in Cloud Shell, run the following commands:
To send another message, run the following commands:
Drag the scrollbar on the Logs page for eventarc-event-logger until you see a successful event.
When the service successfully receives the event, it logs EVENT RECEIVED (google.cloud.pubsub.topic.v1.messagePublished)
. This log entry shows the event type that was delivered in the ce-type header.
Expand the log entry that follows the HEADERS (EXCEPT AUTH)
entry, and then expand the jsonPayload object.
The jsonPayload object contains the headers that were received with the request. Important headers include:
Header | Example value |
---|---|
ce-id | 7029339182900292 |
ce-type | google.cloud.pubsub.topic.v1.messagePublished |
ce-source | //pubsub.googleapis.com/projects/qwiklabs-gcp-03-cbf626add551/topics/eventarc-us-west1-pubsub-trigger-830 |
ce-time | 2023-02-27T23:02:53.861Z |
content-type | application/json |
Headers starting with ce-
are CloudEvents standard headers.
ce-id is the Pub/Sub message ID of the message you delivered to the topic. The message ID is used as the CloudEvents ID for Pub/Sub messaging triggers.
ce-type is the event type of the received event.
ce-source is the URI for the topic.
ce-time indicates the time the Pub/Sub message was received by the topic.
content-type specifies the type of the body. In this case, the header indicates that the body contains JSON.
Expand the log entry that follows the BODY
entry, and then expand the jsonPayload object.
The jsonPayload object in this entry contains the Pub/Sub event. The JSON for this object resembles this:
You can see the attributes that were added to the Pub/Sub message. The message content is in the data value, stored as base64 data.
In Cloud Shell, run the following command:
The base64 data decodes to My Pub/Sub message!
.
Click Check my progress to verify the objective.
In this task, you create a service account for the Audit Logs trigger and add the required permissions.
Before you create the trigger, you use gcloud CLI commands to create the service account with the correct permissions.
To create the service account, in Cloud Shell, run the following command:
This service account initially has no roles assigned.
For triggers that source events from Cloud Audit Logs entries, the service account needs the eventarc.eventReceiver.
To let the service account use Cloud Audit Logs entries as a source, run the following command:
To let the service account invoke the eventarc-event-logger service, run the following command:
Click Check my progress to verify the objective.
In this task, you create and test a trigger that delivers an event when a log entry indicates that a service account was created.
In the Google Cloud console, navigate to Cloud Run, and then click on eventarc-event-logger service.
On the Service details page, navigate to the Triggers tab.
Click + Add Triggers and select Other Eventarc trigger from the dropdown.
In the Eventarc trigger window, specify the following:
Property | Value |
---|---|
Trigger name | svcacct-create-trigger |
Trigger type | select Google sources |
Event provider | select Cloud IAM |
Event type | select google.iam.admin.v1.CreateServiceAccount |
Resource | select Any resource |
Region |
select |
Service account | select the svcacct-create-sa service account |
By selecting Any resource, you will get an event for each service account created for this project.
Click Save Trigger.
After the trigger has been created, you're automatically taken to the Service details page.
On the Service details page, click the Logs tab.
You should only see google.cloud.pubsub.topic.v1.messagePublished logs. The generation of IAM service account creation events started when the trigger was created. Service accounts you created before the trigger will not result in events.
To create a new service account, in Cloud Shell, run the following command:
Drag the scrollbar on the Logs page for eventarc-event-logger until you see a successful audit log event.
You should see an EVENT RECEIVED (google.cloud.audit.log.v1.written)
log entry. All events that are generated by Cloud Audit Logs entries use the google.cloud.audit.log.v1.written event type.
Expand the log entry that follows the latest HEADERS (EXCEPT AUTH)
entry, and then expand the jsonPayload object.
The jsonPayload object contains the headers that were received with the request. Important headers include:
Header | Example value |
---|---|
ce-id | projects/qwiklabs-gcp-03-cbf626add551/logs/cloudaudit.googleapis.com%2Factivity1i3uhx4e1rkoq1677550139268490 |
ce-type | google.cloud.audit.log.v1.written |
ce-servicename | iam.googleapis.com |
ce-methodname | google.iam.admin.v1.CreateServiceAccount |
ce-time | 2023-02-28T02:09:00.735689083Z |
ce-recordedtime | 2023-02-28T02:08:59.268490773Z |
Headers starting with ce-
are CloudEvents standard headers.
ce-id specifies the unique CloudEvents ID for the event.
ce-type is the event type of the received event. For Cloud Audit Logs events, the type is always google.cloud.audit.log.v1.written
.
ce-servicename specifies the service that created the audit log entry.
ce-methodname specifies the log entry type. For this example, the method name is the Event that you selected when creating the trigger.
ce-time indicates the time that the audit log entry was created.
ce-recordedtime indicates the time that the actual event occurred, according to the originating service. For this event, the recorded time is the time when the service account was created.
Expand the log entry that follows the latest BODY
entry, and then expand the jsonPayload object.
The jsonPayload object contains the IAM event. The JSON for the object looks similar to this:
You can see the details of the service account creation in the request and response objects. The log entry shows the details of the API call made by the gcloud CLI command.
Click Check my progress to verify the objective.
In this lab, you enabled required APIs and built and deployed a Cloud Run service that logs delivered events. You created an Eventarc trigger that generated an event when a message was sent to a Pub/Sub topic. You created another trigger that sent an event when an IAM service account was created.
For more information about Eventarc, view these links:
Copyright 2023 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.
This content is not currently available
We will notify you via email when it becomes available
Great!
We will contact you via email if it becomes available
One lab at a time
Confirm to end all existing labs and start this one