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.
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.
  4. From the Default storage class options, choose Regional.
  5. Leave the remaining values as 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: la-sms-function
    • Trigger: Cloud Storage
    • Bucket: Your bucket name
    • Source code: Inline editor
    • Runtime: Python 3.7
  4. From the Cloud Shell Editor, select all of the main.py code and copy it.
  5. In the main.py field of the function, paste the copied code.
  6. From the Cloud Shell Editor, open requirements.txt, and copy all.
  7. In the requirements.txt field of the function, paste the copied code.
  8. In the Function to execute field, enter send_sms.
  9. Click Create.
Test in Cloud Shell and Cloud Storage.
  1. In the Cloud Shell, issue the following commands:
    cd images
    ls
    gsutil cp LA-logo.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.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


$2,495.00

Checkout
Sign In
Welcome Back!

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