Sending SMS Text via Cloud Functions

30 minutes
  • 5 Learning Objectives

About this Hands-on Lab

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.
  1. From the main Google Cloud console navigation, choose APIs & Services > Library.
  2. Search for Cloud Functions API, and enable the service, if necessary.
  3. From the Library page, search for Cloud Build API and enable the service, if necessary.
Create the required bucket.
  1. From the main navigation, go to Storage > Browser.
  2. Choose Create bucket.
  3. In the Name field, enter a unique name for the bucket to hold the incoming image and click Continue.
  4. From the Choose where to store your data options, choose Region and select us-east1 as the region.
  5. Leave the remaining values at their defaults, and click Create.
Retrieve files from repo and configure.
  1. Activate the Cloud Shell.
  2. 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
  3. Change directory to the lab folder:
    cd content-gc-functions-deepdive/cloud-functions-sms-lab
  4. Open Shell Editor by clicking the pencil icon.
  5. Navigate to the cloud-functions-sms-lab folder, and open the two files there.
  6. 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]"
  7. Save the file.
Create Cloud Function.
  1. Navigate to the Cloud Functions dashboard.
  2. Click Create function.
  3. Apply the following settings:
    • Name: acg-sms-function
    • Trigger: Cloud Storage
    • Bucket: Your bucket name
  4. Click Save and then Next.
    • Runtime: Python 3.9
    • Source code: Inline editor
  5. From the Cloud Shell Editor, select all of the main.py code and copy it.
  6. In the main.py field of the function, paste the copied code.
  7. From the Cloud Shell Editor, open requirements.txt, and copy all.
  8. In the requirements.txt field of the function, paste the copied code.
  9. In the Entry Point field, enter send_sms.
  10. Click Deploy.
Test in Cloud Shell and Cloud Storage.
  1. In the Cloud Shell, issue the following commands:
    cd images
    ls
    gsutil cp a-cloud-guru.png gs://<BUCKET_NAME>
  2. Check your phone for an incoming text message.

Additional Resources

The company’s latest app includes a Cloud Storage bucket for in-house uploads. Your team leader wants to be notified of any new additions to the storage. You’ve been tasked to implement a Cloud Function that monitors the bucket and, when a new object is received, notify any number of people via an SMS text message with details of the object.

NOTE: In order to successfully complete this lab, you must have an active mobile phone and create an account with Twilio (either a free or paid plan).

You’ll need to accomplish the following steps to complete your task:

  1. Create Twilio account.
  2. Enable APIs.
  3. Create Cloud Storage buckets.
  4. Create and deploy Cloud Function.
  5. Upload image to initiate test.
  6. View results.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?