Use DetachInstances with a CLI - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Use DetachInstances with a CLI

The following code examples show how to use DetachInstances.

CLI
AWS CLI

To detach an instance from an Auto Scaling group

This example detaches the specified instance from the specified Auto Scaling group.

aws autoscaling detach-instances \ --instance-ids i-030017cfa84b20135 \ --auto-scaling-group-name my-asg \ --should-decrement-desired-capacity

Output:

{ "Activities": [ { "ActivityId": "5091cb52-547a-47ce-a236-c9ccbc2cb2c9", "AutoScalingGroupName": "my-asg", "Description": "Detaching EC2 instance: i-030017cfa84b20135", "Cause": "At 2020-10-31T17:35:04Z instance i-030017cfa84b20135 was detached in response to a user request, shrinking the capacity from 2 to 1.", "StartTime": "2020-04-12T15:02:16.179Z", "StatusCode": "InProgress", "Progress": 50, "Details": "{\"Subnet ID\":\"subnet-6194ea3b\",\"Availability Zone\":\"us-west-2c\"}" } ] }
PowerShell
Tools for PowerShell

Example 1: This example detaches the specified instance from the specified Auto Scaling group and decreases the desired capacity so that Auto Scaling does not launch a replacement instance.

Dismount-ASInstance -InstanceId i-93633f9b -AutoScalingGroupName my-asg -ShouldDecrementDesiredCapacity $true

Output:

ActivityId : 06733445-ce94-4039-be1b-b9f1866e276e AutoScalingGroupName : my-asg Cause : At 2015-11-20T22:34:59Z instance i-93633f9b was detached in response to a user request, shrinking the capacity from 2 to 1. Description : Detaching EC2 instance: i-93633f9b Details : {"Availability Zone":"us-west-2b","Subnet ID":"subnet-5264e837"} EndTime : Progress : 50 StartTime : 11/20/2015 2:34:59 PM StatusCode : InProgress StatusMessage :

Example 2: This example detaches the specified instance from the specified Auto Scaling group without decreasing the desired capacity. Auto Scaling launches a replacement instance.

Dismount-ASInstance -InstanceId i-7bf746a2 -AutoScalingGroupName my-asg -ShouldDecrementDesiredCapacity $false

Output:

ActivityId : f43a3cd4-d38c-4af7-9fe0-d76ec2307b6d AutoScalingGroupName : my-asg Cause : At 2015-11-20T22:34:59Z instance i-7bf746a2 was detached in response to a user request. Description : Detaching EC2 instance: i-7bf746a2 Details : {"Availability Zone":"us-west-2b","Subnet ID":"subnet-5264e837"} EndTime : Progress : 50 StartTime : 11/20/2015 2:34:59 PM StatusCode : InProgress StatusMessage :
  • For API details, see DetachInstances in AWS Tools for PowerShell Cmdlet Reference.