42

is there a way to specify the Region/Zone where my firebase functions will be deployed.

Actually i didn't found anything about that in the documentation and my functions are always deployed to us-central1 but i want to have it on eu-central1...

Is it possible to set it in the Firebase - Config - File?

{
  "database": {
    "rules": "database.rules.json"
  },
  "hosting": {
    "public": "public",
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}

I also had a look on the cli options but i did not found anything there.

The Firebase Project itself is correctly set to an european Region o.O

thanks in advance

Frank van Puffelen
  • 418,229
  • 62
  • 649
  • 645
BernhardS
  • 716
  • 2
  • 7
  • 21

5 Answers5

45

firebaser here

Update (2018-07-25):

It is now possible to specify the region for your Cloud Functions in Firebase you specify that region in your code and deploy the change. E.g.:

exports.myStorageFunction = functions
    .region('europe-west1')
    .storage
    .object()
    .onFinalize((object) => {
      // ...
    });

For full details see the Firebase documentation on Cloud Functions locations (from where I got the above snippet) and modifying the region of a deployed function.

abraham
  • 41,605
  • 9
  • 84
  • 134
Frank van Puffelen
  • 418,229
  • 62
  • 649
  • 645
  • Thank you for your answer =) – BernhardS Apr 23 '17 at 14:50
  • 1
    @FrankvanPuffelen Will it be possible in the future to indicate the region where cloud functions for Firebase will run? – Korneel May 19 '17 at 09:07
  • Since functions are ephemeral, what would the advantage of that be? – Frank van Puffelen May 19 '17 at 14:06
  • 5
    @FrankvanPuffelen For that sweet inter-region latency. When architecting a complex setup using Google cloud services, things will be quicker if the data store and the computation are co-located. So to reiterate the question, will it be possible in the future? – Scottmas Jul 06 '17 at 15:23
  • 1
    Multiple regions are coming. GCF doesn't talk to your users; it talks to your other services. I'm very curious though which features you are using. E.g. the Firebase Realtime database is only available in the US, whereas Cloud Storage buckets can be deployed one of multiple regions. If you move your database function to Europe you're likely to increase latency but if you move your storage function to Europe you might decrease it. For HTTP functions, put them behind Firebase Hosting for global CDN caching (though regionality will help cache misses) – Thomas Bouldin Sep 22 '17 at 05:20
  • 2
    Any news, when we can expect to have the feature to change region on firebase functions? – Medin Mar 28 '18 at 10:45
  • 2
    Is there any update on this issue? I saw the official doc mentioning Google Cloud Function does support selecting location https://cloud.google.com/functions/docs/locations . However, I still haven't figured out how to choose region via either CLI or firebase.json – Ivan Wang Jul 25 '18 at 19:18
  • This is indeed now possible. See https://firebase.google.com/docs/functions/manage-functions#modify-region. I'll update my answer. – Frank van Puffelen Jul 25 '18 at 21:16
  • I'm getting an error when I try to use 'functions.region('europe-west2')...' TypeError: functions.region is not a function - any ideas? – pomo Aug 16 '18 at 20:26
  • My initial guess is that you're on an SDK or CLI that doesn't support regions yet. Make sure you use the latest versions. If the problem persists, open a new question with the [minimal, complete code that reproduces the problme](http://stackoverflow.com/help/mcve). – Frank van Puffelen Aug 17 '18 at 05:30
  • Updating my firebase functions from 3.0.2 to 3.1 fixed the issue that @pomo had but also broke a load of other stuff to do with providers. I got about 20 errors similar to this: node_modules/firebase-functions/lib/function-builder.d.ts:4:28 - error TS2307: Cannot find module './providers/analytics'. 4 import * as analytics from './providers/analytics'; For now I have just rolled back as no time to deal with it. – MadMac Jul 15 '19 at 04:38
  • 1
    @FrankvanPuffelen I tried doing that (with `.region('us-east4')` for a Firebase function with pub/sub trigger, but getting this upon the deploy CLI command: `Error: HTTP Error: 400, Location must equal us-east4 because the App Engine app that is associated with this project is located in us-east4`. Any idea what might be causing that? – Pat Needham Aug 19 '19 at 02:11
  • The error message looks weird, but definitely check what region the app engine instance for your project is configured for. – Frank van Puffelen Aug 19 '19 at 03:45
20

From docs: https://firebase.google.com/docs/functions/locations

Now available in the following regions:

  • us-central1 (Iowa)
  • us-east1 (South Carolina)
  • europe-west1 (Belgium)
  • asia-northeast1 (Tokyo)

Best Practices for Changing Region

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

exports.webhook = functions
    .https.onRequest((req, res) => {
            res.send("Hello");
    });

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

exports.webhookEurope = functions
    .region('europe-west1')
    .https.onRequest((req, res) => {
            res.send("Hello");
    });
Jackl
  • 308
  • 2
  • 7
  • 1
    Where do you add this? I want to use `go` functions currently in the hosting options I only define the routes in the `firebase.json` but they always go to `us-central1` – nbari Feb 16 '19 at 19:26
  • 2
    btw. so the ideal setup is: cloud-functions in belgium (EU1) and datacenter in frankfurt (EU3)? I wonder why they do not offer the funtions and the database in the same datacenter? – Simon Fakir Mar 10 '19 at 11:28
  • 1
    @nbari same here. the documentation about it is quite unclear – Ben Mar 23 '19 at 10:48
  • you can set up the default in gcloud: **$gcloud config set functions/region REGION** https://cloud.google.com/functions/docs/locations – Pol Fernández Apr 29 '19 at 07:52
  • Not working. After set default region in gcloud, the rewrite rule in firebase.json still mapping to "us-central1". – joshmori May 31 '19 at 18:03
  • see discussion in https://github.com/firebase/firebase-tools/issues/842 – joshmori May 31 '19 at 18:09
  • Updated my functions from US1 to EU but did not work got redirected to google login. I use a custom domain, wondering if this is the issue - reverted back and working again. Ok I see that even though I am using redirects in hosting I must use functions in US - Important: If you are using HTTP functions to serve dynamic content for Firebase Hosting, you must use us-central1. – Ian Warner Jul 14 '19 at 14:18
  • @IanWarner, let me pick your brains. I have one function hosting an express server. Does this apply? It can only be US? – jackstride Apr 25 '20 at 18:22
6

To use the same custom region for all your functions, you do something like this.

import * as functions from 'firebase-functions';

const regionalFunctions = functions.region('europe-west1');

export const helloWorld = regionalFunctions.https.onRequest((request, response) => {
 response.send("Hello from Firebase!");
});

export const helloWorld2 = regionalFunctions.https.onRequest((request, response) => {
 response.send("Hello from Firebase 2!");
});
han4wluc
  • 1,039
  • 12
  • 23
  • not working for me. Getting error > Deployment error. Function failed on loading user code. This is likely due to a bug in the user code. Error message: Error: please examine your function logs to see the error cause – Damandroid Feb 16 '21 at 20:39
0

To have Firebase functions deployed to a region also work in local emulation, one must initialise the client as such:

const functions = LOCAL ? firebase.app().functions(/*functionsRegion*/) :  
  firebase.app().functions(functionsRegion);

const fun = functions.httpsCallable('your_function_name');

i.e. the code is not the same. Adding a region in the emulated case makes the calls get lost, without an error.

LOCAL is a client side value I've made to know, whether the back end is running in the cloud, or set up for local emulation.

Surely, this is a corner case that Firebase will iron away?

firebase-tools 8.6.0, firebase 7.16.1


Addendum:

The above is meant for browser (wasn't stated). I since stopped using httpsCallables since they require an online connection. Interfacing via Firebase databases, on the other hand, is offline friendly and as such leads to a more robust system.

akauppi
  • 14,244
  • 12
  • 73
  • 94
  • you can use `process.env.FUNCTIONS_EMULATOR` to check if functions are running locally or not – Nick Jan 27 '21 at 09:37
  • @Nick In the browser, you cannot reach `process.env.FUNCTIONS_EMULATOR`. The sample code is for a browser. – akauppi Jan 27 '21 at 18:46
  • Since `firebase-tools` 9.12.0, the emulator now recognizes regions and this answer no longer is valid. – akauppi May 27 '21 at 08:42
-1
"rewrites": [
  {
    "source": "**",
    "run": {
      "serviceId": "<service id>",
      "region": "europe-west1"
    }
  }
]
},
Vikas Katariya
  • 4,703
  • 3
  • 7
  • 28