Sending SMS Text via Cloud Functions

Hands-On Lab

 

Photo of Joseph Lowery

Joseph Lowery

Google Cloud Training Architect II in Content

Length

00:30:00

Difficulty

Beginner

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.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Sending SMS Text via Cloud Functions

Introduction

In this hands-on lab, 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.

How to Log in to Google Lab Accounts

On the lab page, right-click Open GCP Console and select the option to open it in a new private browser window (this option will read differently depending on your browser — e.g., in Chrome, it says "Open Link in Incognito Window"). Then, sign in to Google Cloud Platform using the credentials provided on the lab page.

On the Welcome to your new account screen, review the text, and click Accept. In the "Welcome L.A.!" pop-up once you're signed in, check to agree to the terms of service, choose your country of residence, and click Agree and Continue.

Solution

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).

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 globally unique name (e.g., “la-image-to-sms-” with a series of numbers at the end, like “8675309").
  4. Set the Default storage class to Regional.
  5. Leave the remaining values as their defaults and click Create.

Retrieve Files from Repo and Configure

  1. Activate the Cloud Shell by clicking the icon in the top row.

  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
    • 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, delete the existing code and paste in the copied code.
  6. From the Cloud Shell Editor, open requirements.txt, and copy all.
  7. In the requirements.txt field of the function, delete the existing code and paste in 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, change directory:

    cd images
  2. List the contents:

    ls
  3. Copy the LA-logo.png file:

    gsutil cp LA-logo.png gs://<BUCKET_NAME>
  4. Check your phone for an incoming text message.

Conclusion

Congratulations on completing this hands-on lab!