This quickstart shows you how to receive events from Cloud Storage in an unauthenticated Cloud Run (fully managed) service using Eventarc.
You can complete this quickstart using either the Google Cloud Console or the
gcloud
command-line tool.
In this quickstart, you:
Create a Cloud Storage bucket to be the event source.
Deploy an event receiver service to Cloud Run (fully managed).
Create an event trigger.
Generate an event by uploading a file to the Cloud Storage bucket, and view it in the Cloud Run (fully managed) logs.
Before you begin
- 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.
-
Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.
- Enable the Cloud Run, Cloud Logging, Cloud Build, Pub/Sub, Eventarc APIs.
- When using the
gcloud
tool:- Update
gcloud
components:gcloud components update
- Log in using your account:
gcloud auth login
- Set the configuration variables used in this quickstart:
gcloud config set project PROJECT_ID gcloud config set run/region us-central1 gcloud config set run/platform managed gcloud config set eventarc/location us-central1
- Enable Cloud Audit Logs Admin Read, Data Read, and Data Write Log Types in Google Cloud Storage:
- Grant the
iam.serviceAccountTokenCreator
role to the Pub/Sub service account:export PROJECT_NUMBER="$(gcloud projects describe $(gcloud config get-value project) --format='value(projectNumber)')" gcloud projects add-iam-policy-binding $(gcloud config get-value project) \ --member="serviceAccount:service-${PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com"\ --role='roles/iam.serviceAccountTokenCreator'
- Grant the
eventarc.eventReceiver
role to the Compute Engine service account:gcloud projects add-iam-policy-binding $(gcloud config get-value project) \ --member=serviceAccount:${PROJECT_NUMBER}-compute@developer.gserviceaccount.com \ --role='roles/eventarc.eventReceiver'
- Download and install the Git source code management tool.
- Update
Creating a Cloud Storage bucket
This quickstart uses Cloud Storage as the event source. To create a storage bucket:
Console
Go to Cloud Storage in the Cloud Console.
Click
Create bucket.Enter your bucket information and click Continue to complete each step:
- Enter a unique Name. For example,
eventarcbucket
. - Select Region as the Location Type.
- Select us-central1 (Iowa) as the Location.
- Select Standard for default storage class.
- Select Uniform for Access control.
- Enter a unique Name. For example,
Click Create.
Command line
gsutil mb -l us-central1 gs://events-quickstart-$(gcloud config get-value project)/
After the event source is created, you can deploy the event receiver service on Cloud Run (fully managed).
Deploying the event receiver service to Cloud Run (fully managed)
Deploy a Cloud Run (fully managed) service that receives and logs events. To deploy the sample event receiver service:
Console
Clone the sample repository to your GitHub account:
Go
On GitHub, navigate to GoogleCloudPlatform/golang-samples.
Click Fork.
If prompted, select the location where you want to fork the repository.
Java
On GitHub, navigate to GoogleCloudPlatform/java-docs-samples.
Click Fork.
If prompted, select the location where you want to fork the repository.
.NET
On GitHub, navigate to GoogleCloudPlatform/dotnet-docs-samples.
Click Fork.
If prompted, select the location where you want to fork the repository.
Node.js
On GitHub, navigate to GoogleCloudPlatform/nodejs-docs-samples.
Click Fork.
If prompted, select the location where you want to fork the repository.
Python
On GitHub, navigate to GoogleCloudPlatform/python-docs-samples.
Click Fork.
If prompted, select the location where you want to fork the repository.
Go to Cloud Run in the Cloud Console.
Click
Create service to display the Create service form.Select Cloud Run (fully managed).
Select us-central1(Iowa) as the Region where you want your service located.
Enter the desired service name. For example,
helloworld-events
.Click Next.
Select Continuously deploy new revisions from a source repository.
Click Set up with Cloud Build to open the Set up with Cloud Build form.
In the Set up with Cloud Build form:
- If prompted, enable the Cloud Build API and Container Analysis API.
Select GitHub as the Repository Provider.
If prompted, click Install Google Cloud Build.
Select the GitHub repository you forked as the Repository.
Click Next.
In the Branch field, enter
^master$
.Select Dockerfile as the Build Type and provide the source location of the Dockerfile:
eventarc/audit-storage/Dockerfile
oreventarc/audit_storage/Dockerfile
(Go)Click Save.
In the Create service form, click Next.
In the Configure how this service is triggered section:
Select Allow all traffic.
Select Allow unauthenticated invocations.
Optionally, click Creating an Eventarc trigger.
Add trigger and create a trigger or create a trigger after you create a service. For more information on creating a trigger, seeClick Create.
Command line
Clone the repository:
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples.git cd golang-samples/eventarc/audit_storage
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git cd java-docs-samples/eventarc/audit-storage
.NET
git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples.git cd dotnet-docs-samples/eventarc/audit-storage
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git cd nodejs-docs-samples/eventarc/audit-storage
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git cd python-docs-samples/eventarc/audit-storage
Build the container and upload it to Cloud Build:
gcloud builds submit --tag gcr.io/$(gcloud config get-value project)/helloworld-events
Deploy the container image to Cloud Run (fully managed):
gcloud run deploy helloworld-events \ --image gcr.io/$(gcloud config get-value project)/helloworld-events \ --allow-unauthenticated
When the deployment succeeds, the command line displays the service URL.
Now that you have deployed your event receiver service called
helloworld-events
to Cloud Run (fully managed), you can set up your trigger.
Creating an Eventarc trigger
The Eventarc trigger will send events from the Cloud Storage
bucket to the helloworld-events
Cloud Run (fully managed) service.
Console
Go to Cloud Run in the Cloud Console.
From the list of services, click the service you created, to go to its Service details page.
Click the Triggers tab, and click
Add trigger.In the Pick an event drop-down list, select Cloud Storage > storage.objects.create.
In the Receive events from field, select Single region.
Enable Cloud Audit Logs for the
storage.googleapis.com
service.If prompted, grant the
eventarc.eventReceiver
role to the Compute Engine service account and theiam.serviceAccountTokenCreator
role to the Pub/Sub service account.Click Save.
Command line
Create a trigger that filters Cloud Storage events and that uses the Google Cloud project Compute Engine default service account:
gcloud eventarc triggers create events-quickstart-trigger \ --destination-run-service=helloworld-events \ --destination-run-region=us-central1 \ --event-filters="type=google.cloud.audit.log.v1.written" \ --event-filters="serviceName=storage.googleapis.com" \ --event-filters="methodName=storage.objects.create" \ --service-account=${PROJECT_NUMBER}-compute@developer.gserviceaccount.com
This creates a trigger called
events-quickstart-trigger
.To confirm
events-quickstart-trigger
was successfully created, run:gcloud eventarc triggers list --location=us-central1
events-quickstart-trigger
is displayed listed with a target ofhelloworld-events
.
Generating and viewing an event
To generate an event:
Console
Create a text file with the file name
random.txt
and the text "Hello World".Go to Cloud Storage in the Cloud Console.
Select the storage bucket you created.
In the Objects tab, click Upload files and upload the
random.txt
file.
Command line
Upload a text file to Cloud Storage:
echo "Hello World" > random.txt gsutil cp random.txt gs://events-quickstart-$(gcloud config get-value project)/random.txt
The upload generates an event and the Cloud Run (fully managed) service logs the event's message.
To view the log entry:
Console
Go to Cloud Run in the Cloud Console.
From the list of services, click the name of the service you created to go to its Service details page.
Click the Logs tab, to get the request and container logs for all revisions of this service. You can filter by log severity level.
Look for a log entry similar to:
Command line
gcloud logging read "resource.type=cloud_run_revision AND resource.labels.service_name=helloworld-events"
Look for a log entry similar to:
Detected change in Cloud Storage bucket: storage.googleapis.com/projects/_/buckets/BUCKET_NAME/objects/random.txt
where
BUCKET_NAME
is the name of the Cloud Storage bucket.
Congratulations! You have successfully deployed an event receiver service to Cloud Run (fully managed), created an Eventarc trigger, generated an event from Cloud Storage, and viewed it in the Cloud Run (fully managed) logs.
Clean up
While Cloud Run does not charge when the service is not in use, you might still be charged for storing the container image in Container Registry, Eventarc resources, and for storing files in your Cloud Storage bucket.
You can delete your image and delete your storage bucket. To delete the Eventarc trigger:
gcloud eventarc triggers delete events-quickstart-trigger
Alternatively, you can delete your Google Cloud project to avoid incurring charges. Deleting your Cloud project stops billing for all the resources used within that project.
- In the 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.