How can I specify the region of a Google Cloud Function?

白昼怎懂夜的黑 提交于 2019-12-22 08:33:54

问题


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 ?


回答1:


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.




回答2:


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":

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

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:




回答3:


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


来源:https://stackoverflow.com/questions/44461164/how-can-i-specify-the-region-of-a-google-cloud-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!