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

本頁面提供額外資訊,說明如何針對使用 gcloud functions 指令或 Cloud Functions v2 API 建立的函式設定自訂限制。

如果您已使用 Cloud Run 建立或部署函式,請參閱 Cloud Run 的「管理專案的自訂限制」指南,進一步瞭解如何使用自訂限制。

限制

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

  • 使用大量插入 API 時,不會強制執行 VM 執行個體名稱。
  • 僅針對 Compute Engine 資源的 CREATE 方法強制執行。
  • 僅適用於 Cloud Functions v2 API。但無法套用至 Cloud Run 函式 (第 1 代)。
  • 僅在使用 Cloud Functions v2 API 時保護函式。您也可以透過 Cloud Run API 修改 Cloud Run 函式。為提供額外保護,您可能需要在 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。

後續步驟