From b4bd40c92cc40835ead5fb2fe7e7c32fec9ea9bc Mon Sep 17 00:00:00 2001 From: yashdeep Date: Tue, 12 May 2026 08:52:21 +0530 Subject: [PATCH] fix(packer/linux): power off bootstrap failure when IMDS has no instance id --- .../ansible/roles/agent/files/start-agent.sh | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/packer/linux/ansible/roles/agent/files/start-agent.sh b/packer/linux/ansible/roles/agent/files/start-agent.sh index 6dd7110..2a3c1ee 100755 --- a/packer/linux/ansible/roles/agent/files/start-agent.sh +++ b/packer/linux/ansible/roles/agent/files/start-agent.sh @@ -3,17 +3,23 @@ set -eo pipefail # -# If anything goes wrong with the instance startup, -# we make the instance unhealthy, so the auto scaling group can rotate it. +# If anything goes wrong with the instance startup, mark unhealthy when IMDS returns an +# instance id; otherwise shut down (AWS calls need IMDS; LT uses terminate on shutdown). # on_failure() { local exit_code=$1 if [[ $exit_code != 0 ]] ; then + set +e local __token__=$(fetch_idms_token) local __instance_id__=$(curl -H "X-aws-ec2-metadata-token: $__token__" --fail --silent --show-error --location "http://169.254.169.254/latest/meta-data/instance-id") - aws autoscaling set-instance-health \ - --instance-id "${__instance_id__}" \ - --health-status Unhealthy + if [[ -n "$__instance_id__" ]]; then + aws autoscaling set-instance-health \ + --instance-id "${__instance_id__}" \ + --health-status Unhealthy + else + sudo shutdown -h now || sudo poweroff -f || true + fi + set -e fi }