Skip to content

Instantly share code, notes, and snippets.

@urjitbhatia
Last active December 23, 2021 18:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save urjitbhatia/add0add7ff2b1f4bcb799b7cb8ba24af to your computer and use it in GitHub Desktop.
Save urjitbhatia/add0add7ff2b1f4bcb799b7cb8ba24af to your computer and use it in GitHub Desktop.
Drain a nomad EC2 spot instance node on termination
#!/bin/bash
CMD="curl --write-out %{http_code} --silent --output /dev/null http://169.254.169.254/latest/meta-data/spot/termination-time"
while true
do
if [ "$(${CMD})" != "404" ]; then
# 2 minute warning received. Do all your cleanup work.
echo "2 minute termination warning. Draining nomad node..."
nomad node-drain -yes -self -enable
echo "Hasta la vista baby, I will be back"
break
else
echo "Not terminating yet..."
# Still running fine, sleep and then check again
sleep 10
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment