How to get started with Google cloud functions?

The ‘Cloud Functions’ is a Google technology bundled as part of the Firebase platform. It is a serverless backend to support our app development. Functions can be triggered by events that are emitted by Firebase platform products (Firestore, Cloud storage, Authentication, etc), Google Cloud Services and 3rd parties using Webhooks.

What next?

Let us see, how we can create and deploy our first function with-in 30 minutes of time. We will go through the following steps in detail to achieve this,

  • Create a Firebase project
  • Setup Firebase CLI
  • Initialize Firebase project locally
  • Create a function
  • Deploy a function
  • Access the function

Create a Firebase project

To get started with, you need to create a project in Firebase console and fill the necessary information about your project (refer to the link attached). Just for simplicity, I have named it like ‘helloworld’ and fill the other required information.

Setup Firebase CLI

Next, you need to set up the Firebase CLI (Command Line Interface). To complete the Firebase CLI, you need to make sure that, you have already installed the Node.js language. Node.js versions 8 and 10 are supported with Firebase CLI. So please make you have installed the supported version of Node.js

After you installed the Node.js language, you need to execute a command to install the Firebase CLI.

npm install -g firebase-tools

This installs the firebase utility globally. Please re-execute the command again if you face any problem or need to upgrade the firebase tools. Once everything goes fine, you can execute the below command to see the version of the firebase tools installed in your system.

firebase --version

Initialize Firebase project locally

Your next job is to set up the firebase project locally to create our functions. In order to achieve this, you need to execute the following command and which will create some skeleton codes in our local disk.

> firebase login
//Execute the command to connect with Firebase Cloud project

> firebase init
//Answer the questionnaire properly and select the project which was created earlier
//You will have the skeleton code ready in your once this command is completed


> firebase init functions
//This can be useful when you have the code ready and only to initialize functions
//Refer the sample output of the created project
 +- .firebaserc    # Hidden file that helps you quickly switch between
 |                 # projects with `firebase use`
 +- firebase.json  # Describes properties for your project
 +- functions/     # Directory containing all your functions code
      +- .eslintrc.json  # Optional file containing rules for JavaScript linting.
      +- package.json  # npm package file describing your Cloud Functions code
      +- index.js      # main source file for your Cloud Functions code
      +- node_modules/ # directory where your dependencies (declared in
                       # package.json) are installed

Create a function

Add the following lines into your index.js file and save.

const functions = require('firebase-functions');

//ping function = functions.https.onRequest(async (req, res) => {
  res.send("Welcome Cloud functions!");

//All the functions will be served over an HTTPS protocol by default.

Deploy a function

Execute the following command for deploying your first function into the Firebase cloud.

$ cd helloworld
$ firebase deploy --only functions

Once it is deployed successfully, you will be able to see the deployed function into the Firebase console.

Access the function

Now our function is available for access. You should be able to access your first function with the following endpoint.

The moment you hit the url in the brower you should be able to see the response back from the function.

NOTE: The URL will be generated based on the project id. You can relate helloworld is  our project id in this case. In your case it might different.

Congratulations! Now you have successfully created and tested your first cloud function. Just celebrate the moment and keep learning about the wonderful technology.

This might be a simple cloud function. The whole purpose of the article is to explain the overall concept of cloud function and get started with the setup. You can see more detailed and various other use cases in the upcoming articles.

Keep watching this space for more information! Please share the article if you feel it is useful in some context.

Happy coding!

API Evangelist | Chief Hustler | Habit Coach | Madras Java User Group Leader

Latest articles

Flutter: How does column layout work?

Layouts are the fundamental starting points for developing Flutter applications. There are many layout options are available by default. I am going...

Flutter: Stateless vs Stateful widgets

Flutter is a mobile application development framework developed by Google. You need to understand the fundamental building blocks before you dig deeper...

Why Flutter is a great technology for makers &...

As per the wikipedia, Flutter is an open source UI development kit created by Google. It is used to...

Why 12-Factory methodology matters to your Microservice development?

The 12-Factor application development methodology and why is it important for your microservices strategy

When to use Firebase Cloud functions?

Recently I got a question from one of the Indie makers that, when to use Firebase Cloud Functions? This...

Similar articles


Please enter your comment!
Please enter your name here