Skip to content

Commit 6bcf974

Browse files
Added Events for Cloud Run for Anthos – GCS and PubSub tutorials. Fix… (GoogleCloudPlatform#4232)
* Added Events for Cloud Run for Anthos – GCS and PubSub tutorials. Fixes issue GoogleCloudPlatform#4231 * Externalized cluster name and location into env vars * Added a note to let people know that a cluster in needed Co-authored-by: Dina Graves Portman <dinagraves@google.com>
1 parent 2977bc8 commit 6bcf974

File tree

3 files changed

+186
-2
lines changed

3 files changed

+186
-2
lines changed

run/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ This directory contains samples for [Google Cloud Run](https://cloud.run). [Clou
1515
|[Cloud Pub/Sub][pubsub] | Handling Pub/Sub push messages | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_pubsub] |
1616
|[Cloud SQL (MySQL)][mysql] | Use MySQL with Cloud Run | - |
1717
|[Cloud SQL (Postgres)][postgres] | Use Postgres with Cloud Run | - |
18-
|[Events – Pub/Sub][events_pubsub] | Event-driven service with Events for Cloud Run for Pub/Sub | - |
19-
|[Events – GCS][events_storage] | Event-driven service with Events for Cloud Run for GCS | - |
18+
|[Events – Pub/Sub][events_pubsub] | Event-driven service with Events for Cloud Run for Pub/Sub | - |
19+
|[Anthos Events – Pub/Sub][anthos_events_pubsub] | Event-driven service with Events for Cloud Run on Anthos for Pub/Sub | - |
20+
|[Events – GCS][events_storage] | Event-driven service with Events for Cloud Run for GCS | - |
21+
|[Anthos Events – GCS][anthos_events_storage] | Event-driven service with Events for Cloud Run on Anthos for GCS | - |
2022

2123
For more Cloud Run samples beyond Python, see the main list in the [Cloud Run Samples repository](https://github.com/GoogleCloudPlatform/cloud-run-samples).
2224

@@ -112,7 +114,9 @@ for more information.
112114
[mysql]: ../cloud-sql/mysql/sqlalchemy
113115
[postgres]: ../cloud-sql/postgres/sqlalchemy
114116
[events_pubsub]: events-pubsub/
117+
[anthos_events_pubsub]: events-pubsub/anthos.md
115118
[events_storage]: events-storage/
119+
[anthos_events_storage]: events-storage/anthos.md
116120
[run_button_helloworld]: https://deploy.cloud.run/?git_repo=https://github.com/knative/docs&dir=docs/serving/samples/hello-world/helloworld-python
117121
[run_button_pubsub]: https://deploy.cloud.run/?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&dir=run/pubsub
118122
[testing]: https://cloud.google.com/run/docs/testing/local#running_locally_using_docker_with_access_to_services

run/events-pubsub/anthos.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Events for Cloud Run on Anthos – Pub/Sub tutorial
2+
3+
This sample shows how to create a service that processes Pub/Sub events. We assume
4+
that you have a GKE cluster created with Events for Cloud Run enabled.
5+
6+
## Setup
7+
8+
Login to gcloud:
9+
10+
```sh
11+
gcloud auth login
12+
```
13+
14+
Configure project id:
15+
16+
```sh
17+
gcloud config set project [PROJECT-ID]
18+
```
19+
20+
Configure environment variables:
21+
22+
```sh
23+
MY_RUN_SERVICE=pubsub-service
24+
MY_RUN_CONTAINER=pubsub-container
25+
MY_TOPIC=pubsub-topic
26+
MY_PUBSUB_TRIGGER=pubsub-trigger
27+
MY_CLUSTER_NAME=events-cluster
28+
MY_CLUSTER_LOCATION=us-central1-c
29+
```
30+
31+
## Quickstart
32+
33+
Set cluster name, location and platform:
34+
35+
```sh
36+
gcloud config set run/cluster ${MY_CLUSTER_NAME}
37+
gcloud config set run/cluster_location ${MY_CLUSTER_LOCATION}
38+
gcloud config set run/platform gke
39+
```
40+
41+
Deploy your Cloud Run service:
42+
43+
```sh
44+
gcloud builds submit \
45+
--tag gcr.io/$(gcloud config get-value project)/$MY_RUN_CONTAINER
46+
gcloud run deploy $MY_RUN_SERVICE \
47+
--image gcr.io/$(gcloud config get-value project)/$MY_RUN_CONTAINER
48+
```
49+
50+
Create a Cloud Pub/Sub topic:
51+
52+
```sh
53+
gcloud pubsub topics create $MY_TOPIC
54+
```
55+
56+
Create a Cloud Pub/Sub trigger:
57+
58+
```sh
59+
gcloud alpha events triggers create $MY_PUBSUB_TRIGGER \
60+
--source CloudPubSubSource \
61+
--target-service $MY_RUN_SERVICE \
62+
--type com.google.cloud.pubsub.topic.publish \
63+
--parameters topic=$MY_TOPIC
64+
```
65+
66+
## Test
67+
68+
Test your Cloud Run service by publishing a message to the topic:
69+
70+
```sh
71+
gcloud pubsub topics publish $MY_TOPIC --message="John Doe"
72+
```
73+
74+
You may observe the Cloud Run service printing upon receiving an event in
75+
Cloud Logging.
76+
77+
```sh
78+
gcloud logging read "resource.type=cloud_run_revision AND \
79+
resource.labels.service_name=$MY_RUN_SERVICE" --project \
80+
$(gcloud config get-value project) --limit 30 --format 'value(textPayload)'
81+
```

run/events-storage/anthos.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Events for Cloud Run on Anthos – GCS tutorial
2+
3+
This sample shows how to create a service that processes GCS events. We assume
4+
that you have a GKE cluster created with Events for Cloud Run enabled.
5+
6+
## Setup
7+
8+
Login to gcloud:
9+
10+
```sh
11+
gcloud auth login
12+
```
13+
14+
Configure project id:
15+
16+
```sh
17+
gcloud config set project [PROJECT-ID]
18+
```
19+
20+
Configure environment variables:
21+
22+
```sh
23+
MY_RUN_SERVICE=gcs-service
24+
MY_RUN_CONTAINER=gcs-container
25+
MY_GCS_TRIGGER=gcs-trigger
26+
MY_GCS_BUCKET=gcs-bucket-$(gcloud config get-value project)
27+
MY_CLUSTER_NAME=events-cluster
28+
MY_CLUSTER_LOCATION=us-central1-c
29+
```
30+
31+
## Quickstart
32+
33+
Set cluster name, location and platform:
34+
35+
```sh
36+
gcloud config set run/cluster ${MY_CLUSTER_NAME}
37+
gcloud config set run/cluster_location ${MY_CLUSTER_LOCATION}
38+
gcloud config set run/platform gke
39+
```
40+
41+
Deploy your Cloud Run service:
42+
43+
```sh
44+
gcloud builds submit \
45+
--tag gcr.io/$(gcloud config get-value project)/$MY_RUN_CONTAINER
46+
gcloud run deploy $MY_RUN_SERVICE \
47+
--image gcr.io/$(gcloud config get-value project)/$MY_RUN_CONTAINER
48+
```
49+
50+
Create a bucket:
51+
52+
```sh
53+
gsutil mb -p $(gcloud config get-value project) -l \
54+
us-central1 gs://"$MY_GCS_BUCKET"
55+
```
56+
57+
Before creating a trigger, you need to give the default service account for
58+
Cloud Storage permission to publish to Pub/Sub.
59+
60+
Find the Service Account that Cloud Storage uses to publish
61+
to Pub/Sub. You can use the steps outlined in [Cloud Console or the JSON
62+
API](https://cloud.google.com/storage/docs/getting-service-account). Assume the
63+
service account you found from above was
64+
`service-XYZ@gs-project-accounts.iam.gserviceaccount.com`, set this to an
65+
environment variable:
66+
67+
```sh
68+
export GCS_SERVICE_ACCOUNT=service-XYZ@gs-project-accounts.iam.gserviceaccount.com
69+
gcloud projects add-iam-policy-binding $(gcloud config get-value project) \
70+
--member=serviceAccount:${GCS_SERVICE_ACCOUNT} \
71+
--role roles/pubsub.publisher
72+
```
73+
74+
Create Cloud Storage trigger:
75+
76+
```sh
77+
gcloud alpha events triggers create $MY_GCS_TRIGGER \
78+
--target-service $MY_RUN_SERVICE \
79+
--type=com.google.cloud.storage.object.finalize \
80+
--parameters bucket=$MY_GCS_BUCKET
81+
```
82+
83+
## Test
84+
85+
Test your Cloud Run service by uploading a file to the bucket:
86+
87+
```sh
88+
echo "Hello World" > random.txt
89+
gsutil cp random.txt gs://$MY_GCS_BUCKET/random.txt
90+
```
91+
92+
Observe the Cloud Run service printing upon receiving an event in
93+
Cloud Logging:
94+
95+
```sh
96+
gcloud logging read "resource.type=cloud_run_revision AND \
97+
resource.labels.service_name=$MY_RUN_SERVICE" --project \
98+
$(gcloud config get-value project) --limit 30 --format 'value(textPayload)'
99+
```

0 commit comments

Comments
 (0)