Cloud Functions are, in a way, the glue between services. These services are often other ones on Google Cloud – but they don’t have to be. This hands-on lab will explore integrating Cloud Functions with one of them: Twilio, a web service API for SMS, voice, and other communications. With the help of this external service, we will set up a Cloud Function that, when triggered by the upload of an object to a Cloud Storage bucket, sends a text that alerts one or more recipients of the action taken — complete with details about the object.
Note: Because of the Twilio limitations, this lab will only work with telephone numbers from the U.S. and Canada.
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- Enable necessary APIs and services.
- From the main Google Cloud console navigation, choose APIs & Services > Library.
- Search for Cloud Functions API, and enable the service, if necessary.
- From the Library page, search for Cloud Build API and enable the service, if necessary.
- Create the required bucket.
- From the main navigation, go to Storage > Browser.
- Choose Create bucket.
- In the Name field, enter a unique name for the bucket to hold the incoming image and click Continue.
- From the Choose where to store your data options, choose Region and select us-east1 as the region.
- Leave the remaining values at their defaults, and click Create.
- Retrieve files from repo and configure.
- Activate the Cloud Shell.
From the Cloud Shell, issue the following command to clone the repository for this course:
git clone https://github.com/linuxacademy/content-gc-functions-deepdive
- Change directory to the lab folder:
cd content-gc-functions-deepdive/cloud-functions-sms-lab - Open Shell Editor by clicking the pencil icon.
- Navigate to the cloud-functions-sms-lab folder, and open the two files there.
- In the
main.py
file, make the necessary updates to the variables starting on line 7:TWILIO_ACCOUNT_SID
="[YOUR_TWILIO_ACCOUNT_SID]"
TWILIO_AUTH_TOKEN
="[YOUR_TWILIO_AUTH_TOKEN]"
TWILIO_NUMBER
="[YOUR_TWILIO_NUMBER]"
TO_NUMBERS
=['ARRAY_OF_NUMBERS']
BUCKET
="[BUCKET_NAME]"
- Save the file.
- Create Cloud Function.
- Navigate to the Cloud Functions dashboard.
- Click Create function.
- Apply the following settings:
- Name: acg-sms-function
- Trigger: Cloud Storage
- Bucket: Your bucket name
- Click Save and then Next.
- Runtime: Python 3.9
- Source code: Inline editor
- From the Cloud Shell Editor, select all of the
main.py
code and copy it. - In the main.py field of the function, paste the copied code.
- From the Cloud Shell Editor, open
requirements.txt
, and copy all. - In the requirements.txt field of the function, paste the copied code.
- In the Entry Point field, enter send_sms.
- Click Deploy.
- Test in Cloud Shell and Cloud Storage.
- In the Cloud Shell, issue the following commands:
cd images ls gsutil cp a-cloud-guru.png gs://<BUCKET_NAME>
- Check your phone for an incoming text message.
- In the Cloud Shell, issue the following commands: