A Practical Approach to Cloud Functions for Firebase: Setup

Paul Ruiz
Firebase Developers
9 min readDec 14, 2020

Cloud Functions for Firebase is a lightweight tool that allows you to run code snippets written in JavaScript or TypeScript for the Node environment directly on Google‘s scalable infrastructure. Cloud Functions are also able to tie directly into your Firebase projects without requiring you to set up a typical, cumbersome, backend infrastructure.

These snippets can access other Google APIs, external APIs, other Firebase products within your project, or simply perform some logical operations that return a result to a user. Some example use-cases where you may want to use Cloud Functions include:

  • Adding data to a Firestore database from an IoT device using Google Cloud IoT Core and Pub/Sub.
  • Accessing an external API to perform an action, like Google’s reverse geocoding to return an address for a latitude and longitude.
  • Sending a notification to a user when they authenticate against Firebase for the first time.
  • Handling any sensitive operations that you wouldn’t want to run locally on a user’s device.

Since my background is mainly in Android development without a lot of backend knowledge, I’ve grown to appreciate Cloud Functions as a product for when I need to create a backend for a project, even with some struggling along the way. Given this, I want to share what I’ve learned from using Cloud Functions for Firebase so that you can have more information about the tool the next time you need to decide what you want to use for a project. This series will attempt to share these lessons through practical applications and examples to highlight Cloud Functions in a more realistic environment, and hopefully provide you with some useful ideas for functionality in your own projects.

For this first article I plan to keep things simple and just introduce how to set up a Firebase project to use Cloud Functions, as well as how to deploy a basic “Hello World” function. While this might not be the most exciting example, it does provide the foundation for later articles where I will go into the various ways you can access Cloud Functions, as well as into more depth on specific features of Cloud Functions. If you’ve already used Cloud Functions for Firebase before, then you can safely skim this article and keep an eye out for the next.

With that, let’s get started.

Getting Setup and Deploying a Function

The first thing we will need to do to use Cloud Functions for Firebase is create a Firebase project. If you don’t already have one, you can go to the Firebase Console and add a project there.

Next, after continuing into the Firebase project, we will need to go down to the Functions item in the side navigation panel.

In order to use Cloud Functions for Firebase, we will need to enable billing in our project by clicking on the Upgrade project button on the screen. If you already have billing enabled on your project, then you can skip this step.

But wait, you might be thinking to yourself “isn’t there a free tier with Functions?” and you’d be right. Cloud Functions for Firebase does have a free tier for usage up to a certain point, as outlined on our pricing page. However, because Cloud Functions uses some aspects of Google Cloud’s paid infrastructure, we need to enable billing to use Cloud Functions. In general you shouldn’t be charged more than a few cents per month for basic functions.

That said, I will recommend that you enable budget alerts. Cloud Functions can access other parts of Firebase and use up your free quota if you make a coding mistake, like an infinite loop, and you will want to catch those issues early.

For a really detailed explanation on this, I highly recommend this video from the Firebase YouTube channel.

After continuing through the billing dialog box, you should end up on a screen where you can see a button that says Get started.

Let’s click that button, and then click through the prompts. When that’s all done, you should end up at a screen with the message Waiting for your first deploy. We’ll work on that next.

With Functions enabled, we will need to do some work locally. Because Cloud Functions for Firebase use a Node environment, we will need to get Node installed on our development machine. This process is going to vary by OS and is a bit out of scope for this article, but you can get started by downloading the tools here. Once you’ve finished with that install, you should be able to see what version of Node you’re running with the command node -v from a command line.

Next we will need to install the Firebase command line interface tool. You can do this on Mac by running the Node command npm install -g firebase-tools

If you’re on Windows or Linux, you can find the instructions for your OS here.

After the Firebase CLI has finished installing, let’s navigate to a new folder through our CLI that will contain our Functions code. Once we’re there, we can run the command firebase login to log in to whichever account is associated with our Firebase project, and then approve its access to the project through the CLI.

For our next step, let’s return to the CLI and run the command firebase init. This will prompt us to select whichever functionality we want to support from our CLI, as well as set some general confirmation information. For this article we’ll only need the Functions feature turned on.

We will also be prompted to select a project that we want to link to this directory. Since we’ve already created one, we can select Use an existing project and then select our new project.

Once that’s set, we’ll be asked if we want to use JavaScript or TypeScript for writing our Functions. I’m told that TypeScript offers some additional functionality over JavaScript, but like I mentioned earlier, my background is in mobile development, and JavaScript is easier to search for when I need to fill in the (many) gaps in my knowledge, so I’ll stick to using that for this series. If you’re comfortable with TypeScript, or are just looking for a challenge, a lot of what I talk about in this series should be straightforward enough to convert, so feel free to give it a try :) Other than language selection, I would recommend just sticking to the default options during the setup script.

If we look in our directory now, we should see a firebase.json file and a functions folder.

Let’s go ahead and navigate into that functions folder. In there we should see a few files and another directory.

The one we’re interested in is index.js. This is where we’ll add all of our code for Cloud Functions. Let’s go ahead and see what’s in there now:

Kind of boring, isn’t it? While this isn’t the most exciting code in the world, we’ll do some more interesting things with Cloud Functions later. For now, let’s go ahead and uncomment the last four lines.

This code will instantiate the functions library in the first line, and then it exports a new function called helloWorld. This function listens for an https request, and when it gets one it’ll write to the Cloud Functions logger and respond with the message “Hello from Firebase!”.

So now that we have a Cloud Function, let’s actually deploy it. From the CLI, we can run the command firebase deploy to deploy all of our functions at once, as well as some other Firebase features we might have implemented from the Firebase tool (such as Hosting). If we only want to deploy functions, then we can run the command firebase deploy --only functions, and if we had more than one function we could use deploy --only functions:helloWorld to only deploy that named function. Let’s go ahead and use the first option for now.

One thing that’s important to note here is that you need to be under the functions folder in order to deploy your Cloud Functions. If you’re in the directory above it, you’ll end up getting a 403 error when you try to deploy.

After the command finishes running, you should see a Deploy complete! message at the bottom, as well as some URLs.

The one we’re really interested in right now is the third line from the bottom that starts with Function URL (helloWorld):. This is the endpoint for that new Function. If we go to it in our browser, we should see our response message.

And if we return to the Firebase Console, we should see our new Function in the dashboard.

More importantly, you may remember that we used one of Cloud Function’s logging methods. This ended up populating our logs under the logs tab in the Firebase console, which is also where we’ll find other important debugging information, like crash logs.

Conclusion

In this article we were introduced to Cloud Functions for Firebase. We learned how to set up the Firebase Console, and how to deploy our first Function. In the next few articles we will learn about multiple ways that we can access a Cloud Function, such as directly with a POST request, Firebase’s various SDKs, and Firebase product triggers, as well as how to use multiple product features with Cloud Functions. See you there!

Update: More has been added to this series! :)

Resources

--

--

Paul Ruiz
Firebase Developers

Developer Programs Engineer on Android, Maker, @ptruiz_dev