DEV Community

Cover image for This AWS CLI command can run instance refresh for auto scaling groups
Ashiqur Rahman
Ashiqur Rahman

Posted on

This AWS CLI command can run instance refresh for auto scaling groups

Recently, I was working on upgrading Kubernetes versions for all our EKS clusters. The upgrade process is simple, We upgrade the K8s version and instance AMIs from Terraform. Then, we upgrade chart/image version of the different tools we deploy e.g: CoreDNS, ExternalDNS, kubernetes-dashboard, kube-proxy etc.
All these steps can be automated ofcourse since we use Terraform. Especially when you have to upgrade a large number of clusters automation becomes absolutely essential. One step that always bothered me is that, you need to run an instance refresh for the autoscaling groups that your K8s worker nodes belong to. Otherwise, your EKS cluster will still be using the older worker nodes (using the older K8s version). Previously, I always did this using the AWS console manually. So, this was always a unwanted manual step in this workflow.
Recently, I discovered this AWS CLI command that turns out to be exactly what i was looking for.

First you can fetch the autoscaling groups in your account

aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names | grep AutoScalingGroupName | sed 's/\"AutoScalingGroupName\": //' | sed -E 's/^\s+//' | sed -E 's/\"//' | sed -E 's/\"//' | sed -E 's/\,//' | grep worker
Enter fullscreen mode Exit fullscreen mode

Now, we can run the instance refresh for each item in the list using the following command

for line in $(aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names | grep AutoScalingGroupName | sed 's/\"AutoScalingGroupName\": //' | sed -E 's/^\s+//' | sed -E 's/\"//' | sed -E 's/\"//' | sed -E 's/\,//' | grep spot-group); do
  aws autoscaling start-instance-refresh \
    --auto-scaling-group-name $line \
    --preferences '{"MinHealthyPercentage": 99, "InstanceWarmup": 300, "SkipMatching": false}'
done
Enter fullscreen mode Exit fullscreen mode

If you found this post helpful, CLICK BELOW 👇 Buy Me A Beer

Top comments (0)