使用CLI立即停止AWS自动调整组中的所有实例

2024-06-17 08:33:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用这段代码从自动缩放组中分离实例

import os, subprocess
CMD = "aws autoscaling --region us-east-1 describe-auto-scaling-groups --query 'AutoScalingGroups[?contains(Tags[?Key==`Test_Tag_1`].Value,`SBX_Min_Det`)].Instances[*].[InstanceId]' --output text"
output = subprocess.check_output(CMD, shell=True)
lst = []
for char in output:
   lst.append(char)
lst = ''.join(lst).split('\n')
lst.remove("")

print (lst)
for l in lst:
   l = '"'+str(l)+'"'
   new_cmd = "aws autoscaling --region us-east-1 detach-instances --auto-scaling-group-name Test_Group --should-decrement-desired-capacity --instance-ids "+l
   subprocess.check_output(new_cmd, shell=True)

但是,我希望能够使用一个脚本从多个组中分离多个实例。我对Python和awscli命令非常陌生。任何帮助都将不胜感激。谢谢。在

我对之前的脚本做了一些修改,如下所示,目前仍停留在这里。在

^{pr2}$

Tags: 实例testcmdawsautooutputcheckshell
1条回答
网友
1楼 · 发布于 2024-06-17 08:33:14

amazonec2自动伸缩组可用于根据需要自动提供多个amazonec2实例。在

例如,当入口繁忙时,扩展策略可以自动启动其他实例(“向外扩展”)。然后,在夜间,当实例未被充分利用时,自动缩放可以自动移除实例(“扩展”)。在

自动缩放还监视自动缩放组中实例的运行状况,并将自动替换任何失败的实例。在

这是通过指定一个启动配置来完成的,该配置定义了如何启动一个新实例,包括实例类型、AMI和安全组。在

Types of scaling包括:

  • 始终保持当前实例级别(例如替换失败的实例)
  • 手动缩放
  • 按时间表缩放
  • 基于需求的规模

Dynamic Scaling for Amazon EC2 Auto Scaling - Amazon EC2 Auto Scaling可通过以下方式控制:

  • 目标跟踪缩放:根据特定指标的目标值增加或减少组的当前容量。这与恒温器保持家里温度的方式相似,你选择一个温度,恒温器完成其余的工作。在
  • 步进缩放:根据一组缩放调整(称为步进调整)来增加或减少组的当前容量,这些调整取决于报警中断的大小。在
  • 简单缩放:根据单个缩放调整增加或减少组的当前容量。在

最简单的方法是使用Target Tracking Scaling Policies for Amazon EC2 Auto Scaling

With target tracking scaling policies, you select a scaling metric and set a target value. Amazon EC2 Auto Scaling creates and manages the CloudWatch alarms that trigger the scaling policy and calculates the scaling adjustment based on the metric and the target value. The scaling policy adds or removes capacity as required to keep the metric at, or close to, the specified target value. In addition to keeping the metric close to the target value, a target tracking scaling policy also adjusts to the changes in the metric due to a changing load pattern.

底线:您应该而不是将实例分离并附加到自动缩放组。相反,您应该根据指定的指标(例如CPU利用率、用户数量、工作积压工作等)配置伸缩策略,使之自动发生。在

请注意,自动缩放会启动新实例终止实例。它会启动/停止实例。在

相关问题 更多 >