A lightweight utility to schedule time-based auto-scaling for Google Cloud Run minimum instances.
Cloud Run Cronos provides a simple and effective way to manage the cost of your Google Cloud Run services by scheduling the min-instances parameter. This allows you to scale down your services during periods of low traffic (e.g., at night or on weekends) and scale them up during peak hours, optimizing resource utilization and reducing costs.
The solution uses a combination of Cloud Scheduler and Cloud Run Jobs to achieve this. For each schedule you define, a Cloud Scheduler job is created to trigger a Cloud Run Job at the specified time. The Cloud Run Job then executes a gcloud command to update the min-instances of your Cloud Run service.
This repository provides two versions of the script:
cloud-run-cronos.shfor Unix-like systems (Linux, macOS)cloud-run-cronos.ps1for Windows systems (PowerShell)
Both scripts are configured by setting variables at the beginning of the file.
| Variable | Description | Example |
|---|---|---|
ProjectId |
Your Google Cloud project ID. | my-gcp-project |
ServiceName |
The name of your Cloud Run service. | my-app-service |
Region |
The region where your Cloud Run service is deployed. | us-central1 |
AppPrefix |
A prefix for the resources created by the script (Service Account, Cloud Scheduler jobs, Cloud Run jobs). | my-app |
Timezone |
The timezone for the Cloud Scheduler jobs. Can be UTC or any IANA timezone name. |
Europe/Rome |
Schedules |
An array of schedules for auto-scaling. See the format below. | `("0 7 * * * |
The Schedules variable is an array of strings, where each string represents a scaling rule. The format for each rule is:
"cron-expression|min-instances"
cron-expression: A standard 5-field cron expression (minute hour day-of-month month day-of-week).min-instances: The number of minimum instances to set for your Cloud Run service.
Example: The following configuration will scale up the service to 3 instances at 9 AM and scale it down to 1 instance at 8 PM, every day.
Schedules=(
"0 9 * * *|3"
"0 20 * * *|1"
)
- Open
cloud-run-cronos.ps1and set the variables in the "SET YOUR VARIABLES HERE" section. - Authenticate with gcloud:
gcloud auth application-default login gcloud auth login - Run the script:
./cloud-run-cronos.ps1
- Open
cloud-run-cronos.shand set the variables in the "SET YOUR VARIABLES HERE" section. - Make the script executable:
chmod +x cloud-run-cronos.sh
- Authenticate with gcloud:
gcloud auth application-default login gcloud auth login
- Run the script:
./cloud-run-cronos.sh
This project includes a suite of tests for both the PowerShell and Bash scripts.
The PowerShell tests are written using Pester.
- Install Pester:
Install-Module -Name Pester -Force -SkipPublisherCheck
- Run the tests:
Invoke-Pester -Path tests/cloud-run-cronos.test.ps1
The Bash tests are written using Bats.
- Install Bats:
# Using Homebrew on macOS brew install bats-core # Using npm npm install -g bats
- Run the tests:
bats tests/cloud-run-cronos.test.bats
