How to periodically run a Cloud Function using Google Cloud Scheduler

Edigleysson Silva
8 min readJan 19, 2021

--

Periodic activities are common in our lives. We all have those things that we have to do every day. Be it mowing the lawn once a week, going to the gym every day, whatever. This type of activity is part of our lives.

Applications that we use on a daily basis also need to adapt to this and we as software engineers must keep in mind that at some point we will need to implement something like this.

So, the purpose of this article is show how you can do that. In this post we'll create a Google Cloud Function that runs periodically using an another Google Cloud product that is Google Cloud Scheduler.

For that to happen, we will basically use gcloud to communicate with our project and deploy our function. To write the function we will use the Python language, which is a very simple and incredible language.

I already wrote an article talking about Cloud Functions and Firebase, yes I really like Google Cloud products. In this article we created a function that sends e-mails. And we put things together a little this time. We will implement a function that sends emails periodically (I’m already seeing your newsletter system working).

So, here we go!

Step 1: Setting up

First of all you’ll need is a Google Account. With this access the Google Cloud Console and create a project.

Our project is happy-todolist. This isn’t a great name, I know. But for our purposes this is enough.

In addition you’ll need to configure a Billing Account. That is for get incoming. Without it you’ll can’t use the gcloud to deploy function and can’t use Schedulers, besides other Google Cloud solutions that are available.

Step 2: Creating our function

Once you already have a project with a Billing account attached to it you can now move your focus to writing your function.

Everyone knows that drink is so great for your health. It has many benefits to us. If you didn’t drink water regularly in a day you can present health problemas after.

Despiste all these benefits some people just forget to drink water. We need something to solve this problem, we need to alert people about drink more water along the day. Hummm, how to solve it?

You’re correct if you said “EUREKA: We need to a reminder”. Or someone like that. Who says “EUREKA” by the way. So, let’s create a reminder function.

Let’s create a file called main.py and inside of it let’s create a function called drink_water_now.

This function should be the following

The above function basically sends a email with the message “Hey, remember to drink water. It is very important for your health!!”. Yeah, is very simple and it all what we want, right?

For sending email we use Gmail. You can make it as simple as using unsecured apps in your Google Account or create an app password.

The purpose of this article is to show you how to make the function run periodically, so let’s focus on that and not the send_mail function.

You can check the full example code at https://github.com/geeksilva97/Medium/tree/master/google-cloud-function-with-scheduler and get more details at README.md

In the repository you can get more details about how teste it locally. Here we'll deploy the function to test it! So let's deploy our awesome function!

Step 3: Prepare to Deploy

One we wrote the function we can now send it to the Google Cloud. For this we'll use the Google Cloud CLI aka gcloud.

Make sure that CLI is installed. If doesn't you can install it following this official tutorial https://cloud.google.com/sdk/docs/install

After that, access the folder that contains your main.py file and run the following command

gcloud init

The above command will initialize a Google Cloud Project in the current repository (remember it should contains the main.py file)

Basically the CLI will ask you for authenticate and for project you have two options that are:

  • use an existing project
  • create a new project

If you choose the first option you will only need to select the desired project. If you choose the second one you will need to respond a question about the project and create them.

You can get more informations about gcloud init at gcloud init reference.

After create the project you can see the local config using the command

gcloud config list

You'll see something likes

[compute]
region = us-central
zone = us-central1-a
[core]
account = youremail@gmail.com
disable_usage_reporting = True
project = yourprojectid

It’s time. Let’s deploy our function!

Step 4: Deploy function as a Pub/Sub

The last step was the more hardest thing in this tutorial. You'll be convinced the this is true when you see the following command is the only needed thing to deploy your function

gcloud functions deploy drink_water_now --trigger-topic drink-water-now --runtime python37 --env-vars-file ./env-vars.yaml

I know that you thinking: "Whaaaaat? You told me it would be easy". But hey, let's get know every details at this command.

Below you can see the basic structure of this command:

gcloud function deploy functionName --trigger-topic theTopicName --runtime thenFunctionRuntime --env-vars-file pathToYmlFileWithVars

Get the details

  • functionName: the name of function. In this article our function name drink_water_now
  • theTopicName: the topic that will be called by Pub/Sub. You can define the name you prefer. Here we'll use drink-water-now.
  • theFunctionRuntime: the runtime to execute the function. In our example we using python37 (Python 3.7)
  • pathToYmlFileWithVars: the path where is the file with environment variables.

