Set maximum number of instances (services)

This page describes how to set the maximum number of instances that can be used for your Cloud Run service.

Specifying maximum instances in Cloud Run lets you limit the scaling of your service in response to incoming requests, although this maximum setting can be exceeded for a brief period due to circumstances such as traffic spikes.

You can use this setting as a way to control your costs or to limit the number of connections to a backing service, such as to a database.

For information about the maximum instance limits that might apply to your service, refer to Maximum instances limits.

For more information on the way Cloud Run autoscales container instances, refer to Instance autoscaling.

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.

Set and update maximum instances

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.

By default, Cloud Run services are configured to scale out to a maximum of 100 instances.

You can change the maximum instances setting using the Google Cloud console, the Google Cloud CLI, 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

    • In the field labelled Maximum number of instances, specify the desired maximum number of instances, using any integer value from 1 to the maximum limit
  5. Click Create or Deploy.

gcloud

You can update the maximum number of instances of a given service by using the following command:

gcloud run services update SERVICE --max-instances MAX-VALUE

Replace

  • SERVICE with the name of your service and
  • MAX-VALUE with the desired maximum number of container instances, using any integer value from 1 to the maximum limit. Specify default to clear any maximum instance setting and restore the default of 100 instances.

You can also set the maximum number of instances during deployment using the command:

gcloud run deploy --image IMAGE_URL --max-instances MAX-VALUE

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 .
  • MAX-VALUE with the desired maximum number of container instances.

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 autoscaling.knative.dev/maxScale: attribute:

    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: SERVICE
    spec:
      template:
        metadata:
          annotations:
            autoscaling.knative.dev/maxScale: 'MAX-INSTANCE'
          name: REVISION

    Replace

    • SERVICE with the name of your Cloud Run service
    • MAX-INSTANCE with the required maximum number.
    • 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 maximum number of instances of 10 under template.scaling. Replace 10 with your required maximum number of instances.

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

  deletion_protection = false # set to "true" in production

  template {
    containers {
      image = "us-docker.pkg.dev/cloudrun/container/hello"
    }
    scaling {
      # Max instances
      max_instance_count = 10
    }
  }
}

View maximum instances settings

To view the current maximum instances 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 maximum instances setting is listed under the Container tab.

gcloud

  1. Use the following command:

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