可以使用AWS CLI命令将自动扩展组中的所有实例终止,同时将所需、最小和最大实例设置为0来实现停止服务器的效果。
具体
aws autoscaling update-auto-scaling-group --auto-scaling-group-name --min-size 0 --max-size 0 --desired-capacity 0
aws autoscaling terminate-instance-in-auto-scaling-group --instance-ids --no-should-decrement-desired-capacity
此命令将立即终止所有实例,并从自动扩展组中删除它们。
注意:在执行上述命令之前,请确保已备份并保存了实例中的所有数据。
代码示例:
#!/bin/bash
ASG_NAME=
echo "Stopping Auto Scaling Group: $ASG_NAME"
aws autoscaling update-auto-scaling-group --auto-scaling-group-name $ASG_NAME --min-size 0 --max-size 0 --desired-capacity 0
echo "Terminating Instances in Auto Scaling Group"
INSTANCES=$(aws autoscaling describe-auto-scaling-instances --query "AutoScalingInstances[].InstanceId" --output text)
for INSTANCE in $INSTANCES
do
aws autoscaling terminate-instance-in-auto-scaling-group --instance-ids $INSTANCE --no-should-decrement-desired-capacity
done
使用此脚本可以轻松停止服务器。
下一篇:AWS自动启动脚本