In your example we need to define some variables to be used to send email. Below you can see how should have the env-vars.yml:

EMAIL_USER: youremail@gmail.com
EMAIL_PASSWORD: yourpass

With this file the Google Cloud will define that variables in the environment automatically.

Once you ran the command to deploy the function you'll get the output when it worked normally

Deploying function (may take a while - up to 2 minutes)...done.availableMemoryMb: 256
buildId: 2c7d19d2-6e59-462f-a413-9293udd83
entryPoint: drink_water_now
environmentVariables:
EMAIL_PASSWORD: yourpass
EMAIL_USER: youremail@gmail.com

eventTrigger:
eventType: google.pubsub.topic.publish
failurePolicy: {}
resource: projects/happy-todolist/topics/drink-water-now
service: pubsub.googleapis.com
ingressSettings: ALLOW_ALL
labels:
deployment-tool: cli-gcloud
name: projects/happy-todolist/locations/us-central1/functions/drink_water_now
runtime: python37
serviceAccountEmail: happy-todolist@aspot.gserviceaccount.com
sourceUploadUrl: https://storage.googleapis.com/gcf-upload-us-central1-999999
status: ACTIVE
timeout: 60s
updateTime: '2021-01-15T22:10:45.831Z'
versionId: '3'

Great, now your function is deployed and you can see them in the Google Cloud Console at Cloud Functions menu item like the below image

Image 1 — List of Google Cloud Functions

Hoooooray. Our function is deployed! Let's create a job to call it periodically.

Step 5: Creating the job

Create the job in Google Cloud Console is a really simple task. Basically, in your console you should find the Google Cloud Scheduler (from docs: a fully managed enterprise-grade cron job scheduler) item at side menu and click on it.

You'll see a page like the following

Image 2 — List of jobs in Google Cloud Scheduler

This show a list of your jobs. As you can see in our project we already have two jobs. In your case this could be different, don't care about it.

If you click at the CREATE JOB button you'll be redirected to a form that will get informations about your new job. Likes the following image

Image 3 — Creating the job

You should inform the following:

  • Name: give a great and memorable name to your job
  • Description: give a more detailed description
  • Frequency: inform the frequency of the job in Unix cron format (see examples at crontab.guru).
  • Timezone: this is extremely important. A wrong timezone may cause errors in periodic execution.
  • Target: here you can choose one of three options that are, HTTP, Pub/Sub and App Engine. With HTTP you can call a endpoint in a specific request method, Pub/Sub is our case that our function is called by a topic and App engine is for Google Cloud App Engine applications.
  • Topic: this is field is showing only because our job is a Pub/Sub. Here we passe the topic that the job will call when is executing.
  • Payload: a payload that will be send to the function. It should be a valid JSON.

Pass all information and then click at CREATE button. And now your great job is created and prepared to execute periodically.

In the listing page you casse them one. This is so GREAT!!

Step 6: Appreciate the magic and never forget to drink water

You great is now working and now at every three hours you'll receive an email with a message reminding you to drink water.

But you are anxious, I know you don’t want to wait three hours to see your great function at work. I do not want too.

So in the list of jobs you can see some informations about your job, but you can see also a RUN NOW button. If you click this button your job will be executed right now! Try it!

I tried and the result is following

Image 4 — The result of job run

When the job runs it sends a Pub/Sub message with the topic drink-water-now to the function that runs and sends that e-mail and now I really keep hydrated.

Summary

That’s its folks! We create a wonderful function that helps people to remember to drink water. For this we used Google Cloud Function as a Pub/Sub but as you saw the Google Cloud Scheduler can run a http request too, so if you already have a task that need to run periodically but don’t want to write a Google cloud function, just put your URL and configure the request and everything should works fine!

We did a simple demonstration how to use it. I know that you mind is blowing right now. So, do it! Do great things, great scheduled jobs and important keep hydrated.

Please clap 👏👏 and share this post if it was useful to you. Thanks for reading. See ya!

References

--

--

Edigleysson Silva
Edigleysson Silva

Written by Edigleysson Silva

Trying to make stuff on my computer.

Responses (1)