Google Cloud Functions - Serverless in GCP

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back so let me start with a simple into the equation you have a small code a small piece of code and you want to execute that code without creating an infrastructure for example the code is to write a simple function write the function it could be written in a small HTML page or it could be you know doing an ETL processing it could be anything so you have to write that code without worrying about infrastructure with minimal cost and it should be able to should be able to get it done as fast as you can now let's say you got this question in interview if you are experienced in the cloud computing the first thing that you'll get in your mind that the interviewee is talking about something called Savalas technology so it's not a technology as such it's a paradigm shift in cloud computing where developers will focus just on writing the code without worrying about the infrastructure now if you remember in my last video I did cover about the app engine which is one you know service in GCP that talks about sour lists where you create you know your application without worrying about the infrastructure now in this video we're going to cover about cloud function so cloud function is another server less compute in GCP so the main difference between the App Engine and cloud function is that in App Engine you can create the whole application and you can create multiple functions inside that application but when when compared to cloud functions let's say in cloud functions you would create smaller functions as a service let's say you have a micro services services based architecture and you want to create smaller functions each service by itself is a function or you know one key word that you will remember more often that is tied with server Desi's function of the service which means that every function that you write is a service so if you have such kind of requirement then you would go with cloud functions whereas you want to deploy your whole application dealing with multiple functions then you would go with App Engine the both are several is compute services in in GCP now remember this this is very important for associate exam as well they might ask you to choose the right service for the right use case no other other important difference is that let's say that you want to deal with only Python or node or go then you have to use cloud functions because cloud functions today as of now supports only three languages whereas if you want to you know use your own language like Java or anything else then you would go with you know App Engine so that is the second key difference because App Engine as we discussed in my previous video it has standard as well as flexible in standard it supports only few languages but inflexible if you if you want to use your own runtime then you can go with the containers for App Engine flexible the third key difference is the cloud functions has a specific time like for example the function can execute only for 9 minutes so beyond that the function will timeout because the way it is designed even if you see a double s lambda which is equivalent to cloud functions which is their server less service it executes only for 15 minutes max or they might increase the time in future but currently it exists only for 50 minutes similarly in GCP the cloud functions excuse only for 9 minutes so your micro service has to be so small that it can execute only for 9 minutes or beyond that it will timeout so if you have such applications or such services that run only for 9 minutes then you would use cloud functions but in the case of App Engine you can use more time so this is one key difference the third three differences so one difference is you know the runtimes it supports loss function supports only small small runtimes like three and then second difference is the time nine minutes versus you can have more time in App Engine the third difference is is that load function is a small function a service unlike App Engine where you can have your whole application deployed so in this video I am going to cover the cloud functions we are going to deploy a simple cloud function understand the cloud functions and then I'm going I am going to create a simple website like a simple HTML website using cloud functions so cloud function supports events and context so one thing that we want to understand in cloud functions is that when you create a function you will have a specific event let's say that you know you have puts put an object in in a cloud storage right so that's an event so based on that event you can trigger a function to do specific things on on top of the event or you can also trigger the function using HTTP in DCP so that's that's like a normal request using flask where you can send a request and you know you can post or put or delete as a request for HTTP or HTTPS and then you will get a response and the other type is using event in context so think of cloud functions as a glue inside GCP that talks to different different or multiple DCP services so that's one key advantage of using cloud functions when compared to any other service or even for that matter lambda in AWS now plot functions can trigger based on any events like like I discussed there are multiple events like pub/sub is one event and and Google Cloud Storage is another event and I will show you the list of events in in the GCP console so with that let's dive into the demo and see more about how to create a cloud function and how to trigger a cloud function see you there all right guys so right now I'm in the DCP console I'm going to the Google cloud functions as you see I don't have any function right now so I'm going to create a new function create function and you can name the function anything that you want I'm gonna just leave it by default so as we discussed you know the main important thing that we have to decide is how we want to trigger the function either you can choose the HTTP or you can choose the cloud pub/sub cloud storage cloud storage is like you know if you put an object inside the cloud storage if you want to trigger this function based on that event then you can use cloud storage so there are many use cases that most companies use for cloud storage even in Ada bliss when you use s3 and lambda so that combination is a most commonly used used you know use case for using serverless cloud functions or lambda functions so in in for example here let's say that whenever there is an object that that's created or vary whenever there is an object that got created inside you know GCP cloud storage you want to trigger this function and then you want to do a specific action on that object like delete the object or you know renaming the object or it could be anything so when you want to do that then you would click Cloud Storage and similarly Cloud five stories for the database actions and there are other actions so for this you know tutorial I would just focus on HTTP and then we can talk about cloud storage in in the next video so I will select HTTP and this is the HTTP URL of my function this name will change based on the function name here and I'll leave this as default because we won't execute this function from internet so either you can upload your code using an inline editor or you can zip upload the whole code and and because our code is just the example code that you see here using the node.js I'm not going to upload anything as a zip file I'm going to leave this as default whatever we see here and the most important thing that we discussed in the first part of the video is the different types of runtimes so function support Co node and then Python right so there are only three runtimes available and this is important tip for the exam so I'm going to choose Python I'm more comfortable with that and this is the default code as you can see this is the function and the request so the request is something that you want to send from you know when you make a post call to this HTTP URL and the thing that you know we want to remember here is that the function hello world is the function to execute so whatever you define here is the one that's going to be executed inside so let's say that you want to rename this to hello then you would rename this to hello here right so this is important to remember and then the other important parameters are the the memory allocated for example if you want to execute this function and this function has lot of memory usage you know this function runs inside inside some container which we don't have any access to so that's all that's the beauty of server listen you don't have to control your infrastructure right so we don't know where the exact very exactly the function is running so but for instance if your code demands more memory you can increase the memory and the max memory you can allocate is 2gb so the region again you know where do you want to run this function based on your latency and your application you can you can define the region so timeout is 60 seconds but I'm assuming you can increase that and then so there is a nine minute I think I can you can check on the documentation but but the maximum time what is nine nine minutes so you we have to convert that into seconds and you can put that here as a time mode all right so this is like the concurrency right so for example you know when you call this function there will be one container with a transient at the background but if you want to call this function multiple times at the same time you know you want to increase the concurrency of this function then you can specify the maximum function instances here and VPC character is something that you want to use for example if your function runs within a private network fun or if you just ignore that we can discuss about that in the future videos but for the example of you there's not needed okay so these are all the settings now I'm going to create the function there's going to take a while and then you can see the trigger as HTTP another runtime is 3.7 and the memory is 256 MB keep this video while getting created okay so no it got created now I can test the function by clicking on test function here test function and if you do not specify the event in the source code if you remember it is just like it will return hello world so I I just click on test function then it returns hello world if you want to specify anything else you know you can specify that in the JSON format and hear the message okay so I want to all right now if I test the function you can see that I got this output now one question you might have in your mind is how would I do bug this function if there is an issue with the function or you know what's going on in this by default the function locks are stored in the stack driver logging so we can look for that locks by clicking on the view locks here and you can see everything related to the function that code executed so that's one good thing about you know cloud functions where that logging is done in the stack driver so you don't have to worry about anything else with respect to logging now let's say that the use case here is you know you have an HTTP URL and you want to run a very simple HTML based website right so all we have to do is we have to output that HTML code so that the browser can render the HTML elements so I have my sample code in the notepad and just want to copy that and click on edit here copy the whole thing here now the the execution the function name is hollow so I'm going to change this to hello and do a deploy okay so it's going to take a while let's let's wait for that and then you would see you have this HTTP URL at the trigger so select this HTTP URL copy the URL okay the function is not yet uploaded still so let's wait for the function to be ready to execute okay it is ready to be executed I'm going to call we have already copied this URL if not you can click on the URL directly and click on here as you can see you know this is getting called from the function and it's rendering the HTML code which which is a very simple HTML code which is saying hello in the title and then a body with a bold please do subscribe this is just one one example where you can you know create your simple HTML website and then you can use cloud functions to execute that website and that's going to be very cost effective because you going to pay only for the executions you're not going to pay for for any infrastructure in like as opposed to if you create this piece of code in a VM where you have to pay for the VM and the volumen etcetera till you're running the VM now let's say that you want to change the trigger to something else you cannot do that right now because you have created the function and and the trigger is create done only at the time of creation of the function so we have to create the new function for that right so here click on create new function and cloud storage so the one thing that I want to show here is as we discussed right so the most important part for the function is the event in the context so the event is the event that you want to get from the cloud storage let's say in this case there is an object that got created so all those events like the metadata of the you know the objects can be can be used here too to do actions on the object of the GCS right so the context is the metadata of the event itself like what is the name of the event when was it created etc etc so this is just an introduction for further the other trigger we're going to discuss more in detail about the use cases that we want to use in Clawd functions with respect to the cloud storage maybe in the next video alright guys so that's it for this video I hope you got the initial understanding of the cloud functions and from the example of view them important things are the runtime and the maximum memory you can allocate for the function and the main differences between when you want to choose the cloud function versus when you want to choose the App Engine because both are like I've said the serverless compute services in GCP so these three are very important for the exam and then they'll give you some scenarios it might be useful even for the architect level exam the senior s would be like you know you will be given a situation you will be given a use case where you have to choose the right service and you have to decide smartly which services more cost-effective for that use case and I'm going to give some of the important tips and and the important points for the exam point of view in the description below
Info
Channel: Cloud Advocate
Views: 11,341
Rating: undefined out of 5
Keywords: serverless in GCP, google cloud functions, serverless in google cloud, GCP associate exam, cloud functions
Id: BejWo40P3HY
Channel Id: undefined
Length: 17min 52sec (1072 seconds)
Published: Sat Nov 30 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.