source: google search

Introduction to Google Cloud Functions!

A short and beginner level introduction to google cloud functions.

Cloud functions! what are they ?

Cloud computing, Serverless architecture these are two very common terms we hear and read here and there now a days. Tech giants like amazon and google have already stepped into these platforms in quite an impressive way.

In short cloud functions are small block of code that’s executed on the cloud.

but, what does that even mean! That means you can host your functions (yes, read it again — functions) on the cloud and execute them only when you need them. You don’t have to maintain your own server for that. And the most impressive part is that — you’ll be billed for only what you’ve used (memory and disk usage, executions, bandwidth, etc)! Also It’s our friend nodejs on the server!

Why cloud functions?

We all developers face the difficulties with maintaining our own servers and scaling them properly. Sometimes we’ve to maintain an application server for doing just a few simple tasks for which we have to spend a lot of time and effort. It’s just not worth it.

Imagine if you could just ship a function without any application setup, server management, extra billing, extra time and effort for scaling. Wouldn’t it be nicer!

Now, Google cloud function does that for you. You can just write a function and host it there! Then you can use it from anywhere via http calls or via events. Don’t believe me ?

Let’s Try It

First of all we need to browse to the Google Cloud Functions Page and Click on the TRY IT FREE button.

You’ll be taken to the Google cloud platform for free register procedure. After you accept the terms you’ll be taken to the page where they ask for your details. Then you need to add a billing method (don’t fear, they’ll not bill you then).

You’ll get $300 credit on the platform upon sign up and you can enjoy the free tier till you finish them or the 12 month free period expires.

Then browse to the developer console by clicking the console link.

If you’ve already got an app you can select that or you can create a new one and from billing settings you can turn on billing for that app (trust me they won’t charge immediately).

after that your console page would look something like below —

now, select Cloud Functions link directly or select it from the top left menu.

Then by clicking the CREATE FUNCTION we can go to the create form which looks like this -

let’s keep everything default and have a look.

The name filed is the name of the codeblock which is not much important for now.

Then the allocated memory which is a very important field which you can change as your need.

Then comes when to trigger this function, the default is http trigger which executes the function on http request.

It’s a simple express js like function which takes a request and gives a response! Our function is named helloWorld by default.

Then the function to execute will be our helloWorld function here.

now upon clicking on Create Google cloud will deploy this function to the cloud for us and you’ll see the screen below after you select our function -

now, we can test this function here by clicking the Testing link and there we’ll find a form like below in which we can just click the Test the function button to test the function with just an empty object {} in the Triggering event field. Or we can pass a json object {"message": "Hello World!"} in the Triggering event field and we’ll get Success: Hello World! response.

So, Congratulations for running your first google cloud function successfully!

Now, we can use this function from the outside also, Google hosts this function and gives us a dedicated endpoint which we can use anywhere we need. Just browse to the trigger tab.

Here we’ll find a dedicated url for our function to execute it on http calls.

Now we can execute this link from any http client like -

Postman, curl, or other applications. For example I’m showing Curl example -

curl -d ‘{“message”: “Hello World!”}’ -H “Content-Type: application/json” -X POST

So, that was a quick look into google cloud functions. We can use far more advanced things on the cloud like — npm packages, other google or third party services, multiple instances, etc. That’s for you to explore.

Good Luck! :)

you can check out this video -