Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions config/plugin/spot_termination.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,26 @@ OK=0
NONOK=1
UNKNOWN=2

status_code=`curl --max-time 3 --silent --output /dev/stderr --write-out "%{http_code}" "http://169.254.169.254/latest/meta-data/spot/instance-action"`
# Try to get IMDSv2 token (for AL2023)
TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" \
-H "X-aws-ec2-metadata-token-ttl-seconds: 21600" \
--max-time 3 --silent --fail 2>/dev/null)

if [ "${status_code}" -eq "404" ]
then
# Check spot termination notice
if [ -n "$TOKEN" ]; then
# Use IMDSv2 if token was obtained (AL2023)
status_code=$(curl --max-time 3 --silent --output /dev/stderr --write-out "%{http_code}" \
-H "X-aws-ec2-metadata-token: $TOKEN" \
"http://169.254.169.254/latest/meta-data/spot/instance-action")
else
# Fall back to IMDSv1 (Linux 2)
status_code=$(curl --max-time 3 --silent --output /dev/stderr --write-out "%{http_code}" \
"http://169.254.169.254/latest/meta-data/spot/instance-action")
fi

if [ "${status_code}" -eq "404" ]; then
exit $OK
elif [ "${status_code}" -eq "200" ]
then
elif [ "${status_code}" -eq "200" ]; then
exit $NONOK
else
exit $UNKNOWN
Expand Down