Configure CPU limits

This page describes how to specify the number of CPUs to use for each Cloud Run instance. By default, Cloud Run container instances are limited to 1 CPU. You can increase or decrease this value as described in this page.

This page also describes how to enable or disable startup CPU boost, a feature that temporarily increases CPU allocation during instance startup in order to reduce startup latency.

Set and update CPU limits

By default, each instance is limited to 1 CPU. You can increase this using any integer value up to a maximum of 8 CPUs.

CPU and memory

The following are minimum memory requirements for CPUs:

CPUs Minimum memory
4 2 GiB
6 4 GiB
8 4 GiB

Alternatively, if you want to use less than 1 CPU, you can select any value between 0.08 and 1, in increments of 0.01. Any value above 1 must be an integer value. If you use less than 1 CPU, the following requirements are enforced:

Setting Requirement
Memory A minimum of 0.5 CPU is needed to set a memory limit greater than 512MiB.
A minimum of 1 CPU is needed to set a memory limit greater than 1GiB.
Concurrency Maximum concurrency must be set to 1.
Billing The billing settings must be set to request-based billing.
Execution environment You must use the first generation execution environment.

Required roles

To get the permissions that you need to configure and deploy Cloud Run services, ask your administrator to grant you the following IAM roles:

For a list of IAM roles and permissions that are associated with Cloud Run, see Cloud Run IAM roles and Cloud Run IAM permissions. If your Cloud Run service interfaces with Google Cloud APIs, such as Cloud Client Libraries, see the service identity configuration guide. For more information about granting roles, see deployment permissions and manage access.

Configure CPU limits

Any configuration change leads to the creation of a new revision. Subsequent revisions will also automatically get this configuration setting unless you make explicit updates to change it.

You can set CPU limits using the Google Cloud console, the gcloud command line, or a YAML file when you create a new service or deploy a new revision:

Console

  1. In the Google Cloud console, go to Cloud Run:

    Go to Cloud Run

  2. Click Deploy container and select Service to configure a new service. If you are configuring an existing service, click the service, then click Edit and deploy new revision.

  3. If you are configuring a new service, fill out the initial service settings page, then click Container(s), volumes, networking, security to expand the service configuration page.

  4. Click the Container tab.

    image

    • Select the desired CPU limit from the dropdown list, using Custom if you want to use less than 1 CPU. Select a value of 1, 2, 4, 6, or 8 CPUs, or for less than 1 CPU, specify a value from 0.08 to less than 1.00, in increments of 0.01. (See the table under Setting and updating CPU limits for required settings.)
  5. Click Create or Deploy.

gcloud

You can update the CPU limits for a given service by using the following command:

gcloud run services update SERVICE --cpu CPU

Replace

  • SERVICE with the name of your service
  • CPU with the desired CPU limit. Specify the value 1, 2, 4, 6, or 8 CPUs, or for less than 1 CPU, specify a value from 0.08 to less than 1.00, in increments of 0.01. (See the table under Setting and updating CPU limits for required settings.)

You can also set CPU during deployment using the command:

gcloud run deploy --image IMAGE_URL --cpu CPU

Replace

  • IMAGE_URL with a reference to the container image, for example, us-docker.pkg.dev/cloudrun/container/hello:latest. If you use Artifact Registry, the repository REPO_NAME must already be created. The URL has the shape LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG .
  • CPU with the value 1, 2, 4, 6, or 8 CPUs, or for less than 1 CPU, specify a value from 0.08 to less than 1.00, in increments of 0.01. (See the table under Setting and updating CPU limits for required settings.)

YAML

  1. If you are creating a new service, skip this step. If you are updating an existing service, download its YAML configuration:

    gcloud run services describe SERVICE --format export > service.yaml
  2. Update the cpu attribute:

    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: SERVICE
    spec:
      template:
        metadata:
          name: REVISION
        spec:
          containers:
          - image: IMAGE
            resources:
              limits:
                cpu: CPU

    Replace

    • SERVICE with the name of your Cloud Run service
    • IMAGE_URL with a reference to the container image, for example, us-docker.pkg.dev/cloudrun/container/hello:latest. If you use Artifact Registry, the repository REPO_NAME must already be created. The URL has the shape LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG
    • CPU with the desired CPU limit value. Specify the value 1, 2, 4, 6, or 8 CPUs, or for less than 1 CPU, specify a value from 0.08 to less than 1.00, in increments of 0.01. (See the table under Setting and updating CPU limits for required settings.)
    • REVISION with a new revision name or delete it (if present). If you supply a new revision name, it must meet the following criteria:
      • Starts with SERVICE-
      • Contains only lowercase letters, numbers and -
      • Does not end with a -
      • Does not exceed 63 characters
  3. Create or update the service using the following command:

    gcloud run services replace service.yaml

