Reputation: 1
I'm using this code to detach instances from an Auto Scaling group
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)
But, I want to be able to detach multiple instances from multiple groups using one single script. I'm pretty new to Python and AWS CLI commands. Any help will be appreciated. Thanks.
I did some changes to the previous script as below and currently stuck here.
import os, subprocess
CMD = "aws autoscaling --region us-east-1 describe-auto-scaling-groups --query 'AutoScalingGroups[?contains(Tags[?Key==`Name`].Value,`test_asg`)].[AutoScalingGroupName]' --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 describe-auto-scaling-groups --query 'AutoScalingGroups[].Instances[*].[InstanceId]' --output text --auto-scaling-group-name "+l
output2 = subprocess.check_output(new_cmd, shell=True)
lst2 = []
for char in output2:
lst2.append(char)
lst2 = ''.join(lst2).split('\n')
lst2.remove("")
print(lst2)
for l2 in lst2:
l2 = '"'+str(l2)+'"'
new_cmd_2 = "aws autoscaling --region us-east-1 detach-instances --auto-scaling-group-name $CMD --should-decrement-desired-capacity --instance-ids "+l
subprocess.check_output(new_cmd_2, shell=True)
Upvotes: 0
Views: 872
Reputation: 269500
An Amazon EC2 Auto Scaling group can be used to automatically provision a number of Amazon EC2 instances based upon need.
For example, when the intances are busy, a scaling policy can automatically launch additional instances ("scale-out"). Then, at night, when the instances are under-utilized, Auto Scaling can automatically remove instances ("scale-in").
Auto Scaling also monitors the health of instances in an Auto Scaling group and will automatically replace any instances that have failed.
This is done by specifying a Launch Configuration that defines how to launch a new instance, including Instance Type, AMI and Security Group.
Types of scaling include:
Dynamic Scaling for Amazon EC2 Auto Scaling - Amazon EC2 Auto Scaling can be controlled via:
The simplest method is to use 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.
Bottom line: You should not be detaching and attaching instances to an Auto Scaling group. Rather, you should configure scaling policies for this to happen automatically, based upon a nominated metric (eg CPU Utilization, number of users, work backlog, etc).
Please note that Auto Scaling launches new instances or terminates instances. It does not start/stop instances.
Upvotes: 1