‫Welcome back.
‫Let's log in to Google Cloud console and try and create our first cloud function.
‫I'm 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 your server or a runtime environment.
‫OK, a long definition, but
‫I guess most of it made sense.
‫Let's go ahead and say, Create Function.
‫OK, let's wait for a function to be created.
‫Now, the first choice that you need to make when you are creating a cloud function is whether you'd
‫want to go for 1st gen or 2nd gen. As you can see right
‫now, 2nd gen is in preview, but I expect it to be live by the time you're watching this.
‫Now, what is the difference between 1st gen and 2nd gen. 2nd gen provides you with a lot of advanced
‫features.
‫If you use 1st gen,
‫and if you scroll down and choose runtime, build, connection, and security settings and go further down,
‫if you look at the timeout, which is default 60 seconds.
‫But if you hover over there, you can see that the maximum timeout for a cloud function of version V1
‫is 540 seconds.
‫So the maximum amount of duration for a specific invocation of a specific cloud function is only nine
‫minutes, and the maximum amount of memory that you can allocate is 8GB.
‫However, you would see that it would be a little different when you go for a 2nd gen.
‫So if I go for 2nd gen function,
‫you can see that you can configure up to 16 GB, and more importantly, if your cloud function is triggered
‫through HTTPS, then the maximum time outcan be up to 60 minutes.
‫You can see that this is a significant increase. From nine minutes,
‫we are going to 60 minutes. For now,
‫let's keep things simple as 2nd gen is still in preview.
‫I would want you to get a consistent experience, so let's go with 1st gen.
‫What we would do is configure the function name.
‫I'll call this my-first-cloud-function.
‫I'll choose the default region, which is suggested.
‫You can copy the URL so that we can send a request to it a little later.
‫Over here, you can also see that you can configure the trigger type; whether you'd want to trigger
‫it through HTTP or you would want to trigger it from Cloud Pub/Sub or from Cloud Storage; where do you want
‫to trigger it from.
‫I'll go for HTTP
‫and you can also configure how you'd want to authenticate requests.
‫For now, let's choose allow unauthenticated invocations.
‫I don't want to worry about HTTPS for now, so I would uncheck this box and I'd say, Save.
‫I'll minimize the runtime, build, connections, and security settings, and I'll click Next.
‫On next screen, you can configure the specific code that you would want to use to run your cloud function.
‫You can configure the runtime you would want to make use of. As you can see in here, .NET
‫is supported, Go, Java, Node, Python, PHP, Ruby;
‫a lot of languages are supported in here.
‫Let's choose the default one which is suggested, which is Node.js 16.
‫And as you can see in here, it says Cloud Build API is required to use the runtime selected.
‫So I would go ahead and say, Enable API.
‫I'll go ahead and click Enable in here.
‫Cloud Build is a CI/CD tool, continuous integration/continuous deployment tool
‫and it is used to deploy a cloud function.
‫And you need Cloud Build to be able to deploy a cloud function.
‫So you can see that the Cloud Build API is now enabled. So I'll close this window, go back to the old
‫window, and you'd see that there is no warning now. Sp,
‫over here in the inline editor, you can see a sample cloud function present. As you can see in here,
‫it takes some of the information from the request and creates a response based on that.
‫We don't really need to worry about what's in here. All that we need to say now is Deploy.
‫So this would create our first cloud function.
‫The most 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.
‫OK.
‫It took about a couple of minutes and the deployment was complete, and if I go into that specific function
‫now, 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 test the function
‫and you would see the response back from the function in here. If you'd want to actually add in logs,
‫what we can do is, go back to source
‫and I would say, 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 codes. 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. This would update the function and deploy the new version of the function.
‫And you would see that the URL that we used earlier would continue to work even while the function
‫is getting deployed.
‫OK.
‫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 percent of the requests were executed within 25 seconds.
‫99th percentile is 25.257 milliseconds.
‫You can see that 99 percent of the requests executed within 25.257 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 percent of the requests used less than 15.506.
‫50 percent of the requests made less than 15.144 MiB.
‫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 would 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 integrate 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
‫this step, we played around with cloud functions.