#
Create Cloud Function in GCP
This tutorial explains how we can create a Cloud Function in GCP.
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.
With Cloud Functions, there are no servers to provision, manage, patch, or update. Functions automatically scale and are highly available and fault-tolerant.Cloud Functions are great for building serverless backends, doing real-time data processing, and creating intelligent apps.
Cloud Functions are priced according to how long your function runs, how many times it is invoked and how many resources you provision for the function. If your function makes an outbound network request, there are also additional data transfer fees. Cloud Functions includes a perpetual free tier for invocations to allow you to experiment with the platform at no charge. Note that even for free tier usage, we require a valid billing account.
Cloud Functions (2nd gen) pricing is based on Cloud Run pricing.
For creating a Cloud Function, we need to connect to the console and go to "Serverless" -> "Cloud Functions".
You will see something like this:
Click on "Create Function".
Choose the environment type to use, function name, the region where the function will be created, and the trigger for that function. In my case, the trigger will be an HTTP request and I use "2nd gen" environment.
Info
Cloud Functions 2nd gen is a Function-as-a-Service product powered by Cloud Run and Eventarc. This enables users to leverage key benefits of Cloud Run including concurrency, traffic splitting, and longer processing times.
Choose the authentication you need.
Click on "Next".
Enter the runtime environment, the Entry Point and the function itself.
Info
A single function invocation results in execution of only the body of the function declared as the entry point.
Here we have the code of my function:
const functions = require('@google-cloud/functions-framework');
functions.http('helloHttp', (req, res) => {
myMessage="Hello "+req.query.name +" from GCP";
res.send(`${myMessage}`);
});
Now we can run https://function-1-sue6hk2rka-uc.a.run.app?name=John
in the browser and we will
see :