Skip to main content

Google Cloud Functions Deep Dive


Intro Video

Photo of Joseph Lowery

Joseph Lowery

Google Cloud Training Architect II in Content







Hands-on Labs


Course Details

Google Cloud Functions is a serverless, event-driven, managed platform for building and connecting cloud services. It’s a code-centric service where the functions you write can be triggered by an HTTP request or any number of cloud events—both on and off Google Cloud. At the start of the course, I’ll thoroughly explain what a serverless, event-driven, managed platform for building and connecting cloud services actually means—and what it’s capable of. We’ll cover Cloud Functions’ primary features and benefits - one of the most compelling of which is its flexibility. A fact attested to by the wide spectrum of use cases we’ll discuss. Then, we’ll dig a little deeper to reveal what makes a Cloud Function function, including a full discussion of the different types of Cloud Functions along with their component aspects: events and triggers. I’ll even go over the pricing of Cloud Functions, something everyone involved in related projects should be aware of. After you’re thoroughly familiar with the overall structure of Cloud Functions, we’ll begin exploring working with them. I’ll show you how to set up a proper development environment – whether you’re on a Mac, Windows, or Linux system – and get your first function deployed right out of the gate. Then, we’ll dive into the particulars of coding functions, specifically in Python: how to code for specific scenarios, like working with JSON variables, or particular situations, such as responding to a trigger from an app in another domain. I’ll give you all the command line code you’ll need for deploying your Cloud Functions, complete with full coverage of the available parameters. We’ll also discuss deploying Cloud Functions from a variety of sources, including repositories like Github. Cloud Functions can be triggered through a variety of methods and one of the most common is to use another Google Cloud service. We’ll discuss how to handle those most frequently relied on, among them Cloud Pub/Sub and Cloud Storage – as well as some of the more targeted services like Cloud SQL and Stackdriver. Testing is also critical in any app development and Cloud Functions is no different. We’ll examine several relevant testing strategies along with a look at implementing a CI/CD workflow with Cloud Functions. The final section of the course really takes off with an in-depth look at a number of different real-world scenarios. These use cases range from retrieving queried data from Cloud SQL, tying together four different Google Cloud services to extract and translate text from images, and integrating with a third-party service to send a text message anywhere in the world – all triggered by your Cloud Functions. I’m really excited to show you all of what Cloud Functions can do and exactly how to do it. It’s a really solid solution for an ever-increasing number of use cases and an excellent tool to have in your cloud computing tool chest. So, come on, let’s get started with our Google Cloud Functions Deep Dive. Download The Function Flow here:


Getting Started

Course Introduction


Lesson Description:

In this lesson, you’ll learn the basics of the Cloud Functions service: what it is, its key features, as well as its primary benefits. You’ll also see simple code examples and a wide range of use cases to help you grasp the possibilities of this important Google Cloud service.

About the Course Author


Lesson Description:

Meet the training architect of this course, Joseph Lowery. Joe has been working with Google Cloud for over five years, transitioning websites to the cloud via App Engine, Compute Engine, Cloud Storage, Cloud Datastore, and other services. He's Linux Academy's training architect for Google Cloud Essentials, Google Kubernetes Engine Deep Dive, and Google App Engine Deep Dive.

Using the Interactive Diagram


Lesson Description:

The Interactive Diagram for this course is a tool that provides both an overview and a detailed breakdown of all the Cloud Functions services covered. You'll see it incorporated throughout the course. Access the link to the Interactive Diagram below.

Course Features and Tools


Lesson Description:

This course, like all Linux Academy courses, is more than just a series of video lessons. Here, I'll detail the other online and offline features available for students.

Overview of Cloud Functions

Introduction to Google Cloud Functions


Lesson Description:

In this lesson, you’ll learn the basics of the Cloud Functions service: what it is, its key features, as well as its primary benefits. You’ll also see simple code examples and a wide range of use cases to help you see the possibilities of this important Google Cloud service. ### Basic Cloud Function in Python 3.7 ```sh def hello_get(request): """HTTP Cloud Function. Args: request (flask.Request): The request object. Returns: The response text, or any set of values that can be turned into a Response object using `make_response` """ return 'Greetings from LA Academy!' ``` ### Basic Cloud Function in Node.js ```sh /** * HTTP Cloud Function. * @param {Object} req Cloud Function request context. * @param {Object} res Cloud Function response context. */ exports.helloGET = (req, res) => { res.send('Greetings from LA Academy!'); };```

