How to launch spot instance using AWS CLI

How to launch spot instance using AWS CLI

May 13, 2020 / Eternal Team

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 AWS CLI please visit the given link.

Prerequisites

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 Spot Instance pricing history.

To launch spot instances with defined duration thorough AWS CLI

First you have to specify duration for your spot instances, in that can be including the

--block-duration-minutes

Also, please visit the below link:
https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-spot-instance-requests.html

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.

aws ec2 request-spot-instances \
  --instance-count 5 \
  --block-duration-minutes 120 \
  --type "one-time" \
  --launch-specification file://specification.json

But if you want to check the cost for spot instances with specific defined duration, so that you have to use the describe-spot-instance-requests 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 describe-spot-instance-requests command with the –query option.

  aws ec2 describe-spot-instance-requests \
    --query "SpotInstanceRequests[*].{ID:InstanceId}"

After using above command you will see something out put like this ,

[
  {
      "ID": "i-1234567890abcdef0"
  },
  {
      "ID": "i-0598c7d356eba48d7"
  }
]

Using the describe-instances command with the –filters option.

aws ec2 describe-instances \
  --filters "Name=instance-lifecycle,Values=spot"

Now tagging is very important so you can easily find the instance.

Next, we will understand how to tagging spot instance.

aws ec2 create-tags \
  --resources sir-08b93456 i-1234567890abcdef0 \
  --tags Key=purpose,Value=test

Canceling the spot instance

You can only cancel spot instance requests that are open, active, or disabled.

Use the cancel-spot-instance-requests command to cancel the specified Spot request.

aws ec2 cancel-spot-instance-requests –spot-instance-request-ids sir-08b93456

And to start and stop instance use below commands.

For start instance

aws ec2 start-instances –instance-ids i-1234567890abcdef0

And for stop instance

aws ec2 stop-instances –instance-ids i-1234567890abcdef0

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 Canceling a Spot Instance request.

For the termination of instance.

aws ec2 terminate-instances –instance-ids i-1234567890abcdef0 i-0598c7d356eba48d7

Note :

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 🙂

AWS-Consulting-Partner

Talk to AWS Certified Consultant

    Want to start a project?

    It’s simple.

    Contact us