To automatically add/remove new ec2 instances to nginx upstream server and avoid ELB.This script checks the health of the new instance prior adding the it to the load balancer.
You need to perform several task before using this. Steps required are given below:
1. Create a SNS topic
You can refer below url for same
http://docs.aws.amazon.com/sns/latest/dg/CreateTopic.html
2. Subscribe an endpoint(HTTP) (webserver where you will recieve the SNS notification and run this script)
You can refer below url for same
http://docs.aws.amazon.com/sns/latest/dg/SubscribeTopic.html
3. Create an autoscale lifecycle before termination
http://docs.aws.amazon.com/AutoScaling/latest/DeveloperGuide/adding-lifecycle-hooks.html
4. Provide access to nginx file so that webserver can edit the file
use setfacl command for this
5. Provide privilege to reload the nginx service to the use executing this script
use visudo and add
nginx ALL=(root)NOPASSWD:/etc/init.d/nginx (trying to find better and secure way to reload the service)
6. Access Key ID and Secret Access Key
http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSGettingStartedGuide/AWSCredentials.html
Just copy the files in your webroot (within a desired folder).
Edit the config file and provide your acess key id, secret access key and region
NOTE : YOU MIGHT NEED TO TWEAK FEW THINGS AS PER YOUR ENVIRONMENT.