排查 Cloud Run functions 问题

本文档介绍了如何在使用 Cloud Run functions 时排查错误消息并解决问题。 如果您是使用 Cloud Run 创建或部署的函数,请参阅 Cloud Run 问题排查

部署

本部分列出了您可能遇到的部署问题,并提供了有关如何解决这些问题的建议。您在部署期间可能遇到的许多问题都与角色和权限或错误配置有关。

您可以使用 Identity and Access Management 授权身份对使用 Cloud Functions v2 API 创建的函数执行管理操作,例如使用 gcloud functions、REST API 或 Terraform 执行操作。管理操作包括创建、更新和删除函数。如需了解详情,请参阅使用 IAM 授予访问权限

部署函数时,用户缺少运行时服务账号的权限

每个函数都与一个服务账号关联,该服务账号充当函数访问其他资源时的身份。此运行时服务账号可以是默认服务账号,也可以是用户管理的服务账号。在多个函数正在访问不同资源的环境中,通常的做法是使用每个函数身份,并使用命名的运行时服务账号而不是默认的运行时服务账号 (PROJECT_NUMBER-compute@developer.gserviceaccount.com)。

如需使用运行时服务账号,部署者必须具有该服务账号的 iam.serviceAccounts.actAs 权限。创建非默认运行时服务账号的用户将自动获得此权限,但其他部署者必须由某一用户授予此权限。

使用 Project Viewer、Cloud Functions Developer 或 Cloud Functions Admin 角色为用户分配运行时服务账号的 iam.serviceAccounts.actAs 权限。

错误消息

控制台

  You must have the iam.serviceAccounts.actAs permission on the selected service account. To obtain this permission, you can grant a role that includes it like the Service Account User role, on the project.

gcloud

默认服务账号:

