Typically there could be some scenario , when a scale in event occurs and Auto Scaling determines that an instance is no longer necessary, it immediately puts the instance into Terminating state. The instance remains in this state until the instance fully terminates. By default, We cannot connect to an instance that is terminating
Recently aws has given the addition features that give you additional control over the EC2 instances managed by each of your Auto Scaling Groups which is called Auto Scaling -Lifecycle Management .
Now we can choose to put the instance in a Terminating:Wait state using Auto Scaling’s PutLifecycleHook command. This option allows you to access the terminating instance.
Create a life-cycle hook to retrieve logs from a terminating server
1. Create a notification target to receives the SNS notification that an instance is in the Pending:Waiting state and make a note of its Amazon Resource Name (ARN).
2. Create an IAM role to AutoScaling Notification Access and make a note of its Amazon Resource Name (ARN).
3. Use below mentioned command to create life-cycle hook
"aws autoscaling put-lifecycle-hook --auto-scaling-group-name QA-Server-AS-Group --lifecycle-hook-name TestHook --lifecycle-transition autoscaling:EC2_INSTANCE_TERMINATING --role-arn arn:aws:iam::651968884577:role/Autoscaling-life-cycle --notification-target-arn arn:aws:sns:ap-southeast-1:651968884577:QA-Server-Memory"
Note : Now you have created a lifecycle hook that applies to any instances that are being terminated from the Auto Scaling group When a scale in event occurs, the Auto Scaling group puts the instance in a Terminating:Wait state.Now connect to the instance to download the log files you required.
By default, the instance remains in the Terminating:Wait state for 60 mintues.Auto Scaling continues the termination process, If you take no action during that time.
4. Use given command to check life-cycle-hooks once it is created.
"aws autoscaling describe-lifecycle-hooks --auto-scaling-group-name QA-Server-AS-Group"
5. Use given command to delete life-cycle-hooks once the debugging is done.
"aws autoscaling delete-lifecycle-hook --auto-scaling-group-name QA-Server-AS-Group --lifecycle-hook-name TestHook"