Skip to content

Commit 04c14d2

Browse files
gamadoclaude
andcommitted
Cancel persistent spot request on instance destroy
Persistent spot requests survive instance termination and could launch a new instance. Cancel the spot request explicitly before deleting the CloudFormation stack to prevent orphaned resources. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 4b1a39e commit 04c14d2

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

deploy/aws-hypervisor/scripts/destroy.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@ if [[ -f "${reservation_file}" ]]; then
6161
rm -f "${instance_data_dir}/availability-zone"
6262
fi
6363

64+
# Cancel persistent spot request if the instance is a spot instance
65+
instance_id_file="${instance_data_dir}/aws-instance-id"
66+
if [[ -f "${instance_id_file}" ]]; then
67+
instance_id=$(cat "${instance_id_file}")
68+
spot_request_id=$(aws --region "${REGION}" ec2 describe-instances \
69+
--instance-ids "${instance_id}" \
70+
--query 'Reservations[0].Instances[0].SpotInstanceRequestId' \
71+
--output text --no-cli-pager 2>/dev/null || echo "")
72+
73+
if [[ -n "${spot_request_id}" && "${spot_request_id}" != "None" && "${spot_request_id}" != "null" ]]; then
74+
msg_info "Canceling persistent spot request ${spot_request_id}..."
75+
aws --region "${REGION}" ec2 cancel-spot-instance-requests \
76+
--spot-instance-request-ids "${spot_request_id}" \
77+
--no-cli-pager >/dev/null 2>&1 || msg_warning "Failed to cancel spot request (may already be canceled)"
78+
fi
79+
fi
80+
6481
# Delete compute stack first (CF prevents deleting network while its exports are imported)
6582
if aws --region "$REGION" cloudformation describe-stacks --stack-name "${STACK_NAME}" &>/dev/null; then
6683
echo "Deleting compute stack '${STACK_NAME}'..."

0 commit comments

Comments
 (0)