This can be achieved by using Amazon SDK ( I am almost done with it, will put it on github ), utilizing the SNS, EC2 and Autoscaling service.
I have followed the below steps to achieve this:
- Enable HTTP notification and subscribed my webserver.
- Added a lifecycle-hook with heartbeat of 1 min (to wait for 1 min before terminating) to my autoscaling group for terminating server
- Created an index file to parse the message to detect what kind of message it is ( i.e Launch or Terminate)
- Once type of event is decided i queried EC2 to get the private ip of the instance
- In case of Launch wait till header 200 is recieved and then add the ip to nginx config and reload
- In case of Terminate remove the IP from config and reload nginx
Please find the script here https://github.com/singhupendra/aws-autoscale