Google Associate Cloud Engineer – Getting Started with Google Cloud Functions
- Step 01 – Getting Started with Google Cloud Functions
Welcome back. Let’s get started with Cloud functions. What are Google Cloud functions or GCF? Imagine you want to execute some code when an event happens. A file is uploaded into Object Storage in Google Cloud, which is Cloud Storage, an error log is written to Cloud Logging, or a message is arriving on a queue which is Cloud pub sub. In all these situations, let’s say you want to run some code. When some event happens, you want to run some code. That’s where you can go for Cloud Functions. You can run code in response to events. You can actually write your business logic for the cloud function in NodeJS python, Go, Java, net and Ruby.
The great thing about Cloud Functions is you don’t really need to worry about servers or scaling or availability. You only need to worry about your code. Even if a million objects are uploaded to Cloud Storage, or a million messages coming to Cloud Pub sub, cloud Functions will be responsible for scaling up your code to handle the millions of requests which come in. So you don’t really need to worry about where this function is running. How do I scale it? How do I configure availability? You pay only for what you use. You pay for the number of invocations, you pay for the compute time of the invocations, and you pay for the amount of memory and CPU that is configured for each invocation.
Basically, you are paying for the execution time of the invocation and the resources that are made use of by a specific invocation. Each invocation of a Cloud function is time bound by default. You can go up to a max of 1 minute. You can also increase this max limit up to nine minutes or 540 seconds. So you cannot use Cloud Function to run a big bad job. For example, if the bad job takes hours to run, then you cannot do that with a Cloud Function. The great thing about Cloud Function is each invocation or each execution runs in a separate instance. So there is nothing that is shaped between invocations. In this step, we got a quick introduction to Cloud Functions.
- Step 02 – Understanding Google Cloud Functions – Important Concepts
Welcome back. In the step, let’s look at some of the important concepts related to cloud functions. An event is something that happened. So you uploaded an object to cloud storage. A Http call is made or a message arrived in Pub. Sub. So these are all events and Trigger is the response to an event with a function call. So which function to trigger? When an event happens? That’s the trigger. What do the functions do? The functions take the event data and perform the action that you want to perform. Events for cloud functions can be triggered from a variety of places object storage service in Google Cloud, which is called Cloud Storage cloud.
Pub. Sub, which is a popular queuing service in Google Cloud platform. Or you can also trigger it from Http calls. A Http call arrives and you can call a cloud function in response to a Http call. You can also trigger your events from Firebase, Cloud, Firestore and Stackdriver logging. The important concept to understand are events, triggers and functions. Event is something happening? Trigger is making sure that you respond to event with a function call and function is what gets the event data and performs the action. The functions can be triggered on events from a variety of places. I’m sure you’re having an interesting time and I’ll see you in the next step.
- Step 03 – Creating your first Google Cloud Functions
Welcome back. Let’s log into Google Cloud Console and try and create our first cloud function. I am in my first project, so make sure that you have switched over to My First project. If you have not deleted or shut down your Kubernetes project, make sure that you are switching over to your My First project and you can actually search for cloud functions. So cloud functions is what we are looking for. The cloud functions are getting enabled, so the first thing is the API should be enabled. So that’s what is happening right now. And as it says in here, Google Cloud Functions is a lightweight event based asynchronous compute solution that allows you to create small single purpose functions that respond to cloud events without the need to manage a server or a runtime environment. Okay, a long definition, but I guess most of it made sense.
Let’s go ahead and say Create function. Okay, let’s wait for a function to be created. So let’s get started with the configuration. So I’ll say my first cloud function as the name. So my first cloud function, you can create it in any region. It should not really matter the trigger, I would want to configure it as Http, so I would want to call a URL. So if there’s a URL, you can copy the URL if you want. And what I would do is for now, I don’t really want to worry about authentication and things like that. I would say I love Unauthenticated invocations, I don’t want to worry about Https as well, and I would want to say save. I don’t really want to worry about all the other settings. I’ll go ahead and say next. And the next thing that you need to choose is the language you want to use and the specific version.
Over here in the runtime, you can see that there are a variety of runtimes which are present dotnet, core, Go, Java, NodeJS. There are a number of versions of node JS and there is Python, three, seven and 38. You can choose any of these and the default template should be populated in here. And if you look at it in here, it says Cloud Build API is required to use the runtime selected. So what I would say is enable. API. So this would enable the Cloud Build API, because cloud build is needed. Cloud Build is a CI CD tool, and to deploy a cloud function you need to enable Cloud Build API. So let’s go ahead and say enable it and while it’s getting enabled, let’s go into our cloud function. So you can see that this default function which is present in here, it does not really do anything, it just gets the request and uses it to form a simple message and it would send it back as a response.
We don’t really need to worry about what’s in here. All that we need to do is say deploy. So this would create our first cloud function. The important thing about cloud functions is the fact that you only pay for it when there are invocations to it. So the deployment of our cloud function is in progress. Okay, it took about a couple of minutes and the deployment was complete.And if I go into that specific function, nav, I can see the details about this specific function over here. If I go over to testing, this is where you can actually test the function. So you can click test the function and you’d see the response back from the function in here. If you want to actually add in logs, what we can do is go back to source and I would say edit open up this trigger URL in another window.
So this is actually Oops, let’s click this. So this is another way you can actually invoke the function. So you can use this URL and you can see that Hello World is a response that is coming back. Now I would want to make a change in the code so I’ll say next. And over here what I would want to do is do a print to the log so console log and let’s print the message and let’s say deploy. So this would update the function and deploy the new version of the function. You would see that the URL that we used earlier would continue to work even while the function is getting deployed. Okay, it took about a minute and the deployment is now complete. So I’ll make a few invocations from here and let’s go back to the function.
Right now we have just created the function, so some of this data is not yet populated invocations per second. However, you can see some data about execution time. You can see that 95% of the requests were executed within 25 seconds. 99 percentile is 25. 25 seven milliseconds. You can see that 99% of the request executed within 25. 25 seven milliseconds. So you can get a little bit of information about the execution time. You can see how much memory is made use of. So 99% of the requests used less than 15. 56, 50% of the request made, less than 15. 44 MB. And you can see, you can see the number of active instances right now. You can see that there is just one instance which is active. And if there are any errors or things like that, you can look at them in here.
If you want to look at the logs, you can go in here and look at the logs as well. So we triggered a function a few times. So you can see our Hello World message printed in here. So this message is coming from cloud logging. Your cloud functions automatically integrates with cloud logging and you can see the logs in here. Similar thing will happen even if you go to testing and test it now. So if you go and if you go to testing and you test the function, what would happen? You’d see output and if you wait for a little while and scroll down, you should see also the logs related to that specific invocation. It took a minute or so and then I can see the logs related to that specific invocation in here in the step we played around with cloudform.
- Step 04 – Important Things to Remember – Google Cloud Functions
Welcome back. In this step, let’s look at a few things that you need to remember about cloud functions. No server management at all. You don’t need to worry about scaling, availability or anything of your function. You don’t need to worry about the servers at all. You write your code and let somebody call it, and it would be automatically scaled based on the number of calls coming into your function. Cloud functions automatically is spin up and back down in response to events. As the number of events come in, the number of instances of cloud functions automatically increase, and once there are no events to handle, the number of instances could go down automatically, so they scale horizontally. If there are hundred requests going on parallel, there are hundred separate cloud function instances that are handling the requests.
Cloud functions are recommended whenever you are responding to events. If you want to do something as soon as something is uploaded to a cloud storage bucket, or you’d want to respond to something on a queue, or you’d want to respond quickly to a Http call, you can go for cloud functions. However, cloud functions are not ideal for long running processes. If you have something like a Batch program which runs for hours, then cloud functions are not ideal. Maybe cloud functions may do a part of the Batch program amps work, but not the entire process. Cloud functions are time bound, as we discussed earlier, the default maximum limit is 1 minute, but it can go up to a max of nine minutes, which is about 540 seconds. In this quick section, we discussed about cloud functions.