|
| 1 | +# Events for Cloud Run on Anthos - Cloud Storage tutorial |
| 2 | + |
| 3 | +This sample shows how to create a service that processes GCS events on |
| 4 | +Anthos. We assume that you have a GKE cluster created with Events for Cloud Run enabled. |
| 5 | + |
| 6 | +For more details on how to work with this sample read the [Google Cloud Run Java Samples README](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/run). |
| 7 | + |
| 8 | +## Dependencies |
| 9 | + |
| 10 | +* **Spring Boot**: Web server framework. |
| 11 | +* **Jib**: Container build tool. |
| 12 | +* **Junit + SpringBootTest**: [development] Test running framework. |
| 13 | +* **MockMVC**: [development] Integration testing support framework. |
| 14 | + |
| 15 | +## Setup |
| 16 | + |
| 17 | +Configure environment variables: |
| 18 | + |
| 19 | +```sh |
| 20 | +export MY_RUN_SERVICE=gcs-service |
| 21 | +export MY_RUN_CONTAINER=gcs-container |
| 22 | +export MY_GCS_TRIGGER=gcs-trigger |
| 23 | +export MY_GCS_BUCKET="$(gcloud config get-value project)-gcs-bucket" |
| 24 | +``` |
| 25 | + |
| 26 | +## Quickstart |
| 27 | + |
| 28 | +Set cluster name, location and platform: |
| 29 | + |
| 30 | +```sh |
| 31 | +gcloud config set run/cluster your-cluster-name |
| 32 | +gcloud config set run/cluster_location us-central1-c |
| 33 | +gcloud config set run/platform gke |
| 34 | +``` |
| 35 | + |
| 36 | +Use the [Jib Maven Plugin](https://github.com/GoogleContainerTools/jib/tree/master/jib-maven-plugin) to build and push your container image: |
| 37 | + |
| 38 | +```sh |
| 39 | +mvn jib:build -Dimage gcr.io/$(gcloud config get-value project)/$MY_RUN_CONTAINER |
| 40 | +``` |
| 41 | + |
| 42 | +Deploy your Cloud Run service: |
| 43 | + |
| 44 | +```sh |
| 45 | +gcloud run deploy $MY_RUN_SERVICE \ |
| 46 | +--image gcr.io/$(gcloud config get-value project)/$MY_RUN_CONTAINER \ |
| 47 | +``` |
| 48 | + |
| 49 | +Create a _single region_ Cloud Storage bucket: |
| 50 | + |
| 51 | +```sh |
| 52 | +gsutil mb -p $(gcloud config get-value project) -l us-central1 gs://"$MY_GCS_BUCKET" |
| 53 | +``` |
| 54 | + |
| 55 | +Before creating a trigger, you need to give the default service account for |
| 56 | +Cloud Storage permission to publish to Pub/Sub. |
| 57 | + |
| 58 | +Find the Service Account that Cloud Storage uses to publish |
| 59 | +to Pub/Sub. You can use the steps outlined in [Cloud Console or the JSON |
| 60 | +API](https://cloud.google.com/storage/docs/getting-service-account). Assume the |
| 61 | +service account you found from above was |
| 62 | +`service-XYZ@gs-project-accounts.iam.gserviceaccount.com`, set this to an |
| 63 | +environment variable: |
| 64 | + |
| 65 | +```sh |
| 66 | +export GCS_SERVICE_ACCOUNT=service-XYZ@gs-project-accounts.iam.gserviceaccount.com |
| 67 | +gcloud projects add-iam-policy-binding $(gcloud config get-value project) \ |
| 68 | +--member=serviceAccount:${GCS_SERVICE_ACCOUNT} \ |
| 69 | +--role roles/pubsub.publisher |
| 70 | +``` |
| 71 | + |
| 72 | +Create Cloud Storage trigger: |
| 73 | + |
| 74 | +```sh |
| 75 | +gcloud alpha events triggers create $MY_GCS_TRIGGER \ |
| 76 | +--target-service $MY_RUN_SERVICE \ |
| 77 | +--type=com.google.cloud.storage.object.finalize \ |
| 78 | +--parameters bucket=$MY_GCS_BUCKET |
| 79 | +``` |
| 80 | + |
| 81 | +## Test |
| 82 | + |
| 83 | +Test your Cloud Run service by uploading a file to the bucket: |
| 84 | + |
| 85 | +```sh |
| 86 | +echo "Hello World" > random.txt |
| 87 | +gsutil cp random.txt gs://$MY_GCS_BUCKET/random.txt |
| 88 | +``` |
| 89 | + |
| 90 | +Observe the Cloud Run service printing upon receiving an event in Cloud Logging: |
| 91 | + |
| 92 | +```sh |
| 93 | +gcloud logging read "resource.type=cloud_run_revision AND \ |
| 94 | +resource.labels.service_name=$MY_RUN_SERVICE" --project \ |
| 95 | +$(gcloud config get-value project) --limit 30 --format 'value(textPayload)' |
| 96 | +``` |
0 commit comments