How to refresh AWS Autoscaling Group using Boto3 after increase instances?

  amazon-ec2, aws-auto-scaling, boto3, python

I am developing an application that monitors the instances of an Autoscaling group with the goal of work with its elasticity. If the instance CPU Load is 30% or lower (and there are more than one instance) I should scale down the autoscaling group, and it is 70% or higher I should scale up.

For scaling down it is working pretty well, I am able to monitor the instance and remove the instances, but when it comes to scaling up, I can do that but it seems the autoscaling group does not refresh, and for me have the new instance appearing when I execute the describe_auto_scaling_groups of Boto3 I have to kill my code execution and start it over.

Is there a way to do that without having to start over every time?

This is my code:

self._asg = boto3.client('autoscaling', region_name=region)
self._cloud_watch = boto3.client('cloudwatch',  region_name=region)
self._ec2 = boto3.client('ec2',  region_name=region)

self._auto_scaling_info = self._asg.describe_auto_scaling_groups(AutoScalingGroupNames=[autoscalinggroup])

After that, I loop to all instances of the current autoscaling group to check its CPU Load with get_metric_statistics of cloud watch and execute a simple if to scale

if instance.getCpuUtilization() <= 30 and len(self._instances) > 2: 
      self._autoScalingClient.set_desired_capacity(AutoScalingGroupName=self._auto_scaling_group.getAutoScalingGroupName(), DesiredCapacity=(self._auto_scaling_group.getDesiredCapacity() - 1))
        
if instance.getCpuUtilization() >= 70:         
    self._autoScalingClient.set_desired_capacity(AutoScalingGroupName=self._auto_scaling_group.getAutoScalingGroupName(), DesiredCapacity=(self._auto_scaling_group.getDesiredCapacity() + 1))

So I am able to scale up and down but after I change the autoscaling desired capacity, I cannot get the new instance that was created via describe_auto_scaling_groups, but if I check on AWS console it is attached to the autoscaling group, it seems that I am missing some kind of refresh after scale it. I have also search about NextToken, but not of my boto3 calls return one.

Does anyone know how can I fix it?

Thanks,
Augusto

Source: Python Questions

LEAVE A COMMENT