When you create virtual machines on Cloud providers, the price depends on the size on the machine and the region where you create it.
You have the possibility to reserve capacity for a long period of time (1 year for instance), to have reductions from the provider.
Another way to have reductions is to get Spot instances : it offers reduced price machines based on spare resources not used by other machines.
The advantage is that you have very interesting price for you virtual machines, the downside is that you should consider this resources can break at any moment.
For developement and testing environments, that’s a very good option.
You have to handle the automated rebuild of the instances to work properly. That’s why we will build a Managed Instance Group on GCP and an Autoscaling Group on AWS.
We are going to see how to implement that on both providers.
On Google Cloud
First, you need to create an Instance Template, in the Compute Engine section :
Give a name to this Instance Template, and select a region (we choose one where there are free credits) :
We choose e2-micro instance (General purpose with 1 Gb RAM), we can see that the cost is very low for one month, including storage : $3.44 :
You can set some firewall rules :
And assign to your prefered VPC, then create :
Now you will create a MIG (Managed Instance Group), to create the VMs according to this template :
Create a stateless Instance Group, give a name and select the template :
Set a location with multiple zones, set an Autoscaling to have permanently 2 machines running, then create the MIG :
Now you can access your VMs, by clicking “Connect SSH” :
On AWS
First, you need to create a Launch Template, in the EC2 section :
Give a name to this Launch Template :
Select an AMI from the catalog, Amazon Linux will allow to easily connect with session manager, and Arm will use less resources and cheaper :
Select t4g.nano, that is an optimized general purpose instance, with a base price of 0.0046 USD per hour :
Select the VPC and Security group you want to use (for the SG, just allow All traffic in Outbound, for now) :
Specify a tag “Name” for your instances to recognize them in the list later :
In Advanced details, specify an IAM role for SSM acces, and that you want Spot instances, then create the template :
Now, create an Auto Scaling Group (ASG) :
Name it and select the template just created :
Select the network options you prefer :
Select the scaling policies you want, then you can create the ASG :
Now you can access your VMs, using Session Manager :
Conclusion
You can see that this mechanism is really similar on both of these cloud providers and Spot instances are really easy to implement.
Of course, the best option will be to automate it an IaC tool like Terraform or OpenTofu and to optimize it according to your needs.
You now have the keys to implement them on your projects, to have low-cost computing resources for any development objective.
Top comments (0)