问题描述
当该功能的入口设置设置为“仅允许内部使用”时,如何从Cloud Tasks中触发一个Cloud Function?
使用该设置,该函数将拒绝来自Cloud Tasks的传入HTTP通信。即使 Cloud Tasks与该功能位于同一项目中,也会发生此行为。
- https://cloud.google.com/functions/docs/networking/network-settings#ingress_settings
- https://cloud.google.com/tasks/docs/creating-http-target-tasks
解决方法
这很有趣,因为我在本周二询问了Google PM。 因为今天你做不到!这是PM的雷达,没有时间表,但有可能一天。
我今天的解决方案。
如果我在内部和外部(或不兼容VPC连接器的Google无服务器产品,例如Cloud Task,Cloud Scheduler,PubSub和Workflows)使用仅内部模式的云功能,则创建一个“代理功能”
- 此代理功能以
ingress=all
模式和no-allow-unauthenticated
参数部署 - 我仅将外部产品的服务帐户授予代理功能上的cloudfunctions.invoker,以确保只有该服务帐户才能调用代理功能
- 我创建一个serverless VPC connector并将其添加到代理功能
- 代理功能仅调用内部功能。
你好@guillaume和@Thomas, 我面临着从云任务调用云功能的挑战。 以下是我的IAM和代码的详细信息:
const { CloudTasksClient } = require('@google-cloud/tasks');
const client = new CloudTasksClient();
//See https://cloud.google.com/tasks/docs/tutorial-gcf
module.exports = async (payload,scheduleTimeInSec) => {
const project = process.env.GOOGLE_APPLICATION_PROJECTID;
const queue = process.env.QUEUE_NAME;
const location = process.env.QUEUE_LOCATION;
const callBackUrl = https://asia-south2-trial-288318.cloudfunctions.net/cloud-function-node-expres/;
// Construct the fully qualified queue name.
const parent = client.queuePath(project,location,queue);
const body = Buffer.from(JSON.stringify(payload)).toString('base64');
const task = {
httpRequest: {
httpMethod: 'POST',url: callBackUrl,headers: { 'Content-Type': 'application/json' },body
},scheduleTime: {
seconds: scheduleTimeInSec,}
};
if (process.env.GOOGLE_APPLICATION_SERVICE_ACCOUNT_EMAIL) {
task.httpRequest.oidcToken = {
serviceAccountEmail: process.env.GOOGLE_APPLICATION_SERVICE_ACCOUNT_EMAIL
}
}
const request = {
parent: parent,task: task,};
// Send create task request.
try {
let [responses] = await client.createTask(request);
return ({ sts: true,taskName: responses.name,msg: "Email Schedule Task Created" })
}
catch (e) {
return ({ sts: true,err: true,errInfo: e,msg: "Unable to Schedule Task. Internal Error." })
}
}
process.env.GOOGLE_APPLICATION_SERVICE_ACCOUNT_EMAIL具有 Cloud Functions Invoker 角色,并且Cloud Function具有 allAuthenticatedUsers 成员,并且具有 Cloud Functions Invoker 角色doc。
但是我仍然看到Cloud Task取消了401处置,而Cloud Function没有被调用(见下图):
对此有何评论,这是怎么回事