Why this matters
Non-production environments burn money when nobody is using them. A dev VM, a staging GKE cluster, and a test Cloud SQL instance can sit idle all night and all weekend. A schedule that stops them outside working hours cuts the compute part of the bill without touching production.
On Google Cloud, what you save depends on what you stop. Stopping a Compute Engine instance ends vCPU and memory charges. Resizing a GKE Standard node pool to zero ends node charges. Stopping a Cloud SQL instance ends its vCPU and memory charges. Disks, snapshots, buckets, static IPs, and committed use discounts keep billing.
A label-driven schedule is easier to maintain than a list of instance names. Label resources with env, owner, and schedule. The scheduler finds them by label and acts. Resources that lack the label stay up by default, which is safer, but you need a weekly report for unlabeled resources so they do not become permanent waste.
Before you start
Write down what must stay on. Shared services such as CI runners, artifact registries, identity services, and monitoring agents often serve production too. Put them in a keep=always label or a separate project. Decide whether staging databases can stop. Some teams keep one small database up for demos and stop the rest.
Create a dedicated service account for the scheduler and a custom role. Grant only the permissions you need: compute.instances.stop and start, container.clusters.update for GKE node pool resizing, cloudsql.instances.update for Cloud SQL stop and start, and logging.logEntries.create. Avoid Owner and Editor. If you use folders, grant at the folder level so new projects inherit the role.
Map dependencies before you write code. A typical stop order is Cloud Run minimum instances, GKE workloads and node pools, Cloud SQL, then Compute Engine. Start is the reverse, with waits. A database that is still booting will fail the app's health check, so the start routine needs a retry loop, not a fixed sleep.
Check quotas for CPU, IP addresses, and persistent disk. Starting many VMs at 07:00 can hit a regional quota and leave half the environment down. Stagger starts by environment or by group. Test on a scratch project first.
How to do it
Use Cloud Scheduler to call a small Cloud Run service or Cloud Function over HTTP. Authenticate the call with an OIDC token tied to a service account. The service reads labels, calls the Google Cloud APIs, and writes one structured log line per resource. Keep stop and start in one codebase so the resource list cannot drift.
Make every action idempotent. If an instance is already stopped, log it and continue. If a Cloud SQL instance is in maintenance, skip it and report. Run with dry_run=true first, then run for real. A force flag is useful for a stuck environment, but require a reason in the request body.
Use separate Scheduler jobs for stop and start, and separate jobs for dev and staging. A cron expression like 0 20 * * 1-5 stops at 20:00 on weekdays. Set the timezone explicitly, because the Scheduler default is UTC. Add retry settings, but cap attempts so a broken job does not run all night.
Put a lock in front of each environment. A Firestore document with an expiry timestamp, or a Cloud Tasks queue with a deduplication ID, stops a start job from running while a stop job is still working. The lock also gives you an audit trail when someone triggers a manual run.
- Label resources by environment. Add labels such as env=dev, env=staging, schedule=weekday, and keep=always to Compute Engine instances, GKE node pools, Cloud SQL instances, and Cloud Run services. Use a consistent key set across projects. For unlabeled resource types, keep a small allowlist.
- Create a least-privilege service account. Make a service account for scheduling and bind a custom role with only stop, start, resize, and update permissions. Let Cloud Scheduler impersonate it with an OIDC token. Do not reuse the default compute service account.
- Write the stop routine. List targets by label, then stop in dependency order: Cloud Run minimum instances, GKE node pools, Cloud SQL, Compute Engine. Treat already stopped resources as success and log the state. Return a nonzero code only when a target fails and cannot be skipped.
- Write the start routine. Start in reverse order and wait for each layer to be ready. For Cloud SQL, poll the instance state until runnable. For GKE, wait for the node pool to report the expected node count. For VMs, wait for the instance to reach RUNNING before starting the next group.
- Add dry run and safety checks. Accept a dry_run flag that lists every resource the job would touch and changes nothing. Refuse to run if the target project has a production label or if the list is larger than a configured cap. Log the request ID and the caller identity.
- Create Cloud Scheduler jobs. Create one job for stop and one for start per environment. Use an HTTP target, the service URL, an OIDC token, and an explicit timezone. Set a short deadline and a retry limit of two or three attempts.
- Schedule exceptions. Add a second pair of jobs for weekends if some teams work Saturdays, or pause the Scheduler job during a release freeze. Use a calendar file or a small config map for holidays rather than editing cron expressions by hand.
How to check it worked
Open Cloud Scheduler and confirm the job status is Enabled and the last attempt succeeded. In Logging, filter on the scheduler job resource and on your service. Look for one line per resource with the action, the resource name, and the resulting state. A summary line should show counts for stopped, skipped, and failed.
Check the real state, not just the logs. Run gcloud compute instances list with a label filter and confirm the status is TERMINATED. Check the GKE node pool node count and the Cloud SQL activation policy. For Cloud Run, confirm the minimum instance setting is zero.
Compare billing for a full week. Use project filters if each environment has its own project; labels do not cover every charge. Look at Compute Engine, GKE, and Cloud SQL SKUs during off hours. Some charges will remain, and that is expected.
Break it on purpose once. Remove one permission or stop a dependency, then run the job and confirm the alert fires. A schedule you have never seen fail is a schedule you do not understand.
Mistakes to avoid
Do not stop a database in the middle of a migration, a backup, or a batch job. Put a check in the stop routine for long-running operations, or give the environment a quiet window. Cloud SQL stops connections abruptly, so apps need retry logic and a clear error page.
Do not assume a stopped resource costs nothing. Persistent disks, snapshots, static IPs, load balancers, NAT gateways, and log buckets keep billing. Reserved external IPs attached to stopped VMs still cost. Review those separately and release what you do not need.
Do not let jobs overlap. A stop that takes longer than expected can collide with a start, leaving half the environment down. Use a lock with a timeout, and make the start routine verify that the stop is finished. Avoid one giant job for every project; a failure in one project should not block the rest.
Do not hide exceptions in chat. If a developer needs a VM at night, give them a label and a documented request path. If the bill grows faster than usage and you want a ranked view, the Cloud cost audit from Cloudverg is $4,500/month, fixed scope over two weeks, and one option among others.
Frequently asked questions
Can Cloud Scheduler stop Compute Engine instances directly?
No. Cloud Scheduler sends an HTTP request, so you need a small service or function that calls the Compute Engine API. The service account used by the function needs compute.instances.stop and start on the target project.
Does stopping a Cloud SQL instance stop all charges?
No. Stopping ends vCPU and memory charges, but storage and backups continue to bill. The instance also cannot serve connections while stopped. Check backup schedules and application retry behavior before you add it to a nightly stop job.
How do I keep one environment up during a release?
Pause the Cloud Scheduler job for that environment, or add a keep=always label to the resources. Pausing is cleaner because it leaves the schedule intact. Set a reminder to resume it, or the environment will stay up until someone notices.