Skip to content

Commit bfe285d

Browse files
committed
fix spot termination for IMDSv2
1 parent 665b3ea commit bfe285d

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

config/plugin/spot_termination.sh

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,26 @@ OK=0
44
NONOK=1
55
UNKNOWN=2
66

7-
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"`
7+
# Try to get IMDSv2 token (for AL2023)
8+
TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" \
9+
-H "X-aws-ec2-metadata-token-ttl-seconds: 21600" \
10+
--max-time 3 --silent --fail 2>/dev/null)
811

9-
if [ "${status_code}" -eq "404" ]
10-
then
12+
# Check spot termination notice
13+
if [ -n "$TOKEN" ]; then
14+
# Use IMDSv2 if token was obtained (AL2023)
15+
status_code=$(curl --max-time 3 --silent --output /dev/stderr --write-out "%{http_code}" \
16+
-H "X-aws-ec2-metadata-token: $TOKEN" \
17+
"http://169.254.169.254/latest/meta-data/spot/instance-action")
18+
else
19+
# Fall back to IMDSv1 (Linux 2)
20+
status_code=$(curl --max-time 3 --silent --output /dev/stderr --write-out "%{http_code}" \
21+
"http://169.254.169.254/latest/meta-data/spot/instance-action")
22+
fi
23+
24+
if [ "${status_code}" -eq "404" ]; then
1125
exit $OK
12-
elif [ "${status_code}" -eq "200" ]
13-
then
26+
elif [ "${status_code}" -eq "200" ]; then
1427
exit $NONOK
1528
else
1629
exit $UNKNOWN

0 commit comments

Comments
 (0)