Easy environment variables config for Firebase Cloud Functions

João Teixeira
Firelayer
Published in
3 min readOct 2, 2019
Photo by Fabian Grohs on Unsplash

Deploying Firebase Cloud Functions nowadays is very easy, a simple command and that’s it. But those functions can and will be called in multiple environments, they can be called on a test environment, staging, production and while developing on our local machine.

So many environments for the same functions means that the functions may need to adapt to call a different host for a service or different credentials for each environment. How can we achieve that? With environment variables, well applied and they keep the functions versatile to work in all env contexts.

How can we keep a simple strategy for the env variables? This article will try to give some tips and tricks on how can we use and deploy with ease the environment variables for our Cloud Functions.

Have in mind that the following directions are for those who already have started the Firebase project with Cloud Functions enabled.

The commands

The commands to handle the environment variables are very straight forward. Let’s check them out with some quick practical example.

We can start by setting two variables, keep in mind that each config value must have a 2-part key (e.g. foo.bar) and you cannot use upper case for the key.

firebase functions:config:set slack.url="value" slack.key="123"

Let’s get current config for your project

firebase functions:config:get

Which will return the following content:

{
"slack": {
"url": "value",
"key": "123"
}
}

And to finish just unset one of the variables from the project config

firebase functions:config:unset slack.key# and if we re-run the get commandfirebase functions:config:get# we should get{
"slack": {
"url": "value"
}
}

How to use in functions

Using the project environment variables inside our functions is also very easy to accomplish, just call the functions.config() from firebase-functions and you are all set and ready to use the variable, see in example bellow.

Firebase Cloud Functions environment variables use example

The problem

While your application grows in size and complexity it can become hard to keep track of all your variables. It’s especially hard because they will pile up as we usually don’t unset the variables that are not used anymore, and as they don’t unset on their own, they will be there forever.

A single source of truth

To tackle the issue above, let’s considered using a file where we can keep all the environment variables and where we can keep track of them.

Enter the env.json, a file that we will contain all our environment variables and make life easier for us.

env.json content

How to deploy the variables with the env.json file? We just output them as a property to the main key env:

firebase functions:config:set env="$(cat env.json)"

We update our functions to use const config = functions.config().env because now everything is inside the env key.

Great! Now we can make a bulk update of our variables and keep track of them, but we still are lacking to tackle one issue, how to remove the previously defined variables that we no longer want inside the env key.

To tackle this we can combine the unset and set to make the variables deploy clean as a whistle:

firebase functions:config:unset env && firebase functions:config:set env="$(cat env.json)"

And that’s it we can now relax and just worry about that single file for our environment variables.

Now we can run the deploy of the env.json variables before the Cloud Functions deploy:

So if we run npm run deploy the new configs will be updated and then the deploy for the Cloud Functions will run.

Using it for local development

We can go a bit further we can see how to use this variables locally for development.

First we need to do is to set a common file that will contain the logic to return the current configuration. So we setup a config.js that will handle that switch. Afterwards we update the previous example to use the new config.js file.

Using environment variables locally with Firebase Cloud Functions

And that’s that, locally we are using the file env.json and when we deploy we will be using the Cloud Functions defined variables.

Firelayer — Launch your Firebase MVP 10x faster with our templates

Jump-start your Firebase Project with this open source project

https://firelayer.io

Thanks for reading. You can learn more about me on Medium, Github.

--

--