Contents
Introduction to Cloud Functions
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
Now that deployment is complete I can click on the name. Here you can get more details by clicking on “Details”. You can view the code by clicking on Source. You should note that you cannot actually edit the code on this screen. To do that, you first need to click on the Edit button up here. This will then let you make any changes you wish, including editing the source.
You can view the trigger by clicking on the Trigger tab. Here is the URL that will execute my function. So if I click on the link (and zoom in a little bit) you can see that it is working. So this is how I will call my function. There is also a Metrics tab here to get me an idea of how often this function is getting called. It will tell me how fast it executes. How much memory it is using. And if there are any errors.
There is also a Logs tab that can give me more detailed information about each request. This is extremely useful for tracking down issues. As you saw, I was able to do a basic test by directly calling the URL. But what if I wanted to pass in some values? Remember this function will print any string that I pass in. Luckily, Google provides this Testing tab, to help you out.
I can first use this form to create a JSON object containing different values. My function is expecting a variable called “message”, so let me add that here. Once you have created a correctly formatted JSON object, you will see the “Test the function” button will become clickable. And if I scroll down, you can see the result.
I can also test my function by using the command line. I just have to copy this command, and because I have installed the Cloud SDK locally, I can paste it into my terminal. So as you can see, this tab makes it really easy to test out your new Cloud Function.
You have a couple different options for deleting the function. You can either select all the functions you wish to delete and then use this button. Or you can delete one function at a time by using the menu here. Alright, that covers all the main features of Cloud Function using the web console.
Next, I want to show you how to do all the same things, except using the command line. Most developers are not going to want to have to fill out forms every time they want to do a new deployment.
So here I have already created a basic Python Flask app. All this does is print out the string “Hello World!”. So, I mentioned that I have previously installed the Google Cloud SDK. And that means I can use this gcloud command to create a new Cloud Function based off of this code. Notice that the command is called “functions deploy”.
So in the command, you need to specify the name of the function. I am calling it “demo-1g-cmdline” because it is going to be a 1st gen function that I deployed using the command line. Now here is where you specify the language you will be using. In this case it is Python 3.9. And this sets the trigger type to HTTP. And this will allow anyone to call the URL without requiring any authentication. And this is what specifies the name of the function in my code to call when it is triggered.
Again, deployment takes a few minutes. But when it is finished you will get a bunch of information, including the trigger URL. So I can copy and paste this to verify that it is working. Ok, that looks good. Now if you forgot to save a copy of the URL, you can always get it again using this “functions describe” command. This provides the exact same information as you got before.
So now that I have deployed my function, I can still manage it using the web console if I wish. Here is my new function. Here are the details. And here is the source.
Updating the function from the command line is very simple. Just make whatever changes to the code you wish. And then run the exact same “functions deploy” command as you did before. And deletion is just as easy. You can use this command to accomplish that. Much like using the web console, it’s pretty simple to manage your functions from the command line.
Ok, so now you know how to create, edit and delete generation 1 Cloud Functions. Now I want to spend a little time going over generation 2. Second gen functions have some significant advantages. If I expand this table you can see that they allow better concurrency. You get a lot more trigger types. The timeout values can be much higher, especially for HTTP triggers. And generation 2 functions support multiple revisions and traffic splitting.
So the creation process for generation 2 is pretty similar. You still have to pick a name and a region. The trigger section looks a little different. Here you are automatically provided an HTTPS URL. And then you can add more triggers if you wish. Do notice that generation 2 does not support HTTP addresses. Only HTTPS.
Here (just like before) you can enforce authentication if you wish. And here you get a list of all the different trigger types that are available. Remember, at the time of this recording, generation 2 is still currently in beta. So they might add or change some of these in the future.
I want to point out that by default generation 2 uses the Compute Engine service account instead of the App Engine service account. This is important because the permission sets between the two are probably different. So if your code works fine as a Gen 1 function but not as Gen 2, then it might be because of this.
You can spot a few more differences in the advanced options. You will see that there are more options for memory. It looks like they are going to support up to 32 gigs. And you can see that there is also a custom option as well.
As I said before, the timeout can be much longer for 2nd gen. It can be up to 60 minutes for functions triggered by HTTPS. And up to 10 minutes for all the other triggers. The rest of these options are pretty much the same as Gen 1. So I am going to name this function “demo-2g-web”. And then I’ll hit “Next”. You will see that this page looks pretty much the same as last time. And so does the deployment process. Management is basically the same as well.
However, this next part is very, very interesting. When you create a Generation 2 Cloud Function, what it is actually doing behind the scenes is creating a Cloud Run service. Now I can prove that by opening up a new tab and going to Cloud Run. So even though I created a “demo-2g-web” function in Cloud Functions, you can see that it is also showing up here in Cloud Run. And if you look under the “Deployed by” column, you can see that it says “Cloud Functions”. So a Gen 2 Cloud Function is really just a Cloud Run service. And because of that, you can manage it like any other Cloud Run service. Here you can view the logs and metrics. Here I can look up the triggers and details. And here is the URL.
Now because this is actually Cloud Run, I also get some extra features as well. For example, here I can track previous revisions of my code. So you can use this screen to do things like rollbacks and even traffic splitting. Now this is all covered in more detail in the Cloud Run course, so I am not going to go through it in detail. But basically anything you can do with a Cloud Run service, you can also do with a Gen 2 Cloud Function.
Now you might be wondering why not just completely replace Cloud Functions with Cloud Run. Cloud Run requires that you create a whole container. Cloud Function does not. So creating a 2nd generation Cloud Function is still useful for people who do not want to develop their own containers.
Alright, so now you should understand how to create and manage both Gen 1 and Gen 2 Cloud Functions.
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.