I have a backend for an e-commerce app, built on Django, and hosted on AWS ECS (more precisely with Fargate). Django is in a container, and this container works with another based on Nginx. ECS is used to host several tasks of this group of containers. Theses tasks can be removed / replaced / duplicated depending on the load to the app. The aim is to respond with a 503 status codes when maintenance mode is activated.
I already have a maintenance mode, based on a custom Django middleware. I use an environment variable to check if the site is in maintenance.
class MaintenanceMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
try:
maintenance = int(os.environ['MAINTENANCE'])
except Exception:
maintenance = 0
if maintenance:
return HttpResponse(status=503)
return self.get_response(request)
Very simple and efficient. But to enable maintenance mode, I have to update my task definition in ECS in order to change the environment variable ; and then, redeploy the service. In this way, every running ECS task is replaced with a new task, which contains the new value of the environment variable.
It's not something very easy to do. And in my point of view, a task definition must not be changed in order to manage something ephemeral like the maintenance mode. But I'm lacking of an idea to have a better way to implement this.
I know that I can use the database or for example, a file in S3, instead of the environment variable in the middleware. But this means that every request on Django (including users requests) would be impacted by an aditonnal time (necessary to query the database or reading an S3 file). Furthermore, you know that almost everything is paid in AWS (and multiplying reads on a file may involves costs).
Another idea is to add a rule on the ALB to respond directly with a 503. Nevertheless, ALB responses don't includes the CORS headers, which leads to a unfunctional app for users, instead of a nice maintenance screen. (the frontend is built on top of Angular)
So i'm throwing a bottle to the sea. Have you ideas to implement a maintenance mode with theses constraints ?
Every contribution is welcomed