May 13, 2020 / Nirav Shah
Have you wondered that you can get an AWS instance for the lowest price. AWS instance prices are actually very low when compared with on-premise server. Still it doesn’t hurt any enterprise or person if they save money. In this blog we will show you how to create spot instance using AWS CLI.
So that Before we are starting first you have to install AWS CLI on your system,
For installing an <a href=”https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html” target=”_blank” class=”linkcolor”>AWS CLI</a> please visit the given link.
<strong>Prerequisites</strong>
Before you begin, you have to define your maximum price, and how many Spot instances you require, and what instance type to use. To see the spot price please visit the <a href=”https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-spot-instances-history.html” target=”_blank” class=”linkcolor”>Spot Instance pricing history</a>.
<strong>To launch spot instances with defined duration thorough AWS CLI</strong>
First you have to specify duration for your spot instances, in that can be including the
<pre>–block-duration-minutes</pre>
Also, please visit the below link:
<a href=”https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-spot-instance-requests.html” target=”_blank” class=”linkcolor”>https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-spot-instance-requests.html</a>
Now, Let’s take an example and understand the installation of spot instance.
With the following command you can create a spot instance that can run for two hours. Here we define the duration in minutes. We have set it for 120 minutes that equates to 2 hours.
<pre>
aws ec2 request-spot-instances \
–instance-count 5 \
–block-duration-minutes 120 \
–type “one-time” \
–launch-specification file://specification.json
</pre>
But if you want to check the cost for spot instances with specific defined duration, so that you have to use the <a href=”https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-spot-instance-requests.html” target=”_blank” class=”linkcolor”>describe-spot-instance-requests </a>command to retrieve the fixed cost for your spot instances with a specified duration.
Next thing will see, how can we find your running spot instance with help of AWS CLI
Again you have to check <a href=”https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-spot-instance-requests.html” target=”_blank” class=”linkcolor”>describe-spot-instance-requests </a>command with the –query option.
<pre>
aws ec2 describe-spot-instance-requests \
–query “SpotInstanceRequests[*].{ID:InstanceId}”
</pre>
After using above command you will see something out put like this ,
<pre>
[
{
“ID”: “i-1234567890abcdef0”
},
{
“ID”: “i-0598c7d356eba48d7”
}
]
</pre>
Using the describe-instances command with the –filters option.
<pre>
aws ec2 describe-instances \
–filters “Name=instance-lifecycle,Values=spot”
</pre>
Now tagging is very important so you can easily find the instance.
Next, we will understand how to tagging spot instance.
<pre>
aws ec2 create-tags \
–resources sir-08b93456 i-1234567890abcdef0 \
–tags Key=purpose,Value=test
</pre>
<strong>Canceling the spot instance</strong>
You can only cancel spot instance requests that are open, active, or disabled.
Use the <a href=”https://docs.aws.amazon.com/cli/latest/reference/ec2/cancel-spot-instance-requests.html” target=”_blank” class=”linkcolor”>cancel-spot-instance-requests</a> command to cancel the specified Spot request.
<strong>aws ec2 cancel-spot-instance-requests –spot-instance-request-ids sir-08b93456</strong>
And to start and stop instance use below commands.
For start instance
<strong>aws ec2 start-instances –instance-ids i-1234567890abcdef0</strong>
And for stop instance
<strong>aws ec2 stop-instances –instance-ids i-1234567890abcdef0</strong>
If your Spot Instance request is active and has an associated running Spot Instance, or your Spot Instance request is disabled and has an associated stopped Spot Instance, canceling the request does not terminate the instance; you must terminate the running Spot Instance manually.For more information about canceling a Spot Instance request, see <a href=”https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-requests.html#using-spot-instances-cancel” target=”_blank” class=”linkcolor”>Canceling a Spot Instance request.</a>
For the termination of instance.
<strong>aws ec2 terminate-instances –instance-ids i-1234567890abcdef0 i-0598c7d356eba48d7</strong>
<strong>Note :</strong>
This blog is made with help of official AWS documents ( Spot instance) website so, if you want to add some additional feature please visit and use it as per your requirement. In case we missed something that you were looking for, please let us know through your comments and we add it up and notify you 🙂

Nirav Shah is the Director of Eternal Web Pvt Ltd, an AWS Advanced Consulting Partner and certified Odoo Partner based in the UK. With over a decade of experience in cloud computing, digital transformation, and ERP implementation, Nirav helps enterprises adopt the right technology to solve complex business challenges. He specialises in AWS infrastructure, Odoo ERP, and web development solutions for businesses across the UK and beyond.
Have queries about your project idea or concept? Please drop in your project details to discuss with our AWS Global Cloud Infrastructure service specialists and consultants.