10

I'm currently using Google Cloud Function to build up my restful API. However, I've found that it's slow because the my Google-Cloud-Function server is on "us-central", whereas my service is in Asia.

I tried to change the default region of my Google Project to "asia-west-1" and restart the cloud function—I followed the steps outlined here—but, unfortunately, it's still in "us-central". How can I change the function's region ?

Nicholas
  • 501
  • 2
  • 14
DumDumGenius
  • 163
  • 2
  • 7

4 Answers4

10

No such region as "asia-west-1"

There is no such region as "asia-west-1" on the Google Cloud Platform. At the time of writing, GCP provides the following regions in Asia:

  • asia-east1
  • asia-northeast1
  • asia-south1
  • asia-southeast1

However, asia-northeast1 is currently the only GCP region in Asia available for Google Cloud Functions:

$ gcloud functions regions list
NAME
projects/xxxxxxxx/locations/europe-west1
projects/xxxxxxxx/locations/us-east1
projects/xxxxxxxx/locations/us-central1
projects/xxxxxxxx/locations/asia-northeast1

How to specify the region of a Google Cloud Function

Using Google Cloud Console

The region setting is easy to miss. It's actually hidden behind an accordion entitled "More":

accordion

Clicking on it reveals a Region dropdown (among other advanced settings):

dropdowns

Using gcloud

Simply specify one of the four available regions using the --region flag.

Minimal working example

Under the assumption that you have

$ tree .
.
└── index.js

0 directories, 1 file
$ cat index.js 
exports.hello = (req, res) => {

  res.send(`Hello World!`); 

}

then running

gcloud functions deploy hello --region asia-northeast1 --trigger-http

should deploy function hello to region asia-northeast1:

asia-northeast1

jub0bs
  • 60,866
  • 25
  • 183
  • 186
  • 2
    Is there a way to do that through node js? When I try to deploy using 'firebase deploy --region europe-west1' , gives me 'Uknown option --region'. Any suggestions? – gugateider Oct 11 '18 at 10:41
  • I'm not sure whether the Firebase CLI supports the whole set of Google-Cloud-Functions regions... – jub0bs Oct 11 '18 at 10:53
  • 2
    Apparently there's a way, just found out on this link here: https://firebase.google.com/docs/functions/manage-functions#modify will try now and will let you know. – gugateider Oct 11 '18 at 12:40
  • 2
    Sorry forgot to let you know, yeah it works fine. just use functions.region('europe-west1').https ... it will create on the desired location. Tks – gugateider Oct 11 '18 at 15:00
6

It seems that Google Cloud Functions are currently available only in region us-central1. If I go to "Create function" (https://console.cloud.google.com/functions/add), the Region dropdown has only one choice, and it's us-central1.

  • 1
    This is an older answer. In the newer version, when you click that link, there is a dropdown box that lets you select between multiple different regions – Lance Samaria Oct 07 '21 at 05:35
5

If you are using the Firebase SDK:

Taken from the documentation - https://firebase.google.com/docs/functions/manage-functions#modify

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

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

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

exports.webhookAsia = functions
    .region('asia-northeast1')
    .https.onRequest((req, res) => {
            res.send("Hello");
    });
robsiemb
  • 6,157
  • 7
  • 32
  • 46
Adam91Holt
  • 1,018
  • 1
  • 14
  • 28
0

Learning it hard way, if you do not explicitly define region for functions it will set region to us-central1 even though your firestore and firebase project already set to use another region: export const notifyUsers =

functions.firestore.document("tasks/{docId}").onUpdate(

To fix the issue you need to specify region in your index.ts file for each functions like below and and deploy it again:

export const notifyUsers =
functions.region('europe-west1').firestore.document("tasks/{docId}").onUpdate(

After deploying, you can check Functions tab of firebase console that your function names now showing updated regions. Now you can delete the ones with old region values if you have not done it when asked with yes/no question during deployment process.

Elmar
  • 2,235
  • 24
  • 22