By Sakshi Khandelwal．Dec 11, 2022
In the recent Google Cloud Next Event in Bangalore, GCP is emphasizing Generation 2 of Cloud Function. You must be thinking why are we discussing Generation 2 even when Cloud Run is available? Because Cloud Function — Generation 2 uses Cloud Run in the background!
Let’s go deeper into the second generation of Cloud Function. What’s new in the second generation? As we know, there are 2 versions available:
- 1st Generation: First version
- 2nd Generation: New version built on top of Cloud Run and Eventarc
Key Enhancements in 2nd Generation:
- Longer Request Time:
2nd Generation provides a longer request timeout of 60 minutes for the HTTP triggered function which is almost 6 times as compared to the previous 1st generation.
- Larger instance size:
The next important enhancement is the large instance size. Now, Cloud Function can have RAM up to 16 GiB (32 GiB in Preview) with 4 virtual CPUs. Earlier, it could only have a maximum of 8 GiB RAM with 2 virtual CPUs.
Another key enhancement is concurrency. In the earlier first version, one instance of Cloud Function can have only 1 request. However, with the 2nd Generation, we can handle up to 1000 concurrent requests per function instance.
- Multiple Function Revisions and Traffic Splitting:
The 2nd Generation of Cloud Function is built on top of Cloud Run. Therefore, it supports features like multiple function revisions and traffic splitting. Both these features are not directly supported in the 1st Generation.
- Event Types:
As 2nd Generation is enabled by Eventarc, it supports 90+ events. We used to get only 7 event types for 1st Generation.
Login to your Google Cloud Console and search for Cloud Function. Click on “Create Cloud Function”. Enable required APIs if it’s not there in the next pop-up.
Give the function name and select 2nd Generation in the Environment. In the EventTarc, you can explore 90+ trigger options. For the demo, let’s keep the default HTTPS only like the below and click on Next:
In the Runtime, you can select any language, For now, we will go ahead with Node.js 16 with the existing code. Just click on Deploy.
As we can see now, the build is started. This build is done by Cloud Run. Hence, a container image will build in the background having our code.
And then same container image will be deployed to the Cloud Run as shown.
After a while, we shall see the cloud Function as ready.
Let’s go to the last TESTING tab and run the command in Cloud Shell. We shall be getting the below result:
Now, let’s edit the code by clicking on the top edit button and clicking on Next. Edit your code like the below and deploy it:
The Cloud Function will be built again. Now, again test in the same way, and we shall see if our new code changes are reflected. Hence, when we are changing the Cloud Function code, it’s updating the Cloud Run service.
Now go to the Cloud Run by searching it in the search bar. We should be able to see our Cloud Function name over there.
Click on the function name and go to the Revision tab. We will see 2 revisions of the same function.
Now, the interesting thing is we could configure the traffic to be sent to each of these cloud functions. We can go ahead and click on 3 dots in front of the function and select “Manage Traffic” as below.
Now we can configure like we want to send 50% traffic to the first revision and 50% to another like below. This is not possible in 1st Generation. This is something only possible in the 2nd Generation of Cloud Function.
Now, we have seen that behind all 2nd Generation Cloud Functions, there are Cloud Run revisions. All the features that were typically associated with Cloud Run can also now be used with Cloud Function.
Hope you like it.
If you have any related queries, please free to contact me. I’ll be happy to help you if I could. Stay Connected 🙂
If you liked this post, please clap for it. I would really appreciate it.
The original article published on Medium.