image
Demo 1
Start course
Difficulty
Beginner
Duration
25m
Students
489
Ratings
5/5
Description

This course will demonstrate how you can create complex workflows by using existing Google Cloud Products and connecting them via simple, single-purpose functions. 

Learning Objectives

  • Understand the main features of Cloud Functions
  • Learn how to deploy a function
  • Learn how to set up triggers
  • Understand the differences between Gen 1 and Gen 2

Intended Audience

  • Cloud Architects
  • GCP Developers
  • Anyone preparing for a Google Cloud certification

Prerequisites

  • Access to a GCP account
Transcript

In this lesson, I am going to demonstrate how to use Google Cloud Functions. You are going to see how to deploy a function using both the web console and the command line. This is going to include how to set up the trigger, and well as how to test the function. And of course, I will also cover editing and deleting functions as well. Alright, let’s get started!

So first I want to cover the web console. So you are going to need to log in and then search for “Cloud Functions”. This will take you to the main Cloud Functions page where you will get a list of all your existing functions. Now, if you don’t have any functions created yet, then your screen should look like this.

To create a new function, just click on the “Create Function” button. So first, it’s going to do a quick check to verify that you have all the correct APIs enabled. If you don’t, it will display what APIs are missing and then give you the option to enable them. 

Next, you will need to fill out this form. Here you have to pick the environment, a name, region, and trigger type. 

The environment field can be set to either 1st generation or 2nd generation. First generation is the older version. Second generation is the newer version with more features. At the time of this recording, the 2nd generation environment is still in preview mode. And some features might be missing or subject to change. So I would not currently recommend using a 2nd generation Cloud Function in a production environment. Because of that, in this demo, I am going to pick “1st generation”.

Alright, next I need to pick a name. I am going to call this “demo-1g-web” since this will be a 1st generation function that was created using the web interface. And I then need to pick a region. If your function is going to be used with other Google services, then you probably want to make sure that the regions are all the same.

This next section allows you to set a trigger. A trigger is what determines when your function is called. By default, this is set to an HTTP call. So if you access this URL, then your function will be called. There are other trigger types available as well. I can create a trigger based upon publishing a message to a particular Pub/Sub topic. Or I can trigger my function whenever I perform certain actions on a Cloud Storage bucket. So like, when a file is archived or deleted. You can also trigger off events in Firestore. And they have recently added a few Firebase trigger types as well. I am going to stick with HTTP since that is probably the most common and it’s the easiest one to demonstrate.

Next, I need to set authentication. If you want to limit who can call your function, then you can pick the second option. But if you want to open it up to anyone to call it, then pick the first. Here I can choose whether I want to use HTTP or HTTPS. This means all data passed to the function will be encrypted. This is very important especially if you are going to be passing in sensitive parameters like passwords. And once you have finished configuring the trigger, you want to click on the Save button here.

Now at this point, you can click on the Next button to proceed. But I do quickly want to show you some of the extra options that are available as well. Here you can adjust the amount of memory that’s going to be allocated to your function. So I could bump this up the way to 8 gigs if I wanted. You can adjust the timeout value here. This is the maximum amount of time the function is allowed to run. By default, this is set to 60 seconds, but you might need to set this higher to give your function enough time to complete. Right now the maximum time is 9 minutes. That means any 1st generation function that tries to run longer than that is going to fail.

Alright, here you can set the Service account. This defines the set of permissions that your function will be provided. So if your function needs to access Cloud Storage or connect to a Cloud SQL database, you need to make sure those operations are permitted here. By default, it is going to use the App Engine default service account. But you can pick another account or create a new one with custom permissions.

Down here a little further you can set values for autoscaling. Leaving the minimum instances at zero is going to save you money. If no one is calling your function then you will have no instances and thus no charges. However, creating new instances does take time. This can cause delays when responding to increased loads. So if performance is more important than cost, you might need to bump this number up. You can also set a cap on the maximum number of instances that are allowed. This can be used to make sure that your spending cannot exceed a certain threshold, no matter how many requests.

There are several other options available. I’m not going to go through everything. You have the option to control what sort of traffic to allow. If you need to pass in sensitive parameters, you can use secrets instead of environment variables. And you can choose where to store your compiled code. Alright, so remember, the main things to pick are: the environment, the name, the region, and the trigger. Once you are happy with your selections, click on Next. Here you get a second screen where you can upload your code. So first you need to specify the language. You can choose Go. You can choose Java. There is Node.js, PHP, Python, and Ruby.

After picking the language, you are going to notice that Google will provide a default “Hello world” example. This is helpful because it gives you a basic idea of how to structure your code. And you can either use the inline editor if your function is going to be small and relatively simple. Or, you can upload a ZIP file for something more complicated. You can also pull code from a preexisting Cloud Source repository if you wish.

I am just going to use the default Hello World example. What this does is print out whatever string that you pass in. And then if you don’t pass anything in, it will print the default string “Hello World”. Once you have picked a runtime and selected your code, then you can click on Deploy to actually create your function.

Deployment takes a little while to complete. So while we are waiting, we will review the options that I picked. Remember, I created a 1st gen Cloud Function. It’s written in Python and it is going to be triggered by making an HTTP request.

About the Author
Students
37386
Courses
44
Learning Paths
16

Daniel began his career as a Software Engineer, focusing mostly on web and mobile development. After twenty years of dealing with insufficient training and fragmented documentation, he decided to use his extensive experience to help the next generation of engineers.

Daniel has spent his most recent years designing and running technical classes for both Amazon and Microsoft. Today at Cloud Academy, he is working on building out an extensive Google Cloud training library.

When he isn’t working or tinkering in his home lab, Daniel enjoys BBQing, target shooting, and watching classic movies.

Covered Topics