AWS Autoscaling Group
This thing started when we have some user-data that do and install a lot of things, especially on Windows, the booting alone could take up to 6 minutes. Often the instance couldn't catch up with the ELB health check and causing the instance stuck in the termination loop.
To overcome the issue, I decided to use the lifecycle hook at instance launch. I was adding these commands to my user-data to extend the time-out and signal the hook of the task completion.
The new instance launch issue was solved then, but we found another issue when our CI/CD tool agent deploying a new application version to the Autoscaling group would make the instance unhealthy from the ELB health check. We didn't want to modify the health check threshold or the interval since it'd make the infrastructure not elastic enough and not scalable enough. We decided to go with AWS Codedeploy as our CD tool that has integration with ELB and Autoscaling Group.
AWS Codedeploy integrates with ELB to manage traffic control before and after installation.
AWS CodeDeploy integrates with EC2 Auto Scaling, which can deploy our revisions to the new instances automatically. So every new instance launch will be updated with the application revision that was most recently deployed.
So now we end up with two hooks since, under the hood, Codedeploy is using a lifecycle hook too to integrate with EC2 Auto Scaling
I found out that If hook 1 is executed firstly and takes default action CONTINUE, then hook 2 will kick in. If custom action is completed and continues the launch process bypassing the default action ABANDON, the instance will be launched, but if custom action is not successful, then it will terminate. So CONTINUE allows any other lifecycle hooks to complete.
Finally, we removed the initial hook and its related commands in user-data
Below is an excellent flow chart from the AWS blog that explains what happens with EC2 Auto Scaling with Codedeploy.