'Deploying in functions in multiple regions with a unified codebase

I have a fairly simple requirement in that I need an identical replica of my Firebase functions, bucket and firestore database in multiple regions to satisfy that data does not move between regions. One for EU, GB, US etc..

Because you can only have one firestore database per firebase project I'm creating a new project per region as recommended. I can redirect my bucket read/writes to the correct region using the firebase project environment variables to define which bucket to write to. So far so good.

Now the last bottle neck is the functions themselves.

The problem is by default functions go to "us-central1" rather than the firebase project region so it seems the only way to specify region is using the .region("eu-west3") specifier in the codebase etc. But because I want a single unified code base across all projects changing this on a per project basis is a bit cumbersome.

Any suggestions on how best to manage this?



Solution 1:[1]

You can store the Region values as environment configs using firebase functions:config:set:

firebase functions:config:set env.region="us-central1" 

After running functions:config:set, you must redeploy functions to make the new configuration available.

Then get the environment variable inside your function by using this code:

exports.myFunction = functions
    .region(functions.config.env.region)
    .https.onRequest((req, res) => {
            res.send("Hello");
    });

With this, you no longer need to change region inside your code. But you still have to change configs and redeploy functions on every project if you want to change regions.


An additional solution is to use Cloud Build to automate everything. I haven't fully tested it yet, but here's what I can come up with.

First, follow the instructions on how to use the Firebase builder tool. You need this community provided image to run Firebase CLI commands on Cloud Build. Once finished, make sure that you have the following API's enabled on your projects:

  1. Cloud Resource Manager API
  2. Firebase Management API

and on your Cloud Build settings, Firebase Admin is enabled.

Then try this cloudbuild.yaml file. Cloud Build will use the tool from your project and to your other projects:

steps:
# Setup First Project
  - name: gcr.io/project-id1/firebase
    id: 'Use Project 1'
    args: ['use', 'project-id1'] 

  - name: gcr.io/project-id1/firebase
    id: 'Set Firebase Environment Config'
    args: ['functions:config:set', 'env.region=us-central1'] 
     
  - name: gcr.io/project-id1/firebase
    id: 'Deploy function1'
    args: ['deploy', '--project=project-id1', '--only=functions']

# Setup Second Project
  - name: gcr.io/project-id1/firebase
    id: 'Use Project 2'
    args: ['use', 'project-id2'] 

  - name: gcr.io/project-id1/firebase
    id: 'Set Firebase Environment Config'
    args: ['functions:config:set', 'env.region=eu-west3'] 
     
  - name: gcr.io/project-id1/firebase
    id: 'Deploy function2'
    args: ['deploy', '--project=project-id2', '--only=functions']

# And so on...

Note: Change project-id with your actual Project ID.

Solution 2:[2]

exports.myFunction = functions
.region(functions.config.env.region)
.https.onRequest((req, res) => {
        res.send("Hello");
});

config is a function so this adjustement was necessary:

exports.myFunction = functions
.region(functions.config().env.region)
.https.onRequest((req, res) => {
        res.send("Hello");
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Donnald Cucharo
Solution 2 Willkuerlich