Terraform

To learn how to apply or remove a Terraform configuration, see Basic Terraform commands.

The following google_cloud_run_v2_service resource specifies a CPU limit under template.containers.resources.limits. Replace 1 with your desired CPU count.

resource "google_cloud_run_v2_service" "default" {
  name     = "cloudrun-service-cpu"
  location = "us-central1"

  deletion_protection = false # set to "true" in production

  template {
    containers {
      image = "us-docker.pkg.dev/cloudrun/container/hello"
      resources {
        limits = {
          # CPU usage limit
          cpu = "1"
        }
      }
    }
  }
}

Set startup CPU boost

The startup CPU boost feature for revisions provides additional CPU during instance startup time and for 10 seconds after the instance has started.

The actual CPU boost varies depending on your CPU limit settings:

CPU limit Boosted CPU
0-1 2
2 4
4 8
6 8
8 8

You are charged for the allocated boosted CPU for the duration of the container startup time. For example, if your container startup time is 15 seconds, and you allocate 2 CPU, with startup CPU boost, you'll be charged for 4 CPU during the (possibly shorter) instance startup time, including the 10 seconds after your container finished starting, and for 2 CPU during the rest of the container lifecycle.

You can enable or disable startup CPU boost using Google Cloud console or Google Cloud CLI.

Console

  1. In the Google Cloud console, go to Cloud Run:

    Go to Cloud Run

  2. Click Deploy container and select Service to configure a new service. If you are configuring an existing service, click the service, then click Edit and deploy new revision.

  3. If you are configuring a new service, fill out the initial service settings page, then click Container(s), volumes, networking, security to expand the service configuration page.

  4. Click the Container tab.

    image

    • To enable startup CPU boost select the check box Startup CPU boost. To disable this feature, deselect the checkbox.
  5. Click Create or Deploy.

gcloud

  1. You can enable startup CPU boost for a given service by using the following command:

    gcloud run services update SERVICE --cpu-boost

    Replace SERVICE with the name of your service

    You can enable startup CPU boost during deployment using the command:

    gcloud run deploy --image IMAGE_URL --cpu-boost

    Replace IMAGE_URL with a reference to the container image, for example, us-docker.pkg.dev/cloudrun/container/hello:latest. If you use Artifact Registry, the repository REPO_NAME must already be created. The URL has the shape LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG .

  2. You can disable startup CPU boost for a given service by using the following command:

    gcloud run services update SERVICE --no-cpu-boost

    Replace SERVICE with the name of your service

    You can disable startup CPU boost during deployment using the command:

    gcloud run deploy --image IMAGE_URL --no-cpu-boost

YAML

  1. If you are creating a new service, skip this step. If you are updating an existing service, download its YAML configuration:

    gcloud run services describe SERVICE --format export > service.yaml
  2. Update the run.googleapis.com/startup-cpu-boost attribute by specifying 'true' to enable startup CPU boost or 'false' to disable:

    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: SERVICE
    spec:
      template:
        metadata:
          annotations:
            run.googleapis.com/startup-cpu-boost: 'true'

    Replace

    • SERVICE with the name of your Cloud Run service
  3. Create or update the service using the following command:

    gcloud run services replace service.yaml

View CPU settings

To view the current CPU settings for your Cloud Run service:

Console

  1. In the Google Cloud console, go to Cloud Run:

    Go to Cloud Run

  2. Click the service you are interested in to open the Service details page.

  3. Click the Revisions tab.

  4. In the details panel at the right, the CPU setting is listed under the Container tab.

gcloud

  1. Use the following command:

    gcloud run services describe SERVICE
  2. Locate the CPU setting in the returned configuration.