Start and stop AWS EC2 instance

半城伤御伤魂 提交于 2021-01-29 06:53:30

问题


I have a requirement in which I need to automate the start and stop of the AWS EC2 Instances (within Autoscaling group) daily. This is mainly to prevent cost. I have built a Python script to start and stop EC2 instances but it's not working properly as EC2 instances are within an Autoscaling group.
Does anybody know any solution for this?


回答1:


An Amazon EC2 Auto Scaling group is designed to automatically scale so that extra instances can be added when there is a higher workload and instances can be removed when there is less workload.

Auto Scaling is also good for ensuring the health of an application. If an instance in an Auto Scaling group fails a health check, it will be automatically terminated. Auto Scaling will then launch a new instance to replace it.

It appears that your requirement is to reduce costs when the application is not being used. This can be done by creating a scaling policy that defines when Auto Scaling should add and remove instances, such as:

  • Add an instance when the average CPU Utilization exceeds 65% (across all instances) over a 15-minute period
  • Remove an instance when the average CPU Utilization is below 30% over a 30-minute period

(Note that it is normally good to scale-out quickly, but scale-in slowly.)

By using scaling policies like this, or even automatic Target Tracking, Auto Scaling will take care of things for you.

If, on the other hand, you wish to control the Auto Scaling group manually, you can add and remove instances using:

  • set_desired_capacity() to tell Auto Scaling how many instances you want (and let it launch/terminate instances to match)
  • terminate_instance_in_auto_scaling_group() to remove specific instances from the Auto Scaling group

You should never directly stop/terminate instances in an Auto Scaling group otherwise Auto Scaling will presume that the instance is failed and will automatically launch a replacement.




回答2:


What you need to do is automate the auto scaling parameters, for desired instances, min instances and max instances. Ideally, you want to change the desired instance amount. This will cause the auto scaler to terminate excessive instances, to meet the desired instance amount.



来源:https://stackoverflow.com/questions/55130327/start-and-stop-aws-ec2-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!