This particular issue has given me nightmares and has haunted me for about 2 weeks, until I fixed it by trial and error. So if I hope, I can save you some trouble by this article.
The context of the problem:
I had 1 EC2 instance with a RDS, where my application was running
I used a load balancer in front my 1 EC2 instance, because I use the SSL certificate from AWS Certificate Manager and you can’t directly configure it on an EC2 instance
Despite of having a load balancer, I wanted to spare me the second EC2 instance,
Everything looked fine in this setup. My EC2 app was healthy. Happy ending! Not!
Most of the people that I send links to my application to, told me they couldn’t access the app. I had the accessing problem myself. However weirdly only on my phone. From my laptop on all browsers it worked super fast. So this accessibility issue was super random and I had no clue what the problem was.
After some analysis and digging deeper, I found out what the problem was.
When registering a load balancer, you HAVE TO configure at least 2 AZs (availability zones) for it. It won't allow just one. What AWS doesn’t give warning about though, is that you HAVE TO also configure at least 1 EC2 instance with your app in each of these AZs. Otherwise, when the load balancer on AWS with just 1 instance behind it gets a request, it randomly distributes the traffic and might send it to the dead end AZ, with no Ec2 in it.
So just cloning my EC2 and adding it in the second AZ solved the problem! The correct configuration now looks like this:
Beware though, if you make this change, it will take some time to propagate and fix the accessibility. In my case it was 1 or 2 days. So be patient.