Autoscaling with python

0

Due to security requirements autoscaling scale out and scale in is not allowed. the requirement is that the instances that are added to the target group and then removed cannot be terminated. The plan is to create EC2 instances and stop the instances. when an instance needs to be added to the target group due to metric being exceeded (CPU over 60%) then the existing instances is started and then added to the target group. then when the metric falls below the CPU metric then the instance is removed from the target group and then stopped.

I have created python script that will start the stopped instance and added the instance to the target group when the metric is exceeded. I have a python script that will remove the instances, decrement the desired capacity, and shutdown the instance when the metric falls below the set metric.

where i am lost is figuring out how to trigger these python scripts to scale out and scale in. I am looking through cloudwatch and don't see anything to help me. the autoscaling group does not have seem to have the setting to just point to a lambda function when scaling out and scaling in.

is this something that just is not doable?

asked 2 years ago664 views
2 Answers
1

If the only requirement is to retain the instances on scale in. You can leverage instance termination protection in auto scaling. This would allow you to take adavantage of all the features of auto scaling, and still retain the instances. https://docs.aws.amazon.com/autoscaling/ec2/userguide/as-instance-termination.html

If you absolutely cannot use auto scaling, you can leverage EventBridge to invoke a Lambda to run your python script that will interact with EC2. https://aws.amazon.com/premiumsupport/knowledge-center/start-stop-lambda-eventbridge/

profile pictureAWS
EXPERT
Rob_H
answered 2 years ago
0

In CloudWatch, create an Alarm on the Auto Scaling Group metric such as CPUUtilization. For the Notification, create or select an SNS topic. Then subscribe your Lambda function to this topic.

profile pictureAWS
EXPERT
kentrad
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions