Manage function resources using custom constraints

As an organization administrator, you can create custom constraints for Cloud Run functions. Organization policies enforce these custom constraints at the project, folder, or organization level.

Organization Policy provides predefined constraints for various Google Cloud services. However, if you want more granular, customizable control over the specific fields that are restricted in your organization policies, you can also create custom organization policies.

This page details how to create custom constraints for functions created using Cloud Functions v2 APIs and enforce them at the project level. For information about custom organization policies, see Creating and managing custom organization policies.

Benefits

  • Cost management: use organization policies to restrict the VM instance and disk sizes and types that can be used in your organization. You can also restrict the machine family that is used for the VM instance.
  • Security, compliance, and governance:
    • To enforce security requirements, you can require specific firewall port rules on VMs.
    • To support hardware isolation or licensing compliance, you can require all VMs within a specific project or folder to run on sole-tenant nodes.
    • To govern automation scripts, you can use custom organization policies to verify that labels match the necessary expressions.

Policy inheritance

When an organization policy is enforced on a resource, all descendants of the resource inherit the organization policy as well. For example, if you enforce a policy on a folder, Google Cloud enforces the policy on all projects in the folder. To learn more about this behavior and how to change it, refer to Hierarchy evaluation rules.

Pricing

The Organization Policy Service, including predefined and custom organization policies, is offered at no charge.

Limitations

The following limitations apply to custom organization policies:

  • Not enforced for VM instance names when you use the bulk insert API.
  • Only enforced on the CREATE method for Compute Engine resources.
  • Only available on Cloud Functions v2 APIs. They can't be applied on Cloud Run functions (1st gen).
  • Only protect functions when using the Cloud Functions v2 APIs. Cloud Run functions can also be modified from the Cloud Run API as well. For additional protection, you might need to also apply custom constraints on Cloud Run as well.

Before you begin

Required roles

Create a custom constraint

A custom constraint is defined in a YAML file by the resources, methods, conditions, and actions that are supported by the service on which you are enforcing the organization policy. Conditions for your custom constraints are defined using Common Expression Language (CEL). For more information about how to build conditions in custom constraints using CEL, see the CEL section of Creating and managing custom constraints.

To specify a custom constraint that denies all function creation with a maximum instance greater than 150, do the following:

  • Create a new maxInstanceConstraint.yaml file with the following content:

    name: organizations/ORGANIZATION_ID/customConstraints/CONSTRAINT_NAME
    resourceTypes:
    - cloudfunctions.googleapis.com/Function
    methodTypes: 
    - CREATE
    - UPDATE
    condition: resource.serviceConfig.maxInstanceCount > 150
    actionType: DENY
    displayName: Deny functions with max instance count greater than 150
    description: Functions cannot be created with a max instance count greater than 150
    

Replace ORGANIZATION_ID with your organization ID, such as 123456789.

For more information, see Defining custom constraints.

Set up a custom constraint

After you have created the YAML file for a new custom constraint, you must set it up to make it available for organization policies in your organization. To set up a custom constraint, use the gcloud org-policies set-custom-constraint command:
gcloud org-policies set-custom-constraint CONSTRAINT_PATH
Replace CONSTRAINT_PATH with the full path to your custom constraint file. For example, /home/user/customconstraint.yaml. Once completed, you will find your custom constraints as available organization policies in your list of Google Cloud organization policies. To verify that the custom constraint exists, use the gcloud org-policies list-custom-constraints command:
gcloud org-policies list-custom-constraints --organization=ORGANIZATION_ID
Replace ORGANIZATION_ID with the ID of your organization resource. For more information, see Viewing organization policies.

Enforce a custom organization policy

You can enforce a boolean constraint by creating an organization policy that references it, and applying that organization policy to a Google Cloud resource.

Console

To enforce a boolean constraint:

  1. In the Google Cloud console, go to the Organization policies page.

    Go to Organization policies

  2. Select the project picker at the top of the page.
  3. From the project picker, select the project for which you want to set the organization policy.
  4. Select your constraint from the list on the Organization policies page. The Policy details page for that constraint should appear.
  5. To configure the organization policy for this resource, click Manage policy.
  6. On the Edit policy page, select Override parent's policy.
  7. Click Add a rule.
  8. Under Enforcement, select whether enforcement of this organization policy should be on or off.
  9. Optionally, to make the organization policy conditional on a tag, click Add condition. Note that if you add a conditional rule to an organization policy, you must add at least one unconditional rule or the policy cannot be saved. For more details, see Setting an organization policy with tags.
  10. If this is a custom constraint, you can click Test changes to simulate the effect of this organization policy. For more information, see Test organization policy changes with Policy Simulator.
  11. To finish and apply the organization policy, click Set policy. The policy will take up to 15 minutes to take effect.

gcloud

To create an organization policy that enforces a boolean constraint, create a policy YAML file that references the constraint:

      name: projects/PROJECT_ID/policies/CONSTRAINT_NAME
      spec:
        rules:
        - enforce: true
    

Replace the following:

  • PROJECT_ID: the project on which you want to enforce your constraint.
  • CONSTRAINT_NAME: the name you defined for your custom constraint. For example, custom.cloudFunctionsMaxInstanceLimit.

To enforce the organization policy containing the constraint, run the following command:

    gcloud org-policies set-policy POLICY_PATH
    

Replace POLICY_PATH with the full path to your organization policy YAML file. The policy will take up to 15 minutes to take effect.

Test the custom organization policy

To create a function with a maximum instance of 151, run the following command:

gcloud functions deploy FUNCTION_NAME \
  ...
  --max-instances 151

The following output is displayed:

 Operation denied by custom org policy on resource: ["customConstraints/custom.cloudFunctionsMaxInstanceLimit": "Cloud Functions cannot be created with a max instance count greater than 150."]

Cloud Run functions-supported resources and operations

Resource Type Method Types API Reference
cloudfunctions.googleapis.com/Function CREATE, UPDATE projects.locations.functions

Common organization policy examples

The following table provides the syntax of some custom organization policies that you might find useful:

Description Constraint syntax
Prevent functions from being created with a specific language

    name: organizations/ORGANIZATION_IDcustomConstraints/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
Require functions to use a specific worker pool
    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 
Replace WORKER_POOL with the name of your Cloud Build worker pool.
Require that functions store all container images in a specific image repository
    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
Replace REPO_PATH with the URI of the image repository URL that you want all functions to store their container images in.

What's next