Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cloud-run-cronos a lightweight utility to schedule time-based auto-scaling for Google Cloud Run minimum instances

Cloud Run Cronos

A lightweight utility to schedule time-based auto-scaling for Google Cloud Run minimum instances.


📜 Overview

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.sh for Unix-like systems (Linux, macOS)
  • cloud-run-cronos.ps1 for Windows systems (PowerShell)

⚙️ General Configuration

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 * * *

Schedule Format

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"
)

🚀 Execution

Windows (PowerShell)

  1. Open cloud-run-cronos.ps1 and set the variables in the "SET YOUR VARIABLES HERE" section.
  2. Authenticate with gcloud:
    gcloud auth application-default login
    gcloud auth login
  3. Run the script:
    ./cloud-run-cronos.ps1

Unix-like (Bash)

  1. Open cloud-run-cronos.sh and set the variables in the "SET YOUR VARIABLES HERE" section.
  2. Make the script executable:
    chmod +x cloud-run-cronos.sh
  3. Authenticate with gcloud:
    gcloud auth application-default login
    gcloud auth login
  4. Run the script:
    ./cloud-run-cronos.sh

✅ Testing

This project includes a suite of tests for both the PowerShell and Bash scripts.

Windows (Pester)

The PowerShell tests are written using Pester.

  1. Install Pester:
    Install-Module -Name Pester -Force -SkipPublisherCheck
  2. Run the tests:
    Invoke-Pester -Path tests/cloud-run-cronos.test.ps1

Unix-like (Bats)

The Bash tests are written using Bats.

  1. Install Bats:
    # Using Homebrew on macOS
    brew install bats-core
    
    # Using npm
    npm install -g bats
  2. Run the tests:
    bats tests/cloud-run-cronos.test.bats