Recently we found ourselves looking to automate some emails as part of our regular 360 degree feedback process. Our solution ended up using the SendGrid API, which we call from a Google Cloud Function. Here is the process we followed to get the very first iteration of this tool up and running:
In the below we’ll walk you through these in some more detail.
The first question to ask yourself is which language you want to use to write your function. Currently on GCP you have the following options:
We ended up writing our function in python. Even if you’ve never seen any python code before, you’ll see the below function is very straight forward. It uses the Sendgrid API client library to send a very simple email, from me to me.
import requests from sendgrid import SendGridAPIClient from sendgrid.helpers.mail import Mail, Email def sendgrid_email(event, context): message = Mail( to_emails="[email protected]", from_email="[email protected]", subject=f"REMINDER: DVELP Feedback", html_content=f""" <p>Hi team,</p> <p>Please remember to provide your feedback.</p> <p>Muchas gracias,</p> <p>Andi</p> """ ) sg = SendGridAPIClient() response = sg.send(message)
In order for this code to work, there are a few dependencies that are listed in the below
requirements.txt file. This file is the equivalent of a
package.json file in Node.js. When deploying a cloud function it is important to send this file along with your code, so that Google Cloud Platform knows how to configure your environment before running your function.
autopep8==1.4.4 certifi==2019.9.11 chardet==3.0.4 idna==2.8 pycodestyle==2.5.0 python-http-client==3.2.1 requests==2.22.0 sendgrid==6.0.5 urllib3==1.25.3
Last, but not least, you should add a
.gcloudignore file to your directory. This works in exactly the same way as a
.gitignore file: You can list the files you don’t want to deploy to Google and it will ignore them.
.gcloudignore .gitignore venv
venv here is the virtual environment folder, which is python’s equivalent to
node_modules. It contains a bunch of code that the cloud function wouldn’t use, so ignoring it speeds up deployment.
If you don’t have a GCP account already, you can try it free here.
To deploy a Google Cloud Function you can download the
gcloud cli tool from here.
Then, to log in to Google Cloud you enter the following command on the command line:
This will walk you through the sign in process for gcloud. The result is that you’re logged in on the right account and that the right Google Cloud Project is selected.
The command to deploy is:
gcloud functions deploy sendgrid_email --runtime python37 --trigger-topic feedback-reminder --set-env-vars SENDGRID_API_KEY=thisIsAnExampleSendgridApiKey
sendgrid_email is the name of our function. The
--trigger-topic option tells it that I want any messages in the
feedback-reminder Pub/Sub channel to trigger the function. And finally,
--set-env-vars lets me tell GCP that my Sendgrid API Key is
psst! it’s a secret!
Our function is deployed!
To make sure the function gets called regularly, we need to set up a schedule on: https://console.cloud.google.com/cloudscheduler
Clicking “Create job” shows you:
Click Create. Now the
feedback-reminder Pub/Sub channel will receive the message
go! at 9am on the 14th of every month.
After clicking Create you see:
Clicking “Run now” will trigger the function and, if you’re me, you’ll have the below email sitting in your inbox:
If this post got you thinking about opportunities for automating your communications and you could do with some help, please don’t hesitate to reach out. We’d be happy to advise you.