为Google Cloud Run配置VPC访问时遇参数错误求助
问题:Cloud Run部署带私有VPC连接器的服务时参数错误
部署时持续报错:
Error: 3 INVALID_ARGUMENT: Violation in CreateServiceRequest.service.template.vpc_access.connector: one and only one of connector and network_interfaces must be set.
移除VPC配置后脚本可正常部署,相关代码如下:
const parent = 'projects/***/locations/us-central1'; const service = { template: { vpcAccess: [ { connector: 'projects/***/locations/us-central1/connectors/socket-connector-name', egress: 'private-ranges-only' } ], containers: [ { image: '***', }, ] }, }; const serviceId = 'abc123'; const { ServicesClient } = require('@google-cloud/run').v2; const runClient = new ServicesClient(); async function callCreateService() { const request = { parent, service, serviceId, }; const [operation] = await runClient.createService(request); const [response] = await operation.promise(); console.log(response); } callCreateService();
解决方案
问题出在vpcAccess的定义上——你把它写成了数组,但Cloud Run v2 API要求vpcAccess是一个单个对象,而非数组。API会将数组格式的vpcAccess识别为多组配置,触发"只能设置connector或network_interfaces其中一项"的校验规则。
修正后的service对象如下:
const service = { template: { vpcAccess: { connector: 'projects/***/locations/us-central1/connectors/socket-connector-name', egress: 'private-ranges-only' }, containers: [ { image: '***', }, ] }, };
只需移除vpcAccess外层的[],改为普通对象格式即可正常部署。
内容的提问来源于stack exchange,提问作者Thomas
相关产品推荐
相关产品推荐

