-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added EC2 termination notice script handler
- Loading branch information
Showing
3 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/bin/bash | ||
|
||
# Spot Termination notice handler. | ||
# See https://aws.amazon.com/blogs/compute/best-practices-for-handling-ec2-spot-instance-interruptions/ | ||
|
||
# If you are using spot instances, you might want to handle termination notices. | ||
# This script will continuously poll the EC2 Metadata Endpoint every 5 seconds, | ||
# and if it detects a termination notice, it can run your arbitrary code, like safely | ||
# shutting down processes, for example. | ||
|
||
# To active this script, you shall uncomment the .platform/files/supervisor.ini process | ||
# that will keep this script alive throughout the instance lifecycle until its termination. | ||
|
||
TOKEN=`curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` | ||
|
||
while sleep 5; do | ||
|
||
HTTP_CODE=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -s -w %{http_code} -o /dev/null http://169.254.169.254/latest/meta-data/spot/instance-action) | ||
|
||
if [[ "$HTTP_CODE" -eq 401 ]] ; then | ||
|
||
# Refreshing the authentication token... | ||
TOKEN=`curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 30"` | ||
|
||
elif [[ "$HTTP_CODE" -eq 200 ]] ; then | ||
|
||
# Here, the instance knows it will be interrupted. | ||
# Run your code. | ||
|
||
else | ||
|
||
# Here, the instance is not interrupted. | ||
|
||
fi | ||
|
||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters