使用自訂限制條件管理函式資源

本頁提供補充資訊,說明如何為使用 Cloud Functions v2 API 建立的函式設定自訂限制,例如透過 gcloud functions 指令。

限制

使用自訂機構政策時,Cloud Functions v2 API 函式有下列限制:

  • 僅適用於 Cloud Functions v2 API
  • 這些配額不適用於 Cloud Run functions (第 1 代)。
  • 僅在使用 Cloud Functions v2 API 時保護函式。您也可以透過 Cloud Run API 修改 Cloud Run functions。如要進一步保護服務,您可能也需要在 Cloud Run 上套用自訂限制

常見的機構政策範例

下表提供一些實用的自訂機構政策語法:

說明 限制語法
禁止以特定語言建立函式
    name: organizations/ORGANIZATION_ID/customConstraints/custom.cloudFunctionRuntimeBlock
    resource_types: cloudfunctions.googleapis.com/Function
    method_types:
      - CREATE
      - UPDATE
    condition: resource.buildConfig.runtime == "python312"
    action_type: DENY
    display_name: Deny functions using Python 3.12
    description: Functions cannot be created with Python 3.12 as the language runtime
要求函式使用特定工作站集區
    name: organizations/ORGANIZATION_ID/customConstraints/custom.cloudFunctionsWorkerPool
    resource_types: cloudfunctions.googleapis.com/Function
    method_types:
      - CREATE
      - UPDATE
    condition: resource.buildConfig.workerPool == "WORKER_POOL"
    action_type: DENY
    display_name: Require worker pool
    description: Functions must use a worker pool
WORKER_POOL 替換為 Cloud Build 工作站集區的名稱。
要求函式將所有容器映像檔儲存在特定映像檔存放區
    name: organizations/ORGANIZATION_ID/customConstraints/custom.cloudFunctionsRepository
    resource_types: cloudfunctions.googleapis.com/Function
    method_types:
      - CREATE
      - UPDATE
    condition: resource.buildConfig.dockerRepository.startsWith("REPO_PATH")
    action_type: DENY
    display_name: Image repository constraint
    description: Functions must push images to a central image repository under REPO_PATH
REPO_PATH 替換為圖片存放區網址的 URI, 所有函式都會將容器映像檔儲存在該處。

後續步驟