Issue
I need to terminate an instance from an AutoScalingGroup as the policies ASG has are leaving the scaled out instances running longer than desired. I need to terminate said instance after its done running a python process.
The code already uses Boto to access other AWS services, so I'm looking to leverage Boto to self-terminate. I have been told that I need to detach the instance from its ASG prior to terminate to avoid side effects.
Any idea how I can go about doing this detachment and self-termination?
Solution
An instance can be removed from an Auto Scaling Group by using detach_instances()
:
Removes one or more instances from the specified Auto Scaling group.
After the instances are detached, you can manage them independent of the Auto Scaling group.
If you do not specify the option to decrement the desired capacity, Amazon EC2 Auto Scaling launches instances to replace the ones that are detached.
response = client.detach_instances(
InstanceIds=[
'string',
],
AutoScalingGroupName='string',
ShouldDecrementDesiredCapacity=True|False
)
So, the steps would be:
- Obtain the Instance ID to be removed
- Call
detach_instances(InstanceIds=['i-xxx'], ShouldDecrementDesiredCapacity=True)
- Call
terminate_instances(InstanceIds=['i-xxx'])
This can be run from the instance itself, or from anywhere on the Internet.
Answered By - John Rotenstein Answer Checked By - David Goodson (WPSolving Volunteer)