This tutorial shows you how to use Eventarc to build a processing pipeline that schedules queries to a public BigQuery dataset, generates charts based on the data, and shares links to the charts through email.
Create a SendGrid API key
SendGrid is a cloud-based email provider that lets you send email without having to maintain email servers.
- Sign in to SendGrid and go to Settings > API Keys.
- Click Create API Key.
- Select the permissions for the key. At a minimum, the key must have Mail Send permissions to send email.
- Name your key and to create the key, click Save.
- SendGrid generates a new key. This is the only copy of the key, so make sure that you copy the key and save it for later.
Create an Artifact Registry standard repository
Create an Artifact Registry standard repository to store your Docker container image:
gcloud artifacts repositories create REPOSITORY \ --repository-format=docker \ --location=$REGION
Replace REPOSITORY
with a unique name for the
repository.
Create a Cloud Storage bucket
Create a unique Cloud Storage bucket to save the charts. Make sure that the bucket and the charts are publicly available, and in the same region as your Cloud Run service:
export BUCKET="$(gcloud config get-value core/project)-charts" gcloud storage buckets create gs://${BUCKET} --location=$(gcloud config get-value run/region) gcloud storage buckets update gs://${BUCKET} --uniform-bucket-level-access gcloud storage buckets add-iam-policy-binding gs://${BUCKET} --member=allUsers --role=roles/storage.objectViewer
Deploy the Notifier service
Deploy a Cloud Run service that receives Chart Creator events and uses SendGrid to email links to the generated charts.
Clone the GitHub repository and change to the
notifier/python
directory:git clone https://github.com/GoogleCloudPlatform/eventarc-samples cd eventarc-samples/processing-pipelines/bigquery/notifier/python/
Build and push the container image:
export SERVICE_NAME=notifier docker build -t $REGION-docker.pkg.dev/$(gcloud config get-value project)/REPOSITORY/${SERVICE_NAME}:v1 . docker push $REGION-docker.pkg.dev/$(gcloud config get-value project)/REPOSITORY/${SERVICE_NAME}:v1
Deploy the container image to Cloud Run, passing in an address to send emails to, and the SendGrid API key:
export TO_EMAILS=EMAIL_ADDRESS export SENDGRID_API_KEY=YOUR_SENDGRID_API_KEY gcloud run deploy ${SERVICE_NAME} \ --image $REGION-docker.pkg.dev/$(gcloud config get-value project)/REPOSITORY/${SERVICE_NAME}:v1 \ --update-env-vars TO_EMAILS=${TO_EMAILS},SENDGRID_API_KEY=${SENDGRID_API_KEY},BUCKET=${BUCKET} \ --allow-unauthenticated
Replace the following:
EMAIL_ADDRESS
with an email address to send the links to the generated chartsYOUR_SENDGRID_API_KEY
with the SendGrid API key you noted previously
When you see the service URL, the deployment is complete.
Create a trigger for the Notifier service
The Eventarc trigger for the Notifier service deployed on
Cloud Run filters for Cloud Storage audit logs where
the methodName
is storage.objects.create
.
Create the trigger:
gcloud eventarc triggers create trigger-${SERVICE_NAME} \ --destination-run-service=${SERVICE_NAME} \ --destination-run-region=${REGION} \ --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
trigger-notifier
.
Deploy the Chart Creator service
Deploy a Cloud Run service that receives Query Runner events, retrieves data from a BigQuery table for a specific country, and then generates a chart, using Matplotlib, from the data. The chart is uploaded to a Cloud Storage bucket.
Change to the
chart-creator/python
directory:cd ../../chart-creator/python
Build and push the container image:
export SERVICE_NAME=chart-creator docker build -t $REGION-docker.pkg.dev/$(gcloud config get-value project)/REPOSITORY/${SERVICE_NAME}:v1 . docker push $REGION-docker.pkg.dev/$(gcloud config get-value project)/REPOSITORY/${SERVICE_NAME}:v1
Deploy the container image to Cloud Run, passing in
BUCKET
:gcloud run deploy ${SERVICE_NAME} \ --image $REGION-docker.pkg.dev/$(gcloud config get-value project)/REPOSITORY/${SERVICE_NAME}:v1 \ --update-env-vars BUCKET=${BUCKET} \ --allow-unauthenticated
When you see the service URL, the deployment is complete.
Create a trigger for the Chart Creator service
The Eventarc trigger for the Chart Creator service deployed on Cloud Run filters for messages published to a Pub/Sub topic.
Create the trigger:
gcloud eventarc triggers create trigger-${SERVICE_NAME} \ --destination-run-service=${SERVICE_NAME} \ --destination-run-region=${REGION} \ --event-filters="type=google.cloud.pubsub.topic.v1.messagePublished"
This creates a trigger called
trigger-chart-creator
.Set the Pub/Sub topic environment variable.
export TOPIC_QUERY_COMPLETED=$(basename $(gcloud eventarc triggers describe trigger-${SERVICE_NAME} --format='value(transport.pubsub.topic)'))
Deploy the Query Runner service
Deploy a Cloud Run service that receives Cloud Scheduler events, retrieves data from a public COVID-19 dataset, and saves the results in a new BigQuery table.
Change to the
processing-pipelines
directory:cd ../../..
Build and push the container image:
export SERVICE_NAME=query-runner docker build -t $REGION-docker.pkg.dev/$(gcloud config get-value project)/REPOSITORY/${SERVICE_NAME}:v1 -f Dockerfile . docker push $REGION-docker.pkg.dev/$(gcloud config get-value project)/REPOSITORY/${SERVICE_NAME}:v1
Deploy the container image to Cloud Run, passing in
PROJECT_ID
andTOPIC_QUERY_COMPLETED
:gcloud run deploy ${SERVICE_NAME} \ --image $REGION-docker.pkg.dev/$(gcloud config get-value project)/REPOSITORY/${SERVICE_NAME}:v1 \ --update-env-vars PROJECT_ID=$(gcloud config get-value project),TOPIC_ID=${TOPIC_QUERY_COMPLETED} \ --allow-unauthenticated
When you see the service URL, the deployment is complete.
Create a trigger for the Query Runner service
The Eventarc trigger for the Query Runner service deployed on Cloud Run filters for messages published to a Pub/Sub topic.
Create the trigger:
gcloud eventarc triggers create trigger-${SERVICE_NAME} \ --destination-run-service=${SERVICE_NAME} \ --destination-run-region=${REGION} \ --event-filters="type=google.cloud.pubsub.topic.v1.messagePublished"
This creates a trigger called
trigger-query-runner
.Set an environment variable for the Pub/Sub topic.
export TOPIC_QUERY_SCHEDULED=$(gcloud eventarc triggers describe trigger-${SERVICE_NAME} --format='value(transport.pubsub.topic)')
Schedule the jobs
The processing pipeline is triggered by two Cloud Scheduler jobs.
Create an App Engine app which is required by Cloud Scheduler and specify an appropriate location:
export APP_ENGINE_LOCATION=LOCATION gcloud app create --region=${APP_ENGINE_LOCATION}
Create two Cloud Scheduler jobs that publish to a Pub/Sub topic once per day:
gcloud scheduler jobs create pubsub cre-scheduler-uk \ --schedule="0 16 * * *" \ --topic=${TOPIC_QUERY_SCHEDULED} \ --message-body="United Kingdom"
gcloud scheduler jobs create pubsub cre-scheduler-cy \ --schedule="0 17 * * *" \ --topic=${TOPIC_QUERY_SCHEDULED} \ --message-body="Cyprus"
The schedule is specified in unix-cron format. For example,
0 16 * * *
means that the jobs runs at 16:00 (4 PM) UTC every day.
Run the pipeline
First, confirm that all the triggers were successfully created:
gcloud eventarc triggers list
The output should be similar to the following:
NAME: trigger-chart-creator TYPE: google.cloud.pubsub.topic.v1.messagePublished DESTINATION: Cloud Run service: chart-creator ACTIVE: Yes LOCATION: us-central1 NAME: trigger-notifier TYPE: google.cloud.audit.log.v1.written DESTINATION: Cloud Run service: notifier ACTIVE: Yes LOCATION: us-central1 NAME: trigger-query-runner TYPE: google.cloud.pubsub.topic.v1.messagePublished DESTINATION: Cloud Run service: query-runner ACTIVE: Yes LOCATION: us-central1
Retrieve the Cloud Scheduler job IDs:
gcloud scheduler jobs list
The output should be similar to the following:
ID LOCATION SCHEDULE (TZ) TARGET_TYPE STATE cre-scheduler-cy us-central1 0 17 * * * (Etc/UTC) Pub/Sub ENABLED cre-scheduler-uk us-central1 0 16 * * * (Etc/UTC) Pub/Sub ENABLED
Although the jobs are scheduled to run daily at 4 and 5 PM, you can also run the Cloud Scheduler jobs manually:
gcloud scheduler jobs run cre-scheduler-cy gcloud scheduler jobs run cre-scheduler-uk
After a few minutes, confirm that there are two charts in the Cloud Storage bucket:
gcloud storage ls gs://${BUCKET}
The output should be similar to the following:
gs://BUCKET/chart-cyprus.png gs://BUCKET/chart-unitedkingdom.png
Congratulations! You should also receive two emails with links to the charts.