[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-05。"],[[["\u003cp\u003eCloud Run functions (1st gen) use Cloud Build for deployments, and new projects may encounter insufficient permissions due to recent changes in Cloud Build's default service account behavior.\u003c/p\u003e\n"],["\u003cp\u003eYou can address potential permission issues by reviewing Cloud Build's guidance, adding the Cloud Build Account role to the default Compute Engine service account, or by creating a custom Cloud Build service account.\u003c/p\u003e\n"],["\u003cp\u003eDeploying functions with a custom service account allows for more control over service account selection for your VPC-SC perimeter, and allows more granular permission management for your function builds.\u003c/p\u003e\n"],["\u003cp\u003eTo use a custom service account, you must enable the IAM API, and grant the necessary roles, including \u003ccode\u003eroles/logging.logWriter\u003c/code\u003e, \u003ccode\u003eroles/artifactregistry.writer\u003c/code\u003e, \u003ccode\u003eroles/storage.objectViewer\u003c/code\u003e, and \u003ccode\u003eroles/storage.objectAdmin\u003c/code\u003e to the service account.\u003c/p\u003e\n"],["\u003cp\u003eTo deploy a function using the custom service account, use the \u003ccode\u003e--build-service-account\u003c/code\u003e flag with the \u003ccode\u003egcloud functions deploy\u003c/code\u003e command when deploying your function through the Google Cloud CLI.\u003c/p\u003e\n"]]],[],null,["# Custom Service Account for Cloud Build (1st gen)\n================================================\n\nCloud Run functions leverages Cloud Build when building and deploying\nyour Cloud Run function. By default, Cloud Run functions uses the default\nCloud Build service account as the principal when performing your build. Starting in July 2024, Cloud Build has changed the default behavior for how Cloud Build uses service accounts in new projects. This change is detailed in [Cloud Build Service Account Change](/build/docs/cloud-build-service-account-updates). As a result of this change, new projects deploying functions for the first time may be using a default Cloud Build service account with [insufficient permissions](/resource-manager/docs/organization-policy/restricting-service-accounts#disable_service_account_default_grants) for building a function. If you are impacted by this change you can do one of the following:\n\n- Review the Cloud Build guidance on changes to the default service account and [opt\n out of these changes](/build/docs/cloud-build-service-account-updates#what_do_you_need_to_do).\n\n- Add the Cloud Build Account role (`roles/cloudbuild.builds.builder`) to the default Compute Engine service account.\n\n- Create a custom Cloud Build service account for function deployments.\n\nThis document describes how to pass in a user-created service account, to be used by\nCloud Build, when deploying your function.\n\nYou can deploy functions with custom service accounts using the Google Cloud CLI,\nGoogle Cloud console, or the Cloud Run functions API.\n\nHere are some scenarios where you may want to provide a different service\naccount to be used when Cloud Build builds your function:\n\n- You want more control of which service accounts to add to your VPC-SC\n perimeter.\n\n- You want Cloud Build to run with different\n [permissions](/build/docs/cloud-build-service-account#default_permissions_of_service_account)\n than what the default service account has without having to revoke each\n permission individually.\n\n- You want to set granular Cloud Build permissions specifically for your\n functions, not share a Cloud Build service account that is optimized for\n other purposes.\n\n- Your organization disabled the usage of the default service account.\n\nEnable APIs\n-----------\n\nThis feature requires the IAM API to be enabled.\n\nUse the [Google Cloud CLI](/sdk/gcloud/reference) to enable the APIs needed to\ndeploy a Cloud Run function, or use\n[Google Cloud console](/endpoints/docs/openapi/enable-api#console): \n\n gcloud services enable iam.googleapis.com\n\nConfigure Service Account\n-------------------------\n\nThis document describes how to create a new service account and grant the\nrequired permissions. If you want to use an existing service account, you\nneed the email address of the service account you plan to use. See\n[configuring user-specified service accounts](/build/docs/securing-builds/configure-user-specified-service-accounts)\nfor details.\n\nYou can view your existing service accounts as follows, or use\nGoogle Cloud console: \n\n gcloud iam service-accounts list\n\n### Create Service Account\n\nUse the [Google Cloud CLI](/sdk/gcloud/reference) to create your service account\nor use Google Cloud console: \n\n gcloud iam service-accounts create \u003cvar translate=\"no\"\u003eSA_EMAIL\u003c/var\u003e\n\nReplace \u003cvar translate=\"no\"\u003eSA_EMAIL\u003c/var\u003e with the email address of your\n[service account](/build/docs/securing-builds/configure-user-specified-service-accounts).\n\n### Grant Permissions\n\nThe service account you use will need the following roles:\n\n- [`roles/logging.logWriter`](/iam/docs/understanding-roles#logging-roles)--- Required to store build logs in Cloud Logging.\n- [`roles/artifactregistry.writer`](/artifact-registry/docs/access-control#grant)--- Required to store build images in Artifact Registry. For the default behavior, the service account needs access to repositories named \"gcf-artifacts\" and \"cloud-run-source-deploy\". Access to the repositories can be set on the repository's IAM policy. You can alternatively provide your own artifact repository through `dockerRepository` field.\n- [`roles/storage.objectViewer`](/iam/docs/understanding-roles#cloud-storage-roles)---\n Required to retrieve the function source from the Cloud Storage bucket,\n and to store build images in Container Registry. For the default behavior, the service account needs access to buckets named \"gcf-sources-\\*\". This can be accomplished by adding an IAM condition to the role grant such as:\n `(resource.type == \"storage.googleapis.com/Object\" && (resource.name.startsWith(\"gcf-sources-\")))`\n\n- [`roles/storage.objectAdmin`](/iam/docs/understanding-roles#cloud-storage-roles)---\n Required for container registry (now deprecated). It needs access to buckets named `*.artifacts.`\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e`.appspot.com` is needed to access the container registry.\n\nGrant the following roles using the\n[Google Cloud CLI](/iam/docs/granting-changing-revoking-access#iam-grant-single-role-gcloud),\nor use\n[Google Cloud console](/iam/docs/granting-changing-revoking-access#iam-grant-single-role-console). \n\n gcloud projects add-iam-policy-binding \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003ePROJECT_ID\u003c/span\u003e\u003c/var\u003e \\\n --member=serviceAccount:\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eSA_EMAIL\u003c/span\u003e\u003c/var\u003e \\\n --role=roles/logging.logWriter\n\n gcloud projects add-iam-policy-binding \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003ePROJECT_ID\u003c/span\u003e\u003c/var\u003e \\\n --member=serviceAccount:\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eSA_EMAIL\u003c/span\u003e\u003c/var\u003e \\\n --role=roles/artifactregistry.writer\n\n gcloud projects add-iam-policy-binding \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003ePROJECT_ID\u003c/span\u003e\u003c/var\u003e \\\n --member=serviceAccount:\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eSA_EMAIL\u003c/span\u003e\u003c/var\u003e \\\n --role=roles/storage.objectViewer\n\n gcloud projects add-iam-policy-binding \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003ePROJECT_ID\u003c/span\u003e\u003c/var\u003e \\\n --member=serviceAccount:\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eSA_EMAIL\u003c/span\u003e\u003c/var\u003e \\\n --role=roles/storage.objectAdmin\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: Your [Google Cloud project ID](/sdk/gcloud/reference/projects).\n- \u003cvar translate=\"no\"\u003eSA_EMAIL\u003c/var\u003e: The email address of your [service account](/build/docs/securing-builds/configure-user-specified-service-accounts).\n\n| **Note:** If you are using a service account from a different project, see [Configure user-specified service accounts](/build/docs/securing-builds/configure-user-specified-service-accounts).\n\nVPC Service Controls considerations\n-----------------------------------\n\nIf you have a VPC Service Controls perimeter protecting both your project and the Cloud Run functions API, and if you're\nusing the Compute Engine default service account as the **Cloud Build Service Account** role for Cloud Run functions,\nyou must create the following ingress rules:\n\n- Allow Compute Engine default service account ingress to all methods on both Cloud Storage and Cloud Logging APIs.\n- Allow the service account `service-[PROJECT_NUMBER]@gcf-admin-robot.iam.gserviceaccount.com` ingress to all methods on both Cloud Storage and Cloud Logging APIs.\n\nDeploy a function with a custom service account\n-----------------------------------------------\n\nYou can use the [Google Cloud CLI](/sdk/gcloud/reference/functions/deploy)\nto deploy a function that uses a custom service account for Cloud Build:\n\n- The `--build-service-account` flag specifies an IAM service account whose credentials will be used for the build step. If a custom service account is not provided, the function uses the project's default service account for Cloud Build.\n- You can optionally use a [private pool](/build/docs/private-pools/run-builds-in-private-pool), which you specify using the [`--build-worker-pool`](/sdk/gcloud/reference/functions/deploy#--build-worker-pool) flag.\n\n\n gcloud functions deploy \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eFUNCTION_NAME\u003c/span\u003e\u003c/var\u003e \\\n --no-gen2 \\\n --region=\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eREGION\u003c/span\u003e\u003c/var\u003e \\\n --project=\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003ePROJECT_ID\u003c/span\u003e\u003c/var\u003e \\\n --runtime=\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eRUNTIME\u003c/span\u003e\u003c/var\u003e \\\n --entry-point=\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eCODE_ENTRYPOINT\u003c/span\u003e\u003c/var\u003e \\\n --build-service-account=projects/\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003ePROJECT_ID\u003c/span\u003e\u003c/var\u003e/serviceAccounts/\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eSA_EMAIL\u003c/span\u003e\u003c/var\u003e \\\n --memory=256Mi \\\n --trigger-http \\\n --source=.\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eFUNCTION_NAME\u003c/var\u003e: The name under which you deployed your function.\n- \u003cvar translate=\"no\"\u003eREGION\u003c/var\u003e: The name of the [Google Cloud region](/functions/1stgendocs/locations) where you want to deploy your function (for example, `us-west1`).\n- \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: Your [Google Cloud project ID](/sdk/gcloud/reference/projects).\n- \u003cvar translate=\"no\"\u003eRUNTIME\u003c/var\u003e: The runtime ID of a [supported runtime version](/static/functions/docs/runtime-support#node.js) to run your function, for example, `nodejs18`.\n- \u003cvar translate=\"no\"\u003eCODE_ENTRYPOINT\u003c/var\u003e: The [entry point](/functions/1stgendocs/writing#entry-point) to your function in your source code. This is the code that will be executed when your function runs.\n- \u003cvar translate=\"no\"\u003eSA_EMAIL\u003c/var\u003e: The email address of your [service account](/build/docs/securing-builds/configure-user-specified-service-accounts).\n\n| **Note:** There is no way to set a single service account across all functions."]]