BernhardS
BernhardS

Reputation: 1088

firebase deploy to custom region (eu-central1)

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

Upvotes: 68

Views: 44881

Answers (9)

I find that passing the region as httpsOptions to the onCall or onRequest works fine. But still tedious to add to every file.

const httpsOptions: HttpsOptions = {
  region: "europe-west3",
};

export const someFunCall = functionsV2.https.onCall(
  httpsOptions,
  async (request: CallableRequest<{}>) => { 
    // your logic
  }
);

export const someFunRequest = functionsV2.https.onRequest(
  httpsOptions, 
  (req, res) => {
    // your logic
  }
);

Upvotes: 0

mfellner
mfellner

Reputation: 329

It's possible to set FIREBASE_FUNCTIONS_DEFAULT_REGION=your-region

See https://github.com/firebase/firebase-tools/blob/v13.8.3/src/deploy/functions/build.ts#L236

It works with v1 and v2 cloud functions.

This is not documented anywhere as far as I can tell but afaik it's the only way to dynamically set the region (e.g. on a CI/CD system) without hardcoding it.

Upvotes: 0

Moe
Moe

Reputation: 559

Solution for V2 Firebase Functions:

Updated 2024

With the new V2 syntax adding .region doesn't work, and also adding {region: ''} object at beginning doesn't work for onDocument events.

Workaround to update region for V2 firebase functions is updating global options to change region which also automatically defaults any function to the region you specified:

import { setGlobalOptions } from "firebase-functions/v2";

setGlobalOptions({ region: 'europe-west1' });

Also if you would like to edit any other options, it will always default to whatever you define in setGlobalOptions()

import { setGlobalOptions } from "firebase-functions/v2";

setGlobalOptions({
    maxInstances: 10,
    region: "europe-west1",
    timeoutSeconds: 60,
    memory: "2GiB",
});

Upvotes: 41

Pratheek Senevirathne
Pratheek Senevirathne

Reputation: 111

If you are using the new Firebase modular SDK, try this:

export const helloWorld = onRequest({ region: 'asia-southeast2' }, (request, response) => {
  logger.info('Hello logs!', { structuredData: true });
  response.send('Hello from Firebase!');
});

The set of available regions can be found here: https://firebase.google.com/docs/functions/locations#supported_regions

Upvotes: 3

Frank van Puffelen
Frank van Puffelen

Reputation: 598740

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.

Upvotes: 74

akauppi
akauppi

Reputation: 18046

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.

Upvotes: 0

Tekano Khambane
Tekano Khambane

Reputation: 15

"rewrites": [
  {
    "source": "**",
    "run": {
      "serviceId": "<service id>",
      "region": "europe-west1"
    }
  }
]
},

Upvotes: -4

han4wluc
han4wluc

Reputation: 1189

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!");
});

Upvotes: 13

Jackl
Jackl

Reputation: 496

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");
    });

Upvotes: 38

Related Questions