我该如何指定Google Cloud Function的区域?

10

我目前正在使用Google Cloud Function构建我的RESTful API。但是,我发现它很慢,因为我的Google Cloud Function服务器位于“us-central”,而我的服务在亚洲。

我尝试将我的Google项目的默认区域更改为“asia-west-1”,并重新启动云函数 - 我按照此处概述的步骤进行操作 - 但不幸的是,它仍然在“us-central”。

如何更改函数的区域?

4个回答

10

"asia-west-1"不是有效的区域

在Google Cloud Platform上,没有名为"asia-west-1"的区域。截至撰写本文时,GCP在亚洲提供以下区域:

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

然而,目前只有asia-northeast1是可用于Google Cloud Functions的GCP亚洲区域。

$ 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

如何指定Google Cloud Function的区域

使用Google Cloud控制台

区域设置很容易被忽略。它实际上隐藏在一个名为“更多”的手风琴后面:

accordion

点击它会显示一个“区域”下拉菜单(以及其他高级设置):

dropdowns

使用gcloud

只需使用--region标志之一指定四个可用区域之一。

最小工作示例

在假设您具有的情况下:

$ tree .
.
└── index.js

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

  res.send(`Hello World!`); 

}

然后运行。
gcloud functions deploy hello --region asia-northeast1 --trigger-http

应该将函数hello部署到区域asia-northeast1

asia-northeast1


2
有没有办法通过Node.js实现这个?当我尝试使用“firebase deploy --region europe-west1”进行部署时,会提示“未知选项--region”。有什么建议吗? - gugateider
我不确定 Firebase CLI 是否支持整个 Google Cloud Functions 区域集合... - jub0bs
2
显然有一种方法,在这个链接上刚刚发现:https://firebase.google.com/docs/functions/manage-functions#modify 现在会尝试并告诉您结果。 - gugateider
2
抱歉忘了告诉你,没问题,它可以正常工作。只需使用 functions.region('europe-west1').https ... 它将在所需位置创建。谢谢。 - gugateider

6

1
这是一个较旧的答案。在更新的版本中,当您单击该链接时,会出现一个下拉框,让您选择多个不同的地区。 - Lance Samaria

5
如果您正在使用Firebase SDK:
源自文档 - 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");
    });

0

学习编程的时候要注意,如果你没有明确为函数定义区域,它会将区域设置为us-central1,即使你的Firestore和Firebase项目已经设置为使用另一个区域:

export const notifyUsers =
functions.firestore.document("tasks/{docId}").onUpdate(

为了解决这个问题,您需要在index.ts文件中为每个函数指定区域,如下所示,并再次部署它:
export const notifyUsers =
functions.region('europe-west1').firestore.document("tasks/{docId}").onUpdate(

部署后,您可以检查 Firebase 控制台的“函数”选项卡,以查看您的函数名称现在显示的更新区域。如果在部署过程中被问及是否删除旧区域值的问题并选择了“是”,则现在可以删除这些函数。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接