First Look: Cloud Functions, Events, and Triggers


Lesson Description:

You’ve learned the basics of cloud functions, now it’s time to delve into the details. In this lesson, I’ll explain what the different types of cloud functions are, which events are supported, and how triggers work. We’ll also examine methods for securing access to your cloud function as well as the specifics of the execution environment.### HTTP Function with Arguments ```sh def greetings_http(request): request_json = request.get_json(silent=True) request_args = request.args if request_json and 'name' in request_json: name = request_json['name'] elif request_args and 'name' in request_args: name = request_args['name'] else: name = 'my friend' return 'Greetings from Linux Academy, {}!'.format(name) ```  ### Background Function example (Cloud Storage)``` def hello_gcs(data, context): """Background Cloud Function to be triggered by Cloud Storage. Args: data (dict): The dictionary with data specific to this type of event. context ( The Cloud Functions event metadata. """ print("File: {}.".format(data['objectId'])) ```

Managing Costs and Quotas


Lesson Description:

One of the key benefits of Cloud Functions is that you only have to pay when your function is running; there are no on-going server fees when the service is idle. However, there are certainly costs involved and, in this lesson, you’ll see exactly how those charges are calculated. After breaking down the details, we’ll run through a number of scenarios using the Google Cloud Pricing Calculator. In addition to covering managing the costs of your Cloud Functions, we’ll explore the applicable quotas. As a Cloud Functions developer, it’s important for you to understand the imposed limits as well as the costs incurred.Link in this lesson:

Setting Up for Cloud Functions

Section Introduction


Lesson Description:

We’re ready to move into actually working with Cloud Functions. This lesson quickly covers all the topics covered in this section: everything from setting up your development environment to integrating Cloud Functions in a CI/CD workflow.

Utilizing the Console and Shell


Lesson Description:

Back in the day, cloud computing was primarily accomplished through the command line. typing command after command. Google Cloud has expanded the possibilities by providing a robust console where many Cloud Function management chores can be handled. In this lesson, I’ll show you how to get started creating a Google Cloud Function, including all the necessary preliminary steps. We’ll also cover how to access the system’s command line in the sky, the Cloud Shell.### **Deploying****Python**```gcloud functions deploy la-function-1 --runtime python37 --trigger-http```**Node.js 6**```gcloud functions deploy la-function-1 --runtime nodejs6 --trigger-http```**Node.js 8**```gcloud functions deploy la-function-1 --runtime nodejs8 --trigger-http```### **Testing** ### ```gcloud functions describe la-function-1```** Returns (where region/zone is us-central1 and project is la-cf-test) **``````

Establishing a Local Environment


Lesson Description:

In this lesson, we’ll turn our attention inward—away from the Google Cloud console and Cloud Shell to creating a local development environment. Setting up a system to code your Cloud Functions in Python requires a good number of steps, and we’ll cover each one, for all three operating systems: Mac IOS, Windows, and Linux. In addition, I’ll show you how to set up a virtual environment, so your projects can be isolated from each other and you can implement just the necessary client libraries required. Links in this lesson: and  ### MacOS ### Compiler required:```xcode-select --install```Install Homebrew (see home page)Install python 3```brew install python@3 python```Install virtualenv then set up and activate per project```sh pip3 install --upgrade virtualenv cd [project dir] virtualenv --python python3 env source env/bin/activate ``` ### Windows Download installer for both Python 2 and 3 from python.orgRun Python Launcher for Windows for Python 3Install virtualenv then set up and activate per project```sh pip install --upgrade virtualenv cd [project dir] virtualenv --python "c:python37python.exe" env ``` ### Linux Install appropriate packages for your distribution.For Debian and Ubuntu:```sh sudo apt update sudo apt install python3 python3-dev ``` Install pip```sh wget sudo python ``` Install virtualenv then set up and activate per project```sh pip install --upgrade virtualenv cd [project dir] source env/bin/activate ```

Working with Environment Variables


Lesson Description:

Your Cloud Function goes through a variety of stages in its life: development, testing, production, and others. Rather than code and deploy completely different functions for each of these iterations, use environment variables to configure a single function accordingly. In this lesson, I’ll show you how to create, manage, and deploy environment variables for Cloud Functions. I’ll also cover the limits and reserved variables you should be aware of.

Coding Cloud Functions

Crafting HTTP Functions


Lesson Description:

