Last active
February 7, 2025 08:49
-
-
Save cipulan/409ee59b19ed7746567e417922ede8d5 to your computer and use it in GitHub Desktop.
AWS Spot Instance Termination Monitor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description=Monitor AWS Spot Termination Event | |
After=network.target | |
[Service] | |
ExecStart=/opt/monitor_termination.sh | |
Restart=always | |
User=root | |
[Install] | |
WantedBy=multi-user.target |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
LOG_FILE="/var/log/spot_termination_monitor.log" | |
AWS_REGION="ap-southeast-3" | |
# Get the instance ID from metadata | |
INSTANCE_ID=$(/usr/bin/curl -s http://169.254.169.254/latest/meta-data/instance-id) | |
log() { | |
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE" | |
} | |
# Function to check termination event | |
check_termination_event() { | |
STATUS_CODE=$(/usr/bin/curl -s -o /dev/null -w "%{http_code}" http://169.254.169.254/latest/meta-data/spot/termination-time) | |
echo "$STATUS_CODE" | |
} | |
# Function to set the instance as unhealthy | |
set_instance_unhealthy() { | |
log "Setting instance $INSTANCE_ID to Unhealthy in region $AWS_REGION..." | |
/usr/bin/aws autoscaling set-instance-health --instance-id "$INSTANCE_ID" --health-status Unhealthy --region "$AWS_REGION" | |
} | |
log "Monitoring for Spot Instance termination events..." | |
while true; do | |
STATUS_CODE=$(check_termination_event) | |
if [ "$STATUS_CODE" -eq 200 ]; then | |
log "Termination event detected! Marking instance as Unhealthy..." | |
set_instance_unhealthy | |
exit 0 | |
else | |
log "No termination event detected. Checking again in 30 seconds..." | |
fi | |
sleep 30 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
AWS Spot Instance Termination Monitor
📌 Description
This script monitors AWS Spot Instance termination events and automatically sets the instance’s health status to Unhealthy in an Auto Scaling Group. This ensures the instance is removed from the load balancer before AWS reclaims it.
🔹 Features:
✅ Checks for termination events every 30 seconds
✅ Logs all actions to /var/log/spot_termination_monitor.log
✅ Sets the instance Unhealthy in ap-southeast-3 region
✅ Runs as a systemd service for automatic monitoring
🚀 How to Use
1️⃣ Download & Set Up the Script
Save the script to /opt/monitor_termination.sh:
2️⃣ (Optional) Run the Script Manually
To test the script manually:
3️⃣ Run as a systemd Service (Recommended)
sudo systemctl daemon-reload sudo systemctl enable monitor-termination sudo systemctl start monitor-termination
4️⃣ Check Logs & Status
• View Service Status:
• Check Logs:
5️⃣ (Optional) Stop or Restart
• To stop the service:
• To restart after modifying the script:
🛠️ Troubleshooting
• If AWS CLI is not installed, install it:
sudo apt update && sudo apt install awscli -y
• Ensure AWS credentials are configured for the instance:
• Check if the instance has permissions to update Auto Scaling health:
Attach the following policy to the IAM role:
This script helps ensure smooth scaling and prevents unexpected 502 errors due to AWS spot termination. 🚀 Let me know if you need any modifications!