DEV Community

Cover image for Create spot instances on GCP & AWS
Jérôme Dx
Jérôme Dx

Posted on

Create spot instances on GCP & AWS

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 :

gcp step 1

Give a name to this Instance Template, and select a region (we choose one where there are free credits) :

gcp step 2

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 :

gcp step 3

You can set some firewall rules :

gcp step 4

And assign to your prefered VPC, then create :

gcp step 5

Now you will create a MIG (Managed Instance Group), to create the VMs according to this template :

gcp step 6

Create a stateless Instance Group, give a name and select the template :

gcp step 7

Set a location with multiple zones, set an Autoscaling to have permanently 2 machines running, then create the MIG :

gcp step 8

Now you can access your VMs, by clicking “Connect SSH” :

gcp step 9

On AWS

First, you need to create a Launch Template, in the EC2 section :

aws step 1

Give a name to this Launch Template :

aws step 2

Select an AMI from the catalog, Amazon Linux will allow to easily connect with session manager, and Arm will use less resources and cheaper :

aws step 3

Select t4g.nano, that is an optimized general purpose instance, with a base price of 0.0046 USD per hour :

aws step 4

Select the VPC and Security group you want to use (for the SG, just allow All traffic in Outbound, for now) :

aws step 5

Specify a tag “Name” for your instances to recognize them in the list later :

aws step 6

In Advanced details, specify an IAM role for SSM acces, and that you want Spot instances, then create the template :

aws step 7.1

aws step 7.2

aws step 7.3

Now, create an Auto Scaling Group (ASG) :

aws step 8

Name it and select the template just created :

aws step 9

Select the network options you prefer :

aws step 10

Select the scaling policies you want, then you can create the ASG :

aws step 11

Now you can access your VMs, using Session Manager :

aws step 12

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)