After you've created a Cloud Run worker pool, you can use [Workflows](https://docs.cloud.google.com/workflows/docs/overview.md.txt) to automate its scaling. This lets you adjust the instance count based on external events, custom logic, or schedules.

<br />

## Before you begin

The following list describes the requirements when using Workflows to
deploy and execute a Cloud Run worker pools workflow:

1.


   Enable the Cloud Run Admin API and Workflows APIs, if any are not already enabled.


   **Roles required to enable APIs**


   To enable APIs, you need the `serviceusage.services.enable` permission. If you
   created the project, then you likely already have this permission through the
   Owner role (`roles/owner`). Otherwise, you can get this permission through the
   Service Usage Admin role (`roles/serviceusage.serviceUsageAdmin`).
   [Learn how to grant roles](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt).

   [Enable the APIs](https://console.cloud.google.com/apis/enableflow?apiid=run.googleapis.com,workflows.googleapis.com)

### Required roles


To get the permissions that
you need to deploy and execute Cloud Run worker pools with Workflows,

ask your administrator to grant you the
following IAM roles:

- [Cloud Run Admin](https://docs.cloud.google.com/iam/docs/roles-permissions/run.md.txt#run.admin) (`roles/run.admin`) on the Cloud Run worker pool
- [Workflows Editor](https://docs.cloud.google.com/iam/docs/roles-permissions/workflows.md.txt#workflows.editor) (`roles/workflows.editor`) on the project
- [Service Account User](https://docs.cloud.google.com/iam/docs/roles-permissions/iam.md.txt#iam.serviceAccountUser) (`roles/iam.serviceAccountUser`) on the service identity


To allow Workflows to update the Cloud Run worker pool,
the service account that is used to deploy your workflow must have the following
role:

- [Cloud Run Admin](https://docs.cloud.google.com/iam/docs/roles-permissions/run.md.txt#run.admin) (`roles/run.admin`)

For a list of IAM roles and permissions that are associated with
Cloud Run, see
[Cloud Run IAM roles](https://docs.cloud.google.com/run/docs/reference/iam/roles.md.txt)
and [Cloud Run IAM permissions](https://docs.cloud.google.com/run/docs/reference/iam/permissions.md.txt).
If your Cloud Run worker pool interfaces with
Google Cloud APIs, such as Cloud Client Libraries, see the
[service identity configuration guide](https://docs.cloud.google.com/run/docs/configuring/workerpools/service-identity.md.txt).
For more information about granting roles, see
[deployment permissions](https://docs.cloud.google.com/run/docs/reference/iam/roles.md.txt#additional-configuration)
and [manage access](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt).

## Create a Cloud Run worker pool

[Create a Cloud Run worker pool](https://docs.cloud.google.com/run/docs/deploy-worker-pools.md.txt#worker-pool)
if you haven't done so already. You will reference the name of the
Cloud Run worker pool when executing your workflow later on.

## Create, deploy, and execute the workflow

The following workflow updates the instance count of your
Cloud Run worker pool.

1. In your home directory, create a new YAML file for your workflow, such as
   `update-cr-worker-pool.yaml`, and copy and paste the following content into
   your file:

   ```yaml
   main:
     params: [args]
     steps:
       - init:
           assign:
             - project: ${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")}
             - region: ${args.region}
             - workerPoolName: ${args.workerPool}
             - instanceCount: ${args.instanceCount}
             - workerPoolUrl: ${"https://run.googleapis.com/v2/projects/" + project + "/locations/" + region + "/workerPools/" + workerPoolName}
       - updateWorkerPool:
           call: http.patch
           args:
             url: ${workerPoolUrl}
             query:
               updateMask: "scaling.manualInstanceCount"
             auth:
               type: OAuth2
             body:
               scaling:
                 manualInstanceCount: ${int(instanceCount)}
           result: updateResult
       - returnOutput:
           return: ${updateResult.body}
   ```
2. Deploy the workflow and associate it with a specified service account:

   ```yaml
   gcloud workflows deploy WORKFLOW_NAME \
       --source=WORKFLOW_NAME.yaml \
       --location=REGION \
       --service-account=SERVICE_ACCOUNT
   ```

   Replace the following:
   - <var translate="no">WORKFLOW_NAME</var>: the name of your workflow, such as `update-worker-pool`. The workflow name should match the name of the YAML file you created earlier.
   - <var translate="no">REGION</var>: the region your Cloud Run worker pool is deployed in, for example `europe-west1`.
   - <var translate="no">SERVICE_ACCOUNT</var>: the service account email that has been granted the [required roles](https://docs.cloud.google.com/run/docs/configuring/workerpools/workflows-autoscaling.md.txt#required-roles), for example:
     - `SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com`
     - `PROJECT_NUMBER-compute@developer.gserviceaccount.com`
3. Execute the workflow with the details of your worker pool and the instance
   count:

   ```yaml
   gcloud workflows execute WORKFLOW_NAME \
       --location=REGION \
       --data='{"region":"REGION", "workerPool":"WORKER_POOL_NAME", "instanceCount":INSTANCE_COUNT}'
   ```

   Replace the following:
   - <var translate="no">WORKFLOW_NAME</var>: the name of your workflow.
   - <var translate="no">REGION</var>: the region your Cloud Run worker pool is deployed in, for example `europe-west1`.
   - <var translate="no">WORKER_POOL_NAME</var>: the name of your worker pool.
   - <var translate="no">INSTANCE_COUNT</var>: the modified number of instances for the worker pool, for example `3`.

## Verify the updates

After you've executed the workflow, you can view the updates in the following
places in the Google Cloud console:

- To view the execution status and results, go to the Workflows
  details page and select your workflow:

  [Go to Workflows](https://console.cloud.google.com/workflows)

  Verify that the `manualInstanceCount` field has changed in the most
  recent execution of the workflow.
- To verify the update was successful in Cloud Run, go to the
  worker pools page:

  [Go to Cloud Run](https://console.cloud.google.com/run/worker-pools)

  Verify that the manual scaling count for your worker pool has changed.