HTTP requests—the basis for HTTP Cloud Functions—come in a wide variety of multi-dimensional flavors, capable of passing variables in a number of ways via a selection of content types. In this lesson, I’ll show you how to code your Cloud Functions to handle any scenario. Additionally, we’ll take a look at the technique required for Cross-Origin Resource Sharing (CORS) so that you can send an HTTP request to your function from a different domain. We’ll close the lesson by examining the additional library and functions you’ll need to work with the multipart/form data content type for transmitting form values as well as uploaded files. ### HTTP POST with variable example ```sh def greetings_http(request): request_json = request.get_json(silent=True) request_args = request.args if request_json and 'name' in request_json: name = request_json['name'] elif request_args and 'name' in request_args: name = request_args['name'] else: name = 'my friend' return 'Greetings from Linux Academy, {}!'.format(name) ``` ### HTTP detect content-type example```sh from flask import escapedef greetings_content(request): content_type = request.headers['content-type'] if content_type == 'application/json': request_json = request.get_json(silent=True) if request_json and 'name' in request_json: name = request_json['name'] else: raise ValueError("JSON is invalid, or missing a 'name' property") elif content_type == 'application/octet-stream': name = elif content_type == 'text/plain': name = elif content_type == 'application/x-www-form-urlencoded': name = request.form.get('name') else: raise ValueError("Unknown content type: {}".format(content_type)) return 'Greetings from Linux Academy, {}!'.format(escape(name)) ```

Developing Background Functions


Lesson Description:

You know by now the difference between HTTP and background Cloud Functions. In this lesson, we’ll ramp up your knowledge of background functions as we discuss the two necessary arguments, core requirements, and the best way to handle retrying triggering such functions.

Deploying Cloud Functions

Deploying from Your Development Environment


Lesson Description:

Once your code is developed, you’ll want to deploy it to Google Cloud for testing and, eventually, live execution. In this lesson, I’ll show you how to deploy your Cloud Function from your local Python development environment. While very straight-forward, there are a couple of key aspects and flags you’ll want to be aware of, so be sure to watch the lesson completely.

Deploying from the Console or a Repository


Lesson Description:

Not all code comes from a single developer’s system, so the previous lesson on deploying Cloud Functions from your local development environment won’t always serve. This lesson demonstrates a couple of additional pathways at your disposal: the console and source code repositories. Each is useful: the console is extremely quick and straight-forward while the connection to a repository opens up the world of collaborative coding. Because of the way the latter method works, I’ll introduce you to the Google Cloud Source Repository feature, if you’re unaware of its existence and capabilities. All roads lead to deploying your Cloud Functions, so let’s get started down these new paths.

Triggering Cloud Functions

Calling Triggers Directly


Lesson Description:

You can think of triggers as the connecting glue between events and Cloud Functions. This lesson will show you how to trigger your Cloud Functions directly, a critical technique when developing and testing your work. I’ll show you several approaches, from the command line and the console.

Triggering Functions via Cloud Pub/Sub


Lesson Description:

You may be familiar with Cloud Pub/Sub from its popular use as a publish-once-to-many-subscribers messaging model, but it is also capable of another, more specific target: a Cloud Function. In this lesson, I’ll explain how messages and other data in Cloud Pub/Sub topics can be conveyed to a Cloud Function for processing and application. We’ll look at both a console approach as well as a direct trigger mechanism.

Establishing Cloud Storage Triggers


Lesson Description:

Cloud Storage is one of those Google Cloud services that most everyone who uses the platform is familiar with—and that’s because it provides such an essential service: object storage. Cloud Storage’s ability to handle almost any size file, whether it’s an image, video, document or other type of file, is enhanced by its Cloud Function connection. This lesson will explain all the different ways you can use Cloud Storage to trigger Cloud Functions as well as provide an example of a working function.

Accessing Cloud SQL


Lesson Description:

Cloud SQL is Google Cloud’s fully managed service for MySQL and PostgreSQL databases and, as such, is very much in demand for small to medium businesses and organizations. While you cannot directly trigger a Cloud Function from Cloud SQL, you can use a Cloud Function to manipulate a Cloud SQL instance: everything from retrieving the results of a query to creating a complete database from scratch. However, to do anything with Cloud SQL, your Cloud Function will need to establish a local socket connection. In this lesson, I’ll detail the code necessary and provide you with a working example. We’ll also cover best practices for integrating Cloud Functions and Cloud SQL.

