Skip to main content

I Love Infra

2 min 385 words

Received a task to modernize our infra, sounds simple from Django + Lambda move it to ECS. Mainly because, I am influencing the team to move towards microservice architecture but we don’t wanna spend most of our time maintaining the architecture so ECS it is.

The reason why we are using Lambda and moving towards MS previously is to cut cost by using multiple Lambdas to handle certain task (request stream and offloading long running tasks by AI agents). And now we decided to not care much about the cost and move back to monolith as the team is small we don’t wanna spent our time maintaining microservices.

I love ECS, had alot of experience using it, I even implement it during my time at Bloomthis, part of the infra modernization.

But this time around I am unfamiliar with Django and I tried my best. Here are some of the learnings I got:

Key Learnings

1. Server Choice: Gunicorn vs Uvicorn

First, I notice that we are using gunicorn. I’ve used FastAPI before and they were using uvicorn by default, but I notice that it is more modern than gunicorn and support async ootb, so I try to change the server for django to use uvicorn: failed miserably because we are using DRF and how we returning request doesnt fit uvicorn way on handling response which is async, so revert back to gunicorn.

2. Health Check Issues

Second, the healthcheck kept on failing from ELB → ECS, this is so frustrating, I stuck 3 hours on this part. This is mainly because on how the Django application works. There’s middleware built in the app and by default it blocks all the hosts that are trying to access the application and we need to explicitly allow it.

So what I did was, bypass the middleware only to allow all host to access the health check endpoint, and now the health check passed.

Infrastructure Strategy

By the way, I implement all of these using Pulumi and it is a very good IaaC framework. The strategy that I use is to not touch the existing infra at all! Everything needs to be tested on DEV environment. I created new Pulumi stack just to test this new architecture and ensure that all smooth before trying to discontinue the old infra.