Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Handling AWS Spot Instance Termination Notices
#!/bin/bash
# AWS Instance Termination Notice Handler
# Based on: https://blog.fugue.co/2015-01-06-spot-termination-notices.html
while true; do
# get meta-data HTTP headers
HEADER=$(curl -Is http://169.254.169.254/latest/meta-data/spot/termination-time)
# HTTP 404 - not marked for termination
if [ -z $(echo $HEADER | head -1 | grep 404 | cut -d \ -f 2) ]; then
echo "Running shutdown hook."
# Call your shutdown script here.
break
else
# Spot instance not yet marked for termination.
echo "Spot instance not yet marked for termination."
sleep 5
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment