11

There's a AWS metadata service which provides some information about the EC2 instance that issues a request to http://169.254.169.254/. I'm wondering if there's a way to know from that metadata if the instance is "Spot" or "On Demand"?

5 Answers 5

10

The information is not available inside the metadata.

However, you can get the instance ID from the metadata, then call ec2-describe-instances to get instance information about your instance. Inside that instance description is the spot instance request ID. If blank, then it's not a spot instance, otherwise, it's a spot instance.

2
  • Nice! Thanks, I've been looking for this for a while and must have overlooked it when looking at the instance data.
    – SaxDaddy
    Dec 26, 2015 at 9:32
  • I see no key called 'spot instance request ID' in the json printed. I did: aws ec2 describe-instances and I am using AWS CLI 2. Nov 5, 2020 at 9:56
4

I don't believe they have this information in the metadata.

You could assign a different profile for instances you launch as spot instances and use the profile name to determine what type of instance it is. If that doesn't seem like a clean or viable solution you can always grab the instance-id from the meta data then run the command ec2-describe-spot-instance-requests --filter instance-id=<instanceid> if the command returns empty than the instance is not a spot instance, if the command returns with data then it's a spot instance.

4

I know this is super old but if anyone wants a one liner:

aws ec2 describe-spot-instance-requests \
  --filters Name=instance-id,Values="$(wget -q -O - http://169.254.169.254/latest/meta-data/instance-id)" \
  --region us-east-1 | \
  jq -r '.SpotInstanceRequests | if length > 0 then "spot" else "normal" end'

Adjust --region accordingly

https://gist.github.com/urjitbhatia/c5af8a3d53661cb3d4e896feae23fc1d

3

A little improved version of that of @urjit:

aws ec2 describe-spot-instance-requests \
  --filters Name=instance-id,Values="$(wget -q -O - http://169.254.169.254/latest/meta-data/instance-id)" \
  --region "$(wget -q -O - http://169.254.169.254/latest/meta-data/placement/availability-zone | sed 's/.$//')" | \
  jq -r '.SpotInstanceRequests | if length > 0 then "Ec2Spot" else "OnDemand" end'
2

It looks like things changed with the years, so now it works

# a spot instance
$ curl -s --fail http://169.254.169.254/latest/meta-data/instance-life-cycle
spot
# a normal instance
$ curl -s --fail http://169.254.169.254/latest/meta-data/instance-life-cycle
on-demand

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .