GCP第二代Cloud Function调用问题:CLI调用失败求助
问题描述
我在GCP Cloud Function上部署了两个函数,执行gcloud functions list显示结果如下:
第一代函数(#1)
NAME: myfunction-no-01 STATE: ACTIVE TRIGGER: HTTP Trigger REGION: us-central1 ENVIRONMENT: 1st gen
第二代函数(#2)
NAME: myfunction-no-01-gen2 STATE: ACTIVE TRIGGER: HTTP Trigger REGION: us-central1 ENVIRONMENT: 2nd gen
调用第一代函数时,执行gcloud functions describe myfunction-no-01可正常返回信息,但调用第二代函数时执行该命令返回404错误:
webmaster@cloudshell:~ (my-project)$ gcloud functions describe myfunction-no-01-gen2 ERROR: (gcloud.functions.describe) ResponseError: status=[404], code=[Ok], message=[Function myfunction-no-01-gen2 in region us-central1 in project my-project does not exist] webmaster@cloudshell:~ (my-project)$
请教以下两个问题:
- 如何调用第二代Cloud Function?
- 是否可以通过CLI调用第二代Cloud Function?
解答
1. 调用第二代Cloud Function的方式
第二代Cloud Function(2nd gen)基于Cloud Run,调用方式分两种:
- HTTP触发调用:直接使用函数的HTTP触发URL访问,这个URL可在GCP控制台的函数详情页找到,也能通过CLI命令获取。
- 事件触发调用:如果是事件触发的函数,会自动响应绑定的事件源(如Cloud Storage、Pub/Sub等),无需手动触发。
2. 通过CLI调用第二代Cloud Function的方法
可以通过CLI操作,但不能直接用gcloud functions命令(该命令主要针对第一代函数),第二代函数需要使用gcloud run相关命令:
- 查看函数详情:执行
gcloud run services describe myfunction-no-01-gen2 --region us-central1 --project my-project,即可获取函数的配置、触发URL等信息。 - 调用HTTP触发的函数:结合
curl工具和函数的HTTP URL发送请求,示例命令:
其中的URL可通过上述curl https://myfunction-no-01-gen2-xxxxxx-uc.a.run.appgcloud run services describe命令获取。
内容的提问来源于stack exchange,提问作者rshardja
相关产品推荐
相关产品推荐