ERROR: (gcloud.functions.deploy) ResponseError: status=[403], code=[Ok], message=[Caller is missing permission 'iam.serviceaccounts.actAs' on service account projects/-/serviceAccounts/PROJECT_NUMBER-compute@developer.gserviceaccount.com.Grant the role 'roles/iam.serviceAccountUser' to the caller on the service account projects/-/serviceAccounts/PROJECT_NUMBER-compute@developer.gserviceaccount.com.You can do that by running 'gcloud iam service-accounts add-iam-policy-binding projects/-/serviceAccounts/PROJECT_NUMBER-compute@developer.gserviceaccount.com --member MEMBER --role roles/iam.serviceAccountUser'where MEMBER has a prefix like 'user:' or 'serviceAccount:'

非默认服务账号:

ERROR: (gcloud.functions.deploy) ResponseError: status=[403], code=[Ok], message=[Caller is missing permission 'iam.serviceaccounts.actAs' on service account projects/-/serviceAccounts/
SERVICE_ACCOUNT_NAME@PROJECT_ID
.iam.gserviceaccount.com.Grant the role 'roles/iam.serviceAccountUser' to the caller on the service account projects/-/serviceAccounts/
SERVICE_ACCOUNT_NAME@PROJECT_ID
.iam.gserviceaccount.com.You can do that by running 'gcloud iam service-accounts add-iam-policy-binding projects/-/serviceAccounts/
SERVICE_ACCOUNT_NAME@PROJECT_ID
.iam.gserviceaccount.com --member MEMBER --role roles/iam.serviceAccountUser'where MEMBER has a prefix like 'user:' or 'serviceAccount:'

解决方案

在默认或非默认运行时服务账号上为用户分配 Service Account User 角色 (roles/iam.serviceAccountUser)。此角色可提供 iam.serviceAccounts.actAs 权限。

部署事件驱动的函数时,部署服务账号缺少 Pub/Sub 权限

执行管理操作时,Cloud Functions 服务会使用 Cloud Functions Service Agent 服务账号 (service-PROJECT_NUMBER@gcf-admin-robot.iam.gserviceaccount.com)。 默认情况下,此账号拥有 Cloud Functions cloudfunctions.serviceAgent 角色。为了部署事件驱动的函数,Cloud Functions 服务必须访问 Pub/Sub 以配置主题和订阅。如果您更改了分配给服务账号的角色,但未授予适当的权限,则 Cloud Functions 服务将无法访问 Pub/Sub,因此部署将失败。

错误消息

控制台

Validation failed for trigger projects/PROJECT_ID/locations/LOCATION/triggers/FUNCTION_NAME-EVENTARC_ID: Permission "iam.serviceAccounts.ActAs" denied on "EndUserCredentials to PROJECT_NUMBER-compute@developer.gserviceaccount.com"

gcloud

ERROR: (gcloud.functions.deploy) ResponseError: status=[403], code=[Ok], message=[Validation failed for trigger projects/test-project-356312/locations/LOCATION/triggers/FUNCTION_NAME-EVENTARC_ID: Permission "iam.serviceAccounts.ActAs" denied on "EndUserCredentials to PROJECT_NUMBER-compute@developer.gserviceaccount.com"]

解决方案

您可以重置服务账号为默认的 cloudfunctions.serviceAgent 角色。

默认运行时服务账号不存在

如果您未指定用户管理的运行时服务账号,Cloud Functions 会使用默认的计算服务账号作为运行时服务账号。如果您删除默认账号但未指定用户管理的账号,部署将会失败。

错误消息

gcloud

ERROR: (gcloud.functions.deploy) ResponseError: status=[404], code=[Ok], message=[Service account projects/-/serviceAccounts/PROJECT_NUMBER-compute@developer.gserviceaccount.com was not found.]

解决方案

如需解决此问题,请按以下任一解决方案操作:

部署函数时,Cloud Functions Service Agent 服务账号缺少项目存储桶权限

Cloud Run functions 函数只能由来自同一 Google Cloud Platform 项目中的 Cloud Storage 存储桶的事件触发。此外,Cloud Functions Service Agent 服务账号 (service-PROJECT_NUMBER@gcf-admin-robot.iam.gserviceaccount.com) 需要项目的 cloudfunctions.serviceAgent 角色。

错误消息

控制台

ERROR: (gcloud.functions.deploy) ResponseError: status=[403], code=[Ok], message=[Validation failed for trigger projects/PROJECT_ID/locations/LOCATION/triggers/FUNCTION_NAME-EVENTARC_ID: Permission "iam.serviceAccounts.ActAs" denied on "EndUserCredentials to PROJECT_NUMBER-compute@developer.gserviceaccount.com"]

gcloud

ERROR: (gcloud.functions.deploy) ResponseError: status=[403], code=[Ok], message=[Validation failed for trigger projects/<project-id>/locations/LOCATION/triggers/FUNCTION_NAME-EVENTARC_ID: Permission "iam.serviceAccounts.ActAs" denied on "EndUserCredentials to PROJECT_NUMBER-compute@developer.gserviceaccount.com"]

解决方案

如需解决此问题,请重置此服务账号为默认角色。

拥有 Project Editor 角色的用户无法公开函数

Project Editor 角色具有管理项目内资源的广泛权限,但它本身并不授予公开 Cloud Functions 的权限。部署该函数的用户或服务将需要 run.services.setIamPolicy 权限。

错误消息

gcloud

ERROR: (gcloud.run.services.add-iam-policy-binding) PERMISSION_DENIED: Permission 'run.services.setIamPolicy' denied on resource 'projects/PROJECT_ID/locations/LOCATION/functions/FUNCTION_NAME' (or resource may not exist).

解决方案

您可以:

使用资源位置限制条件组织政策时,函数部署失败

如果您的组织使用资源位置限制条件政策,则会限制将函数部署在受该政策限制的区域中。在 Google Cloud 控制台中部署函数时,可选区域下拉列表中不会显示受限区域。

错误消息

gcloud

ResponseError: status=[400], code=[Ok], message=["LOCATION" violates constraint "constraints/gcp.resourceLocations" on the resource "projects/PROJECT_ID/locations/LOCATION/functions/FUNCTION_NAME".]

解决方案

您可以通过资源位置限制条件的 allowed_valuesdenied_values 列表添加或移除位置,以便成功部署。

执行函数的全局范围时函数部署失败

此错误表示您的代码存在问题。部署流水线已完成函数部署,但最后一步完成了,即向该函数发送健康检查。此健康检查用于执行函数的全局范围,这可能会导致异常、崩溃或超时。全局范围是您通常加载的库并初始化客户端的位置。

错误消息

在 Cloud Logging 日志中:

Could not create or update Cloud Run service FUNCTION_NAME, Container Healthcheck failed. Revision REVISION_NAMEE is not ready and cannot serve traffic. The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable. Logs for this revision might contain more information.

解决方案

如需解决此问题,请按以下任一解决方案操作:

  • 如需查看更详细的错误消息,请查看函数的构建日志

  • 如果不清楚函数为何无法执行其全局范围,请考虑使用全局变量延迟加载,暂时将代码移动到请求调用中。这样,您就可以围绕您的客户端库添加额外的日志语句,这些语句可能会在其实例化时超时(尤其是当调用其他服务时),也可能完全崩溃/抛出异常。

  • 此外,请尝试增加函数超时。Cloud Run functions 的超时限制越长,初始化的喘息空间就越大,并且在 Cloud Run 环境中提供了可扩缩的资源分配,从而有可能缓解资源耗尽所造成的这个问题。

  • 源代码必须包含通过 Cloud 控制台gcloud 在部署中正确指定的入口点函数。

具有 Viewer 角色的用户无法部署函数

拥有 Project Viewer 或 Cloud Functions Viewer 角色的用户对函数和函数详细信息具有只读权限,并且无法部署新函数。创建函数功能在Google Cloud 控制台中呈灰显状态,并显示以下错误:

错误消息

gcloud

ERROR: (gcloud.functions.deploy) ResponseError: status=[403], code=[Ok], message=[Permission 'cloudfunctions.functions.generateUploadUrl' denied on 'projects/PROJECT_ID/locations/LOCATION/functions']

解决方案

为用户分配 Cloud Functions Developer 角色。

Build 服务账号缺少权限

错误消息