How to Optimize GCP VM Cost with Automated Start/Stop script.
Introduction:
In today’s cloud-centric world, cost optimization is challenging task. Google Cloud Platform (GCP) offers powerful tools to help you minimize expenses while maximizing efficiency. In this guide, I’m gonna show you one of many ways on how to implement a cost-saving solution: automated process to stop and start VMs deployed on GCP. By following this, you can potentially slash your GCP VM costs by half.
Note: If your main goal is start/stop, the Instance schedules is ideal. But for added flexibility like extra pre/post actions, Cloud Functions are the go-to choice. It’s all about picking the right tool for the job!
Aim:
To implement an automated cost optimization solution in GCP that significantly reduces cost by efficiently managing VM instances through a Cloud Function and Cloud Scheduler, ensuring they are active only during essential working hours.
Steps
- Create a Cloud Function.
- Schedule a job using Cloud Scheduler.
- Test and Validate.
Step 1: Create a Cloud Function
The first step is to create a Cloud Function that will handle the automation of starting and stopping your VM. Here’s how:
Navigate to Cloud Functions and create a function with desired computing configurations.
- For Trigger, Choose “HTTP trigger” (we will configure the trigger later).
- Choose one from available runtimes, I’m using “Python 3.7.”
- Use below function calls to start/stop VM using compute_v1 client. For entire code do check out my GitHub repo — GCP-VM-AutoStartStop
def stop_instance(project_id: str, zone: str, instance_name: str) -> None:
instance_client = compute_v1.InstancesClient()
operation = instance_client.stop(
project=project_id, zone=zone, instance=instance_name
)
wait_for_extended_operation(operation, "instance stopping")
return
def start_instance(project_id: str, zone: str, instance_name: str) -> None:
instance_client = compute_v1.InstancesClient()
operation = instance_client.start(
project=project_id, zone=zone, instance=instance_name
)
wait_for_extended_operation(operation, "instance starting")
return
Step 2: Schedule a job with Cloud Scheduler
Now that your Cloud Function is ready, it’s time to set up a schedule using Cloud Scheduler to trigger your function.
Navigation to Cloud Scheduler and schedule two Jobs to run everyday.
Jobs Configuration:
- For Frequency, Set it to your desired time. For example,
everyday before 8am - 55 7 * * *
everyday after 7pm - 05 19 * * *
- For those two jobs target, choose “HTTP” and provide the HTTP endpoint of your Cloud Function.
- HTTP method: Select “GET.”
- Time zone: Specify your desired time zone and create job.
Step 3: Test and Validate
Your cost-saving automation is now set up, but it’s essential to test and validate it to ensure it’s working as expected.
Run a few manual tests to verify that the Cloud Function correctly starts and stops your VM.
Conclusion
By following these steps, you’ve successfully implemented a cost-effective solution to optimize your GCP VM cost. This automated approach ensures that your VMs are only running when needed, reducing costs without sacrificing productivity.
“Automating VM start-stop is not just about cost savings. it’s also about operational efficiency. It allows your team to focus on development.”
This automation reduced my VM cost by 52% monthly. Start saving on your GCP bills today with such automated scripts.
Happy cost-saving!