This repo contains 2 quickstart samples for Events for Cloud Run. The samples are offered in a variety of languages but have common setup and testing instructions described here.
The setup assumes you've installed gcloud
.
Update gcloud
and enable services:
gcloud components update
gcloud components install alpha
gcloud services enable run.googleapis.com
gcloud services enable logging.googleapis.com
gcloud services enable cloudbuild.googleapis.com
# gcloud config set project my-project
gcloud config set run/region us-central1 # Event Sinks, Sources, and Run services must be in the same region
gcloud config set run/platform managed
Choose one of the language folders (i.e. node
, python
, etc.) to set up a Cloud Run service.
Then continue instructions here.
gsutil mb -p $(gcloud config get-value project) -l us-central1 gs://"$MY_GCS_BUCKET"/ # Create bucket
gsutil du -s gs://"$MY_GCS_BUCKET"/ # Verify bucket exists, should _not_ be a BucketNotFoundException
Note: Must be a single region in the same region as your cloud run service.
Be sure you have the latest gcloud component without Cloud Workflows installed.
gcloud alpha events triggers create my-gcs-trigger \
--target-service "$MY_RUN_SERVICE" \
--type com.google.cloud.auditlog.event \
--parameters methodName=storage.buckets.update \
--parameters serviceName=storage.googleapis.com \
--parameters resourceName=projects/_/buckets/"$MY_GCS_BUCKET" # resourceName is optional.
Takes up to 10 minutes. Once ready, GCS update events on "$MY_GCS_BUCKET"
and send them to the cloudrun-events
service.
Trigger an event by updating the bucket (storage class):
gsutil defstorageclass set NEARLINE gs://$MY_GCS_BUCKET
gsutil defstorageclass set STANDARD gs://$MY_GCS_BUCKET
This will send an HTTP POST
request to our Cloud Run service.
Look at the event you've created by looking at Stackdriver logs:
gcloud logging read "projects/$(gcloud config get-value project)/logs/cloudaudit.googleapis.com%2Factivity" --limit=1 --format=json
Note: Ensure you're looking at the right log. The field
timestamp
should be the event's fire time.
Choose one of the language folders (i.e. node
, python
, etc.) to set up a Cloud Run service.
Then continue instructions here.
gcloud pubsub topics create my-topic
gcloud alpha events types describe com.google.cloud.pubsub.topic.publish
gcloud alpha events triggers create pubsub-trigger \
--target-service cloudrun-events \
--type com.google.cloud.pubsub.topic.publish \
--parameters topic=my-topic
gcloud pubsub topics publish my-topic --message="Hello there"
com.google.cloud.pubsub.topic.publish
gcloud logging read "projects/$(gcloud config get-value project)/logs/cloudaudit.googleapis.com%2Factivity" --limit=1 --format=json
The Audit Log should appear in this format:
https://cloud.google.com/logging/docs/reference/audit/auditlog/rest/Shared.Types/AuditLog
Read more about Audit Logs here:
https://cloud.google.com/logging/docs/audit/understanding-audit-logs?hl=en#sample
Delete the items that were created above:
gcloud container images delete gcr.io/$(gcloud config get-value project)/"$MY_RUN_CONTAINER" -q
gcloud run services delete "$MY_RUN_CONTAINER" -q
gcloud alpha events triggers delete my-gcs-trigger -q
gsutil rm -r gs://"$MY_GCS_BUCKET"/
Audit log documentation is here https://cloud.google.com/logging/docs/audit/understanding-audit-logs
View logs via the Cloud Console (http://console.cloud.google.com/logs) or terminal:
gcloud logging read "projects/$(gcloud config get-value project)/logs/cloudaudit.googleapis.com%2Factivity" --limit=1 --format=json
List events:
gcloud alpha events types list
For example, com.google.cloud.auditlog.event
and com.google.cloud.pubsub.topic.publish
.
Describe events:
gcloud alpha events types describe com.google.cloud.auditlog.event
gcloud alpha events triggers list
gcloud alpha events triggers describe my-trigger
Client libraries for this API are not available yet. This discovery document for creating an API client is located here:
https://run.googleapis.com/$discovery/rest?version=v1beta1?key=YOUR_API_KEY