Deploy cloud function on Google Cloud Platform

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 invokation 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!"}

    . 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!
  3. 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.
  4. 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.

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.