If you are in cloud application development, you must have heard about serverless execution multiple times. Your manager or client must have said let's go for serverless and save some cost. Well, they are not entirely wrong. When you deploy your application on EC-2, Compute engine, AppEngine or Container engine, you create an instance, a dedicated machine that will run 24*7 and you will be billed accordingly. What if you have an option to Pay only while your code runs? It sounds cool, right? That's where Serverless comes into the picture. Major Cloud Service Providers have their Serverless Execution Environment. AWS has Lamda and Google Cloud has Cloud Functions. I will specifically talk about Cloud Functions here.
Cloud Function is Event-driven Serverless compute platform. The official Documents from google says cloud function provides a connective layer of logic that lets you integrate and extend GCP and third-party services, making it possible to rapidly build serverless applications that are highly available, secure, and cost-effective. It is event driven, automatically scalable and fault-tolerant. In simple terms, you just write the code and it can be triggered by events emitted from your cloud infrastructure and services or Rest APIs, and the interesting part is you will only pay for the time while your code runs. Here is a list of event types that are supported:
The Supported Languages are:
I will take a reference from Google's official documentation to explain it.
Post a comment on Slack channel in response to a GitHub commit: Suppose you want to Post a Comment to your team's slack channel when one of your team member pushes the code changes in your Project's GIT repository.
It's a very common use case. You may want your code to respond to events from Cloud Storage or Firebase Storage to process files immediately after upload completes to generate thumbnails from image uploads, process logs, validate content, transcode videos, validate, aggregate and filter data in real-time.
The other use case of Real-time file processing could be categorizing the data. As soon as an image or a video file arrives in cloud storage bucket and you may want to apply machine learning to categorize it either as a person(male/female), animal(cat/dog), object etc. You may also want to extract information like emotions from images such as smile, fear, angry or having beard etc. Here is how it will work:
Use Cloud Functions to respond to events from Cloud Pub/Sub to process, transform and enrich streaming data in transaction processing, click stream analysis, application activity tracking, IoT device telemetry, social media analysis, and other types of applications. Here is how this can be done: