I’m using AWS Load Balancer with 3 EC2 servers, and I’m trying to serve a Maintenance page when site is under maintenance.
This page need to return 503 HTTP code, because it is a proper code for a maintenance mode and will prevent possible problems with SEO.
When I return 503 code from any of my servers, Load Balancer makes it “Not In Service”, and when all servers return 503, website returns a blank page (because all servers are disconnected).
My questions are:
1) Is there any way to serve a custom static page with a message for visitors from Load balancer if there is no healthy servers?
2) Or how to configure Load Balancer’s Health Check that it will not consider 503 as a reason to mark server as “unhealthy”?
Thanks!
4
Answers
You could implement an additional route in your app server, let’s say /hcm (for health check maintenance), that always responds 200 OK. When it’s time for maintenance, you programmatically modify the ELB health check to use the /hcm target which returns 200 OK rather than / or /index.html, which both return 503 Service Unavailable. Revert these changes when exiting maintenance.
The load balancer will serve a
503
for you when you no longer have any healthy server behind it so you should not do anything special.If you return anything but a
200
on the health check, ELB will take the machine out of the load balancer after it fails the configured number of health checks.So to recap, you can potentially serve
503
from your app when in maintenance, but you have to return200
for health checks all the time. If you don’t care about the content of the page, you can simply remove the machines from the load balancer (or fail health checks) and the LB will do the right thing for you.Might not meet your 503 requirement but a good option for this is using s3 and dns failover: https://aws.amazon.com/blogs/aws/create-a-backup-website-using-route-53-dns-failover-and-s3-website-hosting/
I’ve been searching for a quick way to do this. We need to return a 503 error to the world during DB upgrade, but white list a few IPs of developers so they can test it before opening back up to public.
Found a one spot solution::
Go to the Loader Balancer in EC2 and select the load balancer you would like to target. Below, you should see Listeners. Click on a listener, and edit the rule. Create a rule like this:
Now everyone gets a pretty maintenance page returned with a 503 error code, and only two IP addresses in the first rule will be able to browse to the site. Order is important, where the two IP exceptions are on top, then it goes down the list. The last item is always there by default.
Listener Rules for Your Application Load Balancer:
https://docs.aws.amazon.com/elasticloadbalancing/latest/application/listener-update-rules.html