Hands-on Labs are real live environments that put you in a real scenario to practice what you have learned without any other extra charge or account to manage.


Tracking Cloud Functions

Integrating Stackdriver Logging and Monitoring


Lesson Description:

In modern cloud computing, keeping track of your operations for analysis and debugging is almost as important as developing the applications in the first place. Google Cloud’s Stackdriver suite of services monitors all cloud-based services and provides easy-to-understand charts in a series of customizable dashboards. In this lesson, we’ll look at how Stackdriver Logging and Stackdriver Monitoring interface with Cloud Functions to provide vital insights into the service. In addition, I’ll show you how to trigger a cloud function through Stackdriver Logging via a method called Second-Party Triggers.

Reporting Errors to Stackdriver


Lesson Description:

Stackdriver is not just useful for logging events and tracking metrics; it also includes a robust debugging tool, Stackdriver Error Reporting. In this lesson, I’ll show you a couple of methods you can rely on to send errors directly to this service—both with and without function termination—as well as expand on code for logging errors.

Testing and Developing Cloud Functions

Running Cloud Function Tests


Lesson Description:

Testing is one of those no-way-around-it realities of all code development: you just have to test your code fully before pushing it live. The best way to test code is to do it early and often. In this lesson, I’ll show you how to apply that maxim to Cloud Functions. I’ll explain three different types of testing and show you sample code and examples for each, covering three basic triggers: HTTP, Cloud Pub/Sub, and Cloud Storage.

Using Cloud Build for CI/CD with Cloud Functions


Lesson Description:

Many organizations have adopted a CI/CD (Continuous Integration/Continuous Delivery) pipeline to maintain coding efficiency. Cloud Functions work well in a CI/CD environment and can be implemented as such through a Google Cloud service, Cloud Build. I’ll show you how to set up your Cloud Functions to integrate Cloud Build’s functionality and features so you and your team can jump in the CI/CD pipeline.

Section Conclusion and Summary


Lesson Description:

After working your way through all of the lessons in this section, it’s a good idea to stop and review some of the high points before moving on. Listen to this brief recap of the lessons in this section to refresh and solidify your memory.

Use Case: Extracting Data

Section Introduction


Lesson Description:

This brief lesson describes the three Cloud Function use cases that will be explored in this section.

Use Case Overview


Lesson Description:

This lesson will provide an overview of the use case detailed in this section. I’ll give you some insight into the database used and detail the steps we will take—from Cloud SQL to Cloud Functions—to bring this scenario to life.

Pulling Data from SQL Queries


Lesson Description:

Time to get down to details, with explicit instructions and demonstrations of constructing our use case. In this lesson, we’ll first walk through the creation and population of a Cloud SQL database instance, describing the data used and showing how we import it. Next, we go over the Cloud Function code, line-by-line, and provide an in-depth explanation of all the functions. Finally, we undertake testing our function in a variety of scenarios.

Use Case: Gathering and Converting Visual Information

Use Case Overview


Lesson Description:

The use case described in this lesson is a somewhat complex one. Be sure to watch this lesson to get a clear picture of the goal, the services employed, and the workflow incorporated.

Translating Text from Images


Lesson Description:

Here’s a very full-featured use case that uses not one, not two, but three separate Cloud Functions to handle a robust app. In order to pull off the stated goal—extracting text from images and translating the text into five languages—we’ll need the help of four other Google Cloud services. Come on in, the coding’s fine.Link used in this lesson:

Use Case: Delivering Text Messages

Use Case Overview


Lesson Description:

Have you ever wanted to automatically alert someone via a text when a web or cloud event takes place? If so, this use case is for you. In this lesson, we’ll get an overview and workflow of what it takes to use Cloud Functions to send text messages.

Delivering Text Messages


Lesson Description:

In this lesson, I’ll go through every element necessary to set up your cloud function to send an SMS text message when triggered. I’ll cover all the preliminary steps—describing how the Twilio service works, as well as the App Engine application used—and then move on to the Python code used in the function. Once it’s all done, I’ll provide a real-time demonstration of the working project.

Section Conclusion and Summary


Lesson Description:

Now that you’ve completed the lessons in this section, take a brief moment to review the topics covered as well as a number of the specific insights shared.

Final Steps

What's Next?


Lesson Description:

Stop by for a final thank you and some suggestions as to what you might be interested in pursuing next.

Take this course and learn a new skill today.

Transform your learning with our all access plan.

Start 7-Day Free Trial