Open In App

Deploy cloud function on Google Cloud Platform

Improve
Improve
Like Article
Like
Save
Share
Report

Cloud functions

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: 
 

  1. Integrate with third-party services and APIs
  2. Serverless mobile backends
  3. Serverless IoT backends

Google Cloud Function lets you code in conventional programming languages, including Python and JavaScript(Node.js). This helps the developers, well versed in Java or Python, to upload functions quickly and easily. 

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. 

 

  1. In the Navigation Menu, hover over Cloud Functions.
  2. If you have not created any functions before, you will be asked whether you want to create a new function. Click on Create Function.
  3. 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 invocation directly via a HTTPs endpoint.)
    • Source code: Inline editor
    • Function to execute: helloWorld
  4. 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

 

  1. 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.
  2. Now, a Function details page will open. In the Triggering event box, add 
     
{"message":"Hello World!"}
  1. . 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. 
    Output: 
     
Hello World!
  1. 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.

 

 

View logs

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

 


Last Updated : 14 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads