freeCodeCamp / infra

> freeCodeCamp.org's infrastructure-as-code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Enhancement for marking instances unhealthy in ASG

raisedadead opened this issue · comments

fetch_instance_id() {
  local retries=5
  for ((i = 0; i < retries; i++)); do
    INSTANCE_ID=$(curl -sS --fail "http://169.254.169.254/latest/meta-data/instance-id" || echo "")
    if [[ -n "$INSTANCE_ID" ]]; then
      return 0
    fi
    echo "Retry $((i + 1))/$retries: Failed to fetch instance ID. Retrying..."
    sleep 5
  done
  echo "Failed to fetch instance ID after $retries retries."
  return 1
}


mark_instance_unhealthy() {
  fetch_instance_id || return 1  # Ensures we only proceed if instance ID is obtained
  local region=$(curl -sS --fail "http://169.254.169.254/latest/meta-data/placement/region" || echo "us-east-1")
  aws autoscaling set-instance-health --instance-id "$INSTANCE_ID" --health-status Unhealthy --region "$region"
}