I have a problem I am trying to solve. When the auto scaling creates a new EC2 instance, the load balancer gets a positive health check result fairly quickly. However, the problem is that we have a custom application that runs when an instance starts and we do not want the instance attached to the load balancer until it has completed. So I need to find a way to make the load balancer wait until our custom application is finished running. What is the best way to accomplish this?
Use the health check capabilities to ensure that your instance is not in service before your custom program is ready.
If you have a classic ELB, you can point to an specific enpoint, for example, /health_check.php that performs the necessary checks in your application to ensure that your custom program is ready. Then, when a new instance is attached to the load balancer, it will perform the health check action that you have configured an expects a right behaviour of it. If not, the health check fails and will continue to perform new health checks in the configured intervals until instance become in service.
On best practice for web servers behind a load balancer, for example, is to add an specific endpoint that checks that all the necessary pieces for your application is running and in service, that is database, storage, caching processes or whatever you new or consider critical for your system.
The official AWS documentation is very useful for this.
Hope this helps