title: Auto Scaling not working properly with Tomcat in Elastic Beanstalk
body: <p>I created an Autoscaled Elastic Beanstalk application using Tomcat (java based) to provide a webservice.</p>
  
  <p>As part of first-time initialization I have to copy and uncompress some large files from S3 to the underlying EC2 instance, ( this step is not required if the instance is restarted and already has the data files). This is all done within the servlets init() method and works perfectly within a non scaled environment - but it is slow - taking about 30 minutes.</p>
  
  <p>But when I created a autoscaled environment i noticed two problems:</p>
  
  <ol>
  <li><p>It actually started three EC2 instances even though not a single call made to the service, I assume whilst monitoring the first instance to start it wasn't responding because it was busy in the init() method and therefore it started another instance and so on. </p></li>
  <li><p>It indicates that the services were ready even though they were busy in their init() methods and not yet ready to accept requests.</p></li>
  </ol>
  
  <p>Surely Elastic Beanstalk should wait for the init() method of the first instance to complete <strong>BEFORE</strong> it indicates as ready and before it monitors it as busy and considers starting new instances.</p>
  
  <p>Then I left it for twenty minutes and because there was no activity two of the three instances were then stopped. So I thought okay I can work round this the first time I deploy I can just wait for a bit and it will sort itself out, and then when the server is genuine busy I already have an extra two instances all ready to go with the data preloaded so they will start quickly. But I looked again and found those two instances have now been terminated and so think the problem will be that if EB decides to start a new instance because instance1 is genuinely busy there will be a period of time where instance2 is not really ready but is accepting requests, therefore user requests may succeed or may fail depending on whether they are redirected to instance1 (works) or instance2 (fails).</p>
  
  <p>Am I correct in my assumptions, why doesn't EB respect init() method and how can I work round this problem ?</p>