How to identify when a spot instance receives a termination notice

AWS EC2 instance metadata spot instances termination notice

2 min read | by Jordi Prats

When running spot instances, if AWS needs to reclaim that capacity (or if the current Spot price exceeds our bid price) we will receive a two minutes grace period to be able to save any data we need.

To check is we need to terminate our tasks on the spot instance we can use the instance's metadata

We have several endpoints to use, all of them will return and HTTP 404 response until the instance is going to be interrupted. We can use:

  • http://169.254.169.254/latest/meta-data/spot: Metadata group for spot instances
  • http://169.254.169.254/latest/meta-data/spot/instance-action: The action (hibernate, stop, or terminate) and the approximate UTC time when the action will occur.
  • http://169.254.169.254/latest/meta-data/spot/termination-time: The approximate UTC time that the operating system will receive the shutdown signal.

For as long as any of these paths return a 404 we can continue using the instance, but as soon as we get a 200 we will have approximately two minutes to prepare for it to be terminated

# curl -I http://169.254.169.254/latest/meta-data/spot/termination-time
HTTP/1.1 404 Not Found
Content-Length: 0
Date: Mon, 23 May 2022 15:48:19 GMT
Server: EC2ws
Connection: close
Content-Type: text/plain

Posted on 24/05/2022