'EC2 Instance reboots automatically and disassociates my Elastic IP and Target Groups
I have my API configured on an EC2 instance (AWS) and have reserved an Elastic IP to be associated with my instance. I also have an ELB configured that points to my public IP (reserved) and with the target group pointing to my EC2 instance.
Every 24 hours (sometimes at a gap of 3-6 hours) my instance gets terminated automatically and a new instance gets created by AWS. While this is fine, it unlinks my Elastic IP (Even after reserving) and removed the new instance from my ELB target groups.
I get a 503 Service Temporarily Unavailable error on my API since the public IP becomes invalid. Everytime this happens (roughly every 24 hours) I need to re-associate the IP with my instance (Elastic IP) and register a new target in my Load Balancer.
How do I go about fixing this?
Solution 1:[1]
It sounds like you have an Amazon EC2 Auto Scaling group configured. The Auto Scaling group will automatically scale the number of instances based upon rules you have configured. It will also automatically replace any instances that fail a Health Check.
First, you should check the Scaling History of the Auto Scaling group to determine why it replaced instances.
Second, you should not be pointing your Elastic Load Balancer to a specific IP address. Instead, it can automatically send traffic to any instance in the Target Group assigned to the Load Balancer. If Auto Scaling replaces an instance, then the new instance will automatically add itself to the Target Group. There should be no need for a manual fix.
In fact, there is no need for you to use a specific public IP address. Instead, users should access the Load Balancer via its DNS name and the traffic will be redirected to instances via their private IP address. For full security, these EC2 instances should be placed in a private subnet (so public IP addresses won't work anyway).
Solution 2:[2]
This was a while back, and what seems to have been working well for me is adding the applicable Target Group under the Target Group section under the Auto Scaling Groups.
So go to EC2 Console, then at the very bottom of the left menu (currently for me anyway), there's the Auto Scaling Groups. Click on the auto-scaling group from the list, and on the Details tab, click Edit, and then add the applicable Target Group.
This then seems to keep them in sync even as the instances change.
This text is from https://serverfault.com/questions/1000783/application-load-balancer-with-aws-elastic-beanstalk-target-group and it worked for me.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | John Rotenstein |
| Solution 2 | djiki |
