首页 > 解决方案 > Cloud Functions 调用另一个 Cloud Functions 面临“访问被禁止”

问题描述

我正在尝试从另一个调用云函数,为此,我正在关注此文档

我创建了两个函数。这是调用另一个函数的代码:

const {get} = require('axios');

// TODO(developer): set these values
const REGION = 'us-central1';
const PROJECT_ID = 'my-project-######';
const RECEIVING_FUNCTION = 'hello-world';

// Constants for setting up metadata server request
// See https://cloud.google.com/compute/docs/instances/verifying-instance-identity#request_signature
const functionURL = `https://${REGION}-${PROJECT_ID}.cloudfunctions.net/${RECEIVING_FUNCTION}`;
const metadataServerURL =
  'http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience=';
const tokenUrl = metadataServerURL + functionURL;

exports.proxy = async (req, res) => {
  // Fetch the token
  const tokenResponse = await get(tokenUrl, {
    headers: {
      'Metadata-Flavor': 'Google',
    },
  });
  const token = tokenResponse.data;
  console.log(`Token: ${token}`);

  // Provide the token in the request to the receiving function
  try {
    console.log(`Calling: ${functionURL}`);
    const functionResponse = await get(functionURL, {
      headers: {Authorization: `bearer ${token}`},
    });
    res.status(200).send(functionResponse.data);
  } catch (err) {
    console.error(JSON.stringify(err));
    res.status(500).send('An error occurred! See logs for more details.');
  }
};

它与文档中提出的几乎相同。我刚刚添加了一些日志,并且在记录错误之前将其字符串化。按照该页面上的说明,我还在我的函数中添加了服务帐户拥有该角色hello-world的权限:my-project-######@appspot.gserviceaccount.comroles/cloudfunctions.invoker

$ gcloud functions add-iam-policy-binding hello-world \
>   --member='serviceAccount:my-project-######@appspot.gserviceaccount.com' \
>   --role='roles/cloudfunctions.invoker'
bindings:
- members:
  - allUsers
  - serviceAccount:my-project--######@appspot.gserviceaccount.com
  role: roles/cloudfunctions.invoker
etag: ############
version: 1

但是,当我调用上面的代码时,我得到403 Access is forbidden. 我确定这是由hello-world函数返回的,因为我可以从代码中看到日志。我可以看到令牌,并且可以hello-world在日志中看到该函数的正确 URL。另外,我可以hello-world直接从 GCP 控制台调用该函数。这两个函数都是Trigger type: HTTP且唯一的hello-world函数是Ingress settings: Allow internal traffic only。另一个,Ingress settings: Allow all traffic

有人可以帮我理解有什么问题吗?

标签: google-cloud-platformgoogle-cloud-functions

解决方案


如果您的Hello world功能处于Allow internal only模式,这意味着:

仅允许来自同一项目中的 VPC 网络或 VPC Service Controls 边界的请求。所有其他请求都被拒绝。

要使用这些功能,您必须通过您的 VPC 调用它。为了这,


推荐阅读