Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I want to setup autoscaling in AWS. I don't want to use Elastic Load Balancer.

Autoscalling in Amazon creates EC2 instances seamlessly during demand spikes to maintain performance, and decreases automatically during demand lulls to minimize costs.

Since this EC2 instances are created automatically, their host names are unknown to NGINX.

I know and already have upstream setup in nginx to 10 EC2 instances.

I want to be able to add/update/delete automatically server names to my upstream nginx configuration, when autoscaling adds/updates/deletes EC2 instances.

share|improve this question
You need to remove "autoscaling" from your question. Autoscaling is an AWS term. I think what you mean is you want to automatically scale (horizontally), by adding more upstream nodes to your nginx acting as an LB, and you are asking how to automatically modify your nginx configuration when upstream nodes are added/deleted/modified. If this is so, please edit your question accordingly. –  talonx Jul 16 '13 at 17:16
well, actually, I know what autoscalling is, and I ment to say that. I want to mix both. I'll update the question. –  Luis Lobo Borobia Aug 5 '13 at 10:46
The question is clearer now, in its intent. I wanted to vote to re-open, but I don't see an option - guess I don't have enough rep yet. –  talonx Aug 5 '13 at 12:52
Thank you @talonx I hope others can upvote to find my answer –  Luis Lobo Borobia Aug 7 '13 at 5:32
I think you can combine AWS autoscaling notifications (delivered using SNS) - assuming it returns the host name of the newly created/terminated instance - and one of the third party nginx APIs to update and reload your nginx configuration. Sorry for being vague - I'm not very familiar with the autoscaling API. –  talonx Aug 7 '13 at 6:22

2 Answers 2

Thank you @talonx, I've done some research, Amazon Autoscale has an api to query current autoscaling group status, and enumerates its members. It returns instance id (http://docs.aws.amazon.com/AutoScaling/latest/DeveloperGuide/api_requests.html#query-example), then you can use the describe tools to get the server name (http://docs.aws.amazon.com/AWSEC2/latest/CommandLineReference/ApiReference-cmd-DescribeInstances.html) and finally recreate the upstream include file. I could sense the Autoscaling notifications to launch a process that performs these tasks.

I still didn't implement it but its a way to go.

One can also use Autocaling with SNS http://docs.aws.amazon.com/AutoScaling/latest/DeveloperGuide/ASGettingNotifications.html

share|improve this answer
This is basically what I did. I wrote a ruby script that runs every N minutes. Using the AWS SDK it queries for the members of the the ASG and using an ERB template it generates a new config. If the new config is different than the current config it copies it into place and tells the daemon (haproxy in my case) to reload its config. Note that instances remain in the ASG for a bit after they are terminated so make sure the instance.status == :running. Also note that if it takes N minutes after the instance is launched for it to serve requests don't use it unil now > instance.launch_time + N. –  Mark Wagner Aug 23 '13 at 19:48
Thank you @MarkWagner. Is there any possibility you can share that script somewhere? Gist, github? Thanks! –  Luis Lobo Borobia Aug 27 '13 at 12:29

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:

  1. Enable HTTP notification and subscribed my webserver.
  2. Added a lifecycle-hook with heartbeat of 1 min (to wait for 1 min before terminating) to my autoscaling group for terminating server
  3. Created an index file to parse the message to detect what kind of message it is ( i.e Launch or Terminate)
  4. Once type of event is decided i queried EC2 to get the private ip of the instance
  5. In case of Launch wait till header 200 is recieved and then add the ip to nginx config and reload
  6. In case of Terminate remove the IP from config and reload nginx
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.