title: How do I work out why an ECS health-check is failing?
body: <h2>Outline:</h2>
  
  <p>I have a very simple ECS container which listens on port 5000 and writes out HelloWorld, plus the hostname of the instance it is running on. I want to deploy many of these containers using ECS and load balance them just to really learn more about how this works. And it is working to a certain extent but my health check is failing (time out) which is causing the containers tasks to be bounced up and down.</p>
  
  <h2>Current configuration:</h2>
  
  <ul>
  <li>1 VPC ( 10.0.0.0/19 )</li>
  <li>1 Internet gateway</li>
  <li>3 private subnets, one for each AZ in eu-west-1 (10.0.0.0/24, 10.0.1.0/24, 10.0.2.0/24)</li>
  <li>3 public subnets, one for each AZ in eu-west-1 (10.0.10.0/24, 10.0.11.0/24, 10.0.12.0/24)</li>
  <li>3 NAT instances, one in each of the public subnets, routing 0.0.0.0/0 to the Internet gateway and each assigned an Elastic IP</li>
  <li>3 ECS instances, again one in each private subnet with a route to the NAT instance in the corresponding public subnet in the same AZ as the ECS instance</li>
  <li>1 ALB load balancer (Internet facing) which is registered with my 3 public subnets</li>
  <li>1 Target group (with no instances registered as per ECS documentation) but a health check set up on the 'traffic' port at /health</li>
  <li>1 Service bringing up 3 tasks spread across AZs and using dynamic ports (which are then mapped to 5000 in the docker container)</li>
  </ul>
  
  <h2>Routing</h2>
  
  <p>Each private subnet has a rule to 10.0.0.0/19, and a default route for 0.0.0.0/0 to the NAT instance in public subnet in the same AZ as it.</p>
  
  <p>Each public subnet has the same 10.0.0.0/19 route and a default route for 0.0.0.0/0 to the internet gateway.</p>
  
  <h2>Security groups</h2>
  
  <p>My instances are in a group that allows egress to anywhere and ingress on ports 32768 - 65535 from the security group the ALB is in.</p>
  
  <p>The ALB is in a security group that allows ingress on port 80 only but egress to the security group my ECS instances are in on any port/protocol</p>
  
  <h2>What happens</h2>
  
  <p>When I bring all this up, it actually works - I can take the public dns record of the ALB and refresh and I see responses coming back to me from my container app telling me the hostname. This is exactly what I want to achieve <em>however</em>, it fails the health check and the container is drained, and replaced - with another one that fails the health check. This continues in a cycle, I have never seen a single successful health check.</p>
  
  <h2>What I've tried</h2>
  
  <ul>
  <li>Tweaked the health check intervals to make ECS require about 5
  minutes of solid failed health-checks before killing the task. I
  thought this would eliminate it being a bit sensitive when the task
  starts up? This still goes on to trigger the tear-down, despite me
  being able to view the application running in my browser throughout.</li>
  <li>Confirmed the /health url end point in a number of ways. I can retrieve it publicly via the ALB (as well as view the main app root url at '/') and curl tells me has a proper 200 OK response (which the health check is set to look for by default). I have ssh'ed into my ECS instances and performed a curl --head {url} on '/' and '/health' and both give a 200 OK response. I've even spun up another instance in the public subnet, granted it the same access as the ALB security group to my instances and been able to curl the health check from there.</li>
  </ul>
  
  <h2>Summary</h2>
  
  <p>I can view my application correctly load-balanced across AZs and private subnets on both its main url '/' and its health check url '/health' through the load balancer, from the ECS instance itself, and by using the instances private IP and port from another machine within the public subnet the ALB is in. The ECS service just cannot see this health check once without timing out. What on earth could I be missing??</p>