Deploy cloud function on Google Cloud Platform
Google Cloud Function is a serverless execution environment for building and connecting cloud services. By serverless, one means that the cloud functions remove a load of managing servers, configuring or updating software, and patching OSs. The software and infrastructure are completely managed by Google. You just have to add the code.
The key features of a cloud function are:
- No server management
- Scales automatically
- Pay only while your code runs
- Runs code in response to events
- Open and familiar
- Connects and extends cloud services
How they work?
Cloud service (Stackdriver, Cloud Datastore, etc. ) emits required events( Direct HTTP Invocation, etc.), a cloud function responds to those events by invoking other services such as APIs. After invoking other services, the cloud function writes back to cloud services.
Few Use cases for cloud function:
- Integrate with third-party services and APIs
- Serverless mobile backends
- Serverless IoT backends
So in this article, we are going to create a function, deploy it on google cloud, test the function and check the logs.
Creating a Function
Creating a function is very easy. Here, we will be creating a function using cloud console.
- In the Navigation Menu, hover over Cloud Functions.
- If you have not created any functions before, you will be asked whether you want to create a new function. Click on Create Function.
- Now, you will see an intimidating form containing different specifications you need to select in order to create a function. For your understanding, let us fill them as follows:
- Name: GCFunction
- Memory allocated: Default
- Trigger: HTTP trigger (To avoid any invokation directly via a HTTPs endpoint.)
- Source code: Inline editor
- Function to execute: helloWorld
- Click on Create.
Deploying the Function
Once you click on Create, you are directed to Cloud Functions Overview page, wherein, you can see all the functions created by you, their region, trigger, memory allocated, executed function, and date and time of last deployment.
After the page loads, you can see a small spinner icon beside the name of the function. This indicates that your function is being deployed.
Once deployed, the spinner icon changes to a green tick mark, indicating that your function is successfully deployed.
Testing the Function
- In the Cloud Functions Overview page, beside the last deployed column, you may find 3 dots which display menu for your function. In that, click Test function.
- Now, a Function details page will open. In the Triggering event box, add
. In this testing form, we provide a JSON formatted message. Next, click Test the function. This invokes our function and the output of out function will be shown to us in the output box, and you can see that your function is executed.
- Below the Output box, in the Logs field, you might see finished with status code:200. A status code of 200 indicates that your function was successfully executed.
A log contains some metadata displaying the time took by our function to execute, the status code that it returned. Moreover, it will also have the messages or errors, if any, that our function throws.
You can view the logs from the display menu on Cloud Functions Overview page. Go back to the Cloud Functions Overview page, find the three dots, click on View logs. The log page will look as follows:
Cloud functions are very easy to create, use and manage. Few trendy and intelligent applications of cloud functions are
- Virtual assistants and Chatbots
- Video and image analysis
- Sentiment Analysis