From bfe285d5c6724173d7573dab4c765aa991ae98d2 Mon Sep 17 00:00:00 2001 From: Massimo Necchi Date: Tue, 10 Feb 2026 17:06:17 +0000 Subject: [PATCH] fix spot termination for IMDSv2 --- config/plugin/spot_termination.sh | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/config/plugin/spot_termination.sh b/config/plugin/spot_termination.sh index 1e86414..22bb207 100755 --- a/config/plugin/spot_termination.sh +++ b/config/plugin/spot_termination.sh @@ -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