I have a django app in a dockerfile, and I deployed it with ECS Fargate. It works well, but I’m am not sure about how pricing works.
python manage.py runserver
has to be running all the time because it needs to be listening the incoming requests. Does it mean that my fargate is beign used 24/7. and subsequently billed 24/7?. If that is true, what is the correct way to use a monolithic app in a fargate container minimizing costs?
Dockerfile
FROM python:3.11-alpine
RUN mkdir /app
WORKDIR /app
COPY . .
RUN sed -i 's/r$//g' ./local/entrypoint.sh
RUN chmod +x ./local/entrypoint.sh
RUN pip install -r ./local/requirements.txt
EXPOSE 8000
ENTRYPOINT ["/bin/sh", "./local/entrypoint.sh"]
entrypoint.sh
#!/bin/sh
set -o errexit
set -o pipefail
set -o nounset
echo "------- RUNNING DJANGO COMMANDS -------"
python /app/manage.py makemigrations
python /app/manage.py migrate
python /app/manage.py runserver 0.0.0.0:8000
2
Answers
A Fargate instance runs 24/7 and is billed 24/7. Don’t be confused with Fargate being called "serverless". Just because you don’t manage the underlying server, you are still reserving some AWS hardware (via the Fargate CPU and Memory reservation settings) 24/7, so you pay for those underlying resources 24/7. Fargate is not AWS Lmabda. Fargate runs all the time.
That’s correct, running your Django Application on AWS ECS Fargate means that your application is running 24/7.
Therefore you’ll be billed for that amount of time.
There are a few things you can do in order to minimize the costs in AWS ECS Fargate itself:
Scale with Demand: If you expect the demand for your application
to be variable, consider setting up Auto Scaling for ECS
services. This allows you to automatically adjust the desired
count of tasks in your service up and down within minimum and
maximum count values.
Optimize Resources: Select the right task size. When defining a task
for Fargate, you specify the amount of vCPU and memory it needs.
Make sure you choose a configuration that best fits the needs of
your application without over-provisioning.
Use Spot Capacity: Starting from late 2019, Fargate started
supporting spot pricing, which can give you up to 70% cost savings
over On-Demand prices. These are spare capacity and can be
interrupted, but for stateless or fault-tolerant applications, they
are a good choice.
Now, if I may give you a tip; don’t use
python manage.py runserver
in production. Therunserver
command is built for local development, you rather look at something likegunicorn
for production.