Cloud Scheduler — Trigger Cloud functions using Cloud Pub/Sub and Twilio

Vishal Khare
4 min readDec 18, 2020

Cloud Scheduler is a beautiful new addition to GCP and let me demonstrate how it can be useful.

Objective — I want my girlfriend to receive an automated voice call everyday at 15:00 hrs to remind her to have a good day — Inspired by this awesome video by fireship.

Here is a quick architecture diagram.We will do this in 4 simple steps as follows-

Architecture Diagram

STEP 1 — Twilio setup

First thing first is to setup a Twilio account and get a trial number. You can keep the trial account and use initially provided $10 credit. However, There are 2 disadvantages I see with trial accounts-

  1. Calls from trial accounts start with — “This is a trial account. To remove this message upgrade your account. Press any key to execute your code”. You press any number key and then you hear the message set by you code in cloud function. If this is a deal breaker for you, go ahead and upgrade your account.
  2. Numbers you need to call have to be verified caller IDs i.e. you need to verify those numbers using an OTP.

For the purpose of this explanation, a trial account will do.

After you have setup your Twilio account, your dashboard will look something like this-

Twilio Dashboard

I am not focusing on Twilio much as this article is more about Cloud Scheduler and Cloud function. In case you have any questions, ask in comments.

STEP 2 — Cloud Scheduler setup

Now that Twilio account has been setup, Let’s go ahead and setup a Cloud Scheduler. It is pretty straightforward. All you have to do is to find Cloud Scheduler in GCP console and click on Create Job.

This is how it should look like-

Cloud Scheduler

Notice that frequency is 0 15 * * * which means it will execute daily at 15:00 hrs. To get hold of Unix-Cron format visit https://crontab.guru/

Target is Cloud Pub/Sub topic where we publish a message everytime the job runs.

STEP 3 — Cloud Pub/Sub topic creation

Its time create the Cloud Pub/Sub topic. Simply navigate to Cloud Pub/Sub in GCP console and create a new topic with the same name you used in creation of Cloud Scheduler job. See image below-

Cloud Pub/Sub

STEP 4 — Cloud Function setup

Last step is to setup the cloud function that will subscribe to the above pub/sub topic we created and execute every time a new message is published in the topic.

To create a Cloud function, navigate to Cloud Function in your GCP console and hit Create Function.

See the image below and notice how the trigger is set to listen to the pub/sub topic we created in step 3.

Cloud Function

Remember to add Runtime environment variables under Environment variables. You need to add 2 environment variables —TWILIO_ACCOUNT_SID & TWILIO_AUTH_TOKEN . Both their values can be copied from Twilio dashboard.

Now, Let’s write some code. As you hit save and next in console, an inline editor will open up.

I have used python and here is the code-

Don’t forget to add twilio as a dependency in requirements.txt file. Just the word twilio and NOT pip install twilio

url attribute in line number 17 takes the URL of a publicly accessible XML. This XML consist of the statements that will actually be played during the call. You need to create a similar XML file and put it at any publicly accessible location. Visit http://demo.twilio.com/docs/voice.xml to know what should the XML file look like.

I have my XML file ready, publicly available and it says — “Vishal says — have a good day.” This is what will be played when my girlfriend answers the call.

Deploy you cloud function and you are good to go.

Test the flow by hitting RUN NOW button of Cloud scheduler.

--

--

Vishal Khare

Engineering manager at TATA 1mg || Google Cloud Certified Cloud Engineer