How to use Boto to self-terminate instance its running on?

Question:

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?

Asked By: Andrew Parmar

||

Answers:

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

If you want to get the instance id automatically (if you are using autoscale group you most likely don’t know). So You can use this –

from subprocess import Popen, PIPE
from ec2_metadata import ec2_metadata # pip3 install ec2-metadata

REGION = 'ap-southeast-1'
instance_id = ec2_metadata.instance_id

command_response = Popen(f"(aws autoscaling terminate-instance-in-auto-scaling-group --instance-id {instance_id} --should-decrement-desired-capacity --region {REGION})", stderr=PIPE, stdout=PIPE, shell=True)

....

Don’t forget to attach autoscaling policy to your instance/template instance.

Answered By: ashraf minhaj