Detach EC2 instances from your Auto Scaling group
You can remove (detach) an instance that is in the InService
state
from an Auto Scaling group. After the instance is detached, you can manage it independently
from the rest of the Auto Scaling group. By detaching an instance, you can:
-
Move an instance out of one Auto Scaling group and attach it to a different group. For more information, see Attach EC2 instances to your Auto Scaling group.
-
Test an Auto Scaling group by creating it using existing instances running your application. You can then detach these instances from the Auto Scaling group when your tests are complete.
When you detach instances, consider the following:
-
If the number of instances that you are detaching decreases the size of the Auto Scaling group below its minimum capacity, you must decrement the minimum capacity for the group before you can detach the instances.
-
When you detach instances, you have the option of decrementing the desired capacity for the Auto Scaling group by the number of instances that you are detaching. If you choose not to decrement the capacity, Amazon EC2 Auto Scaling launches new instances to replace the ones that you detach. If you decrement the capacity but detach multiple instances from the same Availability Zone, Amazon EC2 Auto Scaling can rebalance the Availability Zones unless you suspend the
AZRebalance
process. For more information, see Suspend and resume a process for an Auto Scaling group. -
If you detach an instance from an Auto Scaling group that has an attached load balancer target group or Classic Load Balancer, the instance is deregistered from the load balancer. If connection draining (deregistration delay) is enabled for your load balancer, Amazon EC2 Auto Scaling waits for in-flight requests to complete.
Note
If you are detaching instances that are in the Standby
state,
exercise caution. Attempting to detach instances after putting them into the
Standby
state may cause other instances to terminate
unexpectedly.
Detach instances (console)
Use the following procedure to detach an instance from your Auto Scaling group.
To detach an instance from an existing Auto Scaling group
Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/
, and choose Auto Scaling Groups from the navigation pane. -
Select the check box next to your Auto Scaling group.
A split pane opens up in the bottom of the page.
-
On the Instance management tab, in Instances, select an instance and choose Actions, Detach.
-
In the Detach instance dialog box, keep the Replace instance check box selected to launch a replacement instance. Clear the check box to decrement the desired capacity.
-
When prompted for confirmation, type
detach
to confirm removing the specified instance from the Auto Scaling group, and then choose Detach instance.
Detach instances (AWS CLI)
Use the following procedure to detach an instance from your Auto Scaling group.
The examples use an Auto Scaling group with the following configuration:
-
Auto Scaling group name =
my-asg
-
Minimum size =
1
-
Maximum size =
5
-
Desired capacity =
4
To detach an instance from an existing Auto Scaling group
-
List the current instances using the following describe-auto-scaling-instances command.
aws autoscaling describe-auto-scaling-instances
The following example response shows that the group has four running instances.
{ "AutoScalingInstances": [ { "ProtectedFromScaleIn": false, "AvailabilityZone": "us-west-2a", "LaunchTemplate": { "LaunchTemplateName": "my-launch-template", "Version": "1", "LaunchTemplateId": "lt-050555ad16a3f9c7f" }, "InstanceId": "i-05b4f7d5be44822a6", "AutoScalingGroupName": "my-asg", "HealthStatus": "HEALTHY", "LifecycleState": "InService" }, { "ProtectedFromScaleIn": false, "AvailabilityZone": "us-west-2a", "LaunchTemplate": { "LaunchTemplateName": "my-launch-template", "Version": "1", "LaunchTemplateId": "lt-050555ad16a3f9c7f" }, "InstanceId": "i-0c20ac468fa3049e8", "AutoScalingGroupName": "my-asg", "HealthStatus": "HEALTHY", "LifecycleState": "InService" }, { "ProtectedFromScaleIn": false, "AvailabilityZone": "us-west-2a", "LaunchTemplate": { "LaunchTemplateName": "my-launch-template", "Version": "1", "LaunchTemplateId": "lt-050555ad16a3f9c7f" }, "InstanceId": "i-0787762faf1c28619", "AutoScalingGroupName": "my-asg", "HealthStatus": "HEALTHY", "LifecycleState": "InService" }, { "ProtectedFromScaleIn": false, "AvailabilityZone": "us-west-2a", "LaunchTemplate": { "LaunchTemplateName": "my-launch-template", "Version": "1", "LaunchTemplateId": "lt-050555ad16a3f9c7f" }, "InstanceId": "i-0f280a4c58d319a8a", "AutoScalingGroupName": "my-asg", "HealthStatus": "HEALTHY", "LifecycleState": "InService" } ] }
-
Detach an instance and decrement the desired capacity using the following detach-instances command.
aws autoscaling detach-instances --instance-ids
i-05b4f7d5be44822a6
\ --auto-scaling-group-namemy-asg
--should-decrement-desired-capacity -
Verify that the instance is detached using the following describe-auto-scaling-instances command.
aws autoscaling describe-auto-scaling-instances
The following example response shows that there are now three running instances.
{ "AutoScalingInstances": [ { "ProtectedFromScaleIn": false, "AvailabilityZone": "us-west-2a", "LaunchTemplate": { "LaunchTemplateName": "my-launch-template", "Version": "1", "LaunchTemplateId": "lt-050555ad16a3f9c7f" }, "InstanceId": "i-0c20ac468fa3049e8", "AutoScalingGroupName": "my-asg", "HealthStatus": "HEALTHY", "LifecycleState": "InService" }, { "ProtectedFromScaleIn": false, "AvailabilityZone": "us-west-2a", "LaunchTemplate": { "LaunchTemplateName": "my-launch-template", "Version": "1", "LaunchTemplateId": "lt-050555ad16a3f9c7f" }, "InstanceId": "i-0787762faf1c28619", "AutoScalingGroupName": "my-asg", "HealthStatus": "HEALTHY", "LifecycleState": "InService" }, { "ProtectedFromScaleIn": false, "AvailabilityZone": "us-west-2a", "LaunchTemplate": { "LaunchTemplateName": "my-launch-template", "Version": "1", "LaunchTemplateId": "lt-050555ad16a3f9c7f" }, "InstanceId": "i-0f280a4c58d319a8a", "AutoScalingGroupName": "my-asg", "HealthStatus": "HEALTHY", "LifecycleState": "InService" } ] }