AWS Fargate Flask Deploy: 5 Costly Mistakes to Avoid
AWS Fargate runs your containers without servers to manage, and a Flask app behind an Application Load Balancer is one of the most common things people put on it. The earlier version of this post had a Dockerfile and OpenTofu code for that setup. I tested both without an AWS account: the Dockerfile with Docker 29.10.0 and its build checks, the infrastructure code with OpenTofu 1.13.9 (tofu validate and tofu plan with fake credentials, so nothing was created). The Dockerfile works with two smaller problems. The OpenTofu code did not validate at all, because half of the stack was missing. Below is what failed and a complete version that plans cleanly.

Contents
The Flask container for AWS Fargate
The app for this AWS Fargate test is a minimal Flask module with a /health route, because the load balancer target group checks that path. The requirements are pinned (flask==3.1.3, gunicorn==23.0.0). The old multi-stage Dockerfile built a 210MB image and ran fine:
ok
hello from flask
uid=100(app) gid=101(app) groups=101(app)
[2026-11-17 14:59:18 +0000] [1] [INFO] Starting gunicorn 23.0.0
[2026-11-17 14:59:18 +0000] [1] [INFO] Listening at: http://0.0.0.0:5000 (1)
[2026-11-17 14:59:18 +0000] [1] [INFO] Using worker: gthread
The good parts held up for AWS Fargate: gunicorn instead of the Flask development server, binding to 0.0.0.0 so the load balancer can reach the task, and a non-root user (app, UID 100). Two things were not as good as they looked.
Docker build checks: three warnings
$ docker build --check . 2>&1 | grep -E '^(WARNING|Check complete)'
Check complete, 3 warnings have been found!
WARNING: FromAsCasing - https://docs.docker.com/go/dockerfile/rule/from-as-casing/
WARNING: LegacyKeyValueFormat - https://docs.docker.com/go/dockerfile/rule/legacy-key-value-format/
WARNING: LegacyKeyValueFormat - https://docs.docker.com/go/dockerfile/rule/legacy-key-value-format/
FROM python:3.11-slim as builder mixes upper and lower case keywords (write AS builder), and ENV PYTHONDONTWRITEBYTECODE 1 uses the old space format. The LegacyKeyValueFormat rule asks for ENV KEY=value. Neither breaks the build today; they matter once you make CI fail on build warnings.
The builder stage did nothing useful
The builder ran pip wheel --no-deps, so only two wheels reached the final image:
$ docker run --rm flask-fargate-app ls /wheels
flask-3.1.3-py3-none-any.whl
gunicorn-23.0.0-py3-none-any.whl
Yet the running container had Jinja2 3.1.6 and Werkzeug 3.1.9 installed. The final pip install /wheels/* downloaded the missing dependencies from PyPI, unpinned, during the build. The builder installed gcc and libpq-dev for nothing, since neither package needs compiling. Drop --no-deps so the builder produces every wheel, then install with pip install --no-index --find-links /wheels -r requirements.txt so the final stage cannot reach PyPI (I did not rebuild with this variant here). Or, for an app this small, use a single stage.
The OpenTofu code did not validate
For the AWS Fargate infrastructure, I put the old post's blocks into one file and ran tofu validate, which needs no credentials:
Error: Reference to undeclared resource
on main.tofu line 32, in resource "aws_lb" "main":
32: security_groups = [aws_security_group.alb_sg.id]
There is no managed resource "aws_security_group" "alb_sg" definition in the
root module.
Error: Reference to undeclared resource
on main.tofu line 59, in resource "aws_ecs_task_definition" "app":
59: execution_role_arn = aws_iam_role.ecs_task_execution_role.arn
There is no managed resource "aws_iam_role" "ecs_task_execution_role"
definition in the root module.
Validation stops at undeclared references, and the gaps went further than those two. There was no listener on the load balancer, no security group for the tasks, no CloudWatch log group for the awslogs driver and, most importantly, no aws_ecs_service. A task definition alone runs nothing on AWS Fargate. The old deployment step aws ecs update-service --service flask-service referred to a service the code never created.
A complete AWS Fargate stack that plans cleanly
The fixed version (full file in the lab folder) adds those resources. The parts that matter for AWS Fargate:
resource "aws_iam_role_policy_attachment" "exec" {
role = aws_iam_role.ecs_task_execution_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
}
resource "aws_cloudwatch_log_group" "app" {
name = "/ecs/flask-app"
retention_in_days = 14
}
resource "aws_ecs_service" "app" {
name = "flask-service"
cluster = aws_ecs_cluster.main.id
task_definition = aws_ecs_task_definition.app.arn
desired_count = 2
launch_type = "FARGATE"
network_configuration {
subnets = module.vpc.private_subnets
security_groups = [aws_security_group.task_sg.id]
}
load_balancer {
target_group_arn = aws_lb_target_group.app.arn
container_name = "flask-container"
container_port = 5000
}
depends_on = [aws_lb_listener.http]
}
The task security group accepts port 5000 only from the load balancer's group, and the tasks run in private subnets that reach ECR through the module's single NAT gateway. The execution role uses the AWS managed policy that the ECS task execution role guide names, AmazonECSTaskExecutionRolePolicy. I also pinned the VPC module (version = "~> 6.0"; init downloaded 6.8.0) and the AWS provider (v6.73.0 installed). Result:
[v2]$ tofu validate
Success! The configuration is valid.
Plan: 35 to add, 0 to change, 0 to destroy.
Nineteen of the 35 resources come from the VPC module, including a NAT gateway and an Elastic IP, which cost money while they exist. The plan used fake keys, so it cannot catch account limits or names already in use; run it with real read-only credentials before you apply.
The image tag: latest and IMMUTABLE do not mix
Image tags are where AWS Fargate deployments most often break on the second release. The old ECR repository set image_tag_mutability = "IMMUTABLE", and the deployment pushed :latest every time. The ECR tag mutability page is clear about the result: "Amazon ECR returns an ImageTagAlreadyExistsException if you attempt to push an image with an existing tag." So the second deploy fails at docker push.
Keep the repository immutable, which is the safer choice, and push a unique tag per release (a version or the Git commit). The fixed task definition takes it from a variable, image = "${aws_ecr_repository.app_repo.repository_url}:${var.image_tag}", and the plan above ran with -var image_tag=1.0.0. A new tag changes the task definition, so tofu apply updates the service instead of relying on --force-new-deployment.
AWS Fargate deployment checklist
- Run
docker build --checkand fix warnings before CI does. - Build all wheels in the builder and install offline in the final stage, or use one stage.
- Run
tofu validateon any infrastructure snippet before trusting it; a tutorial's partial code will not apply. - Make sure the stack has a service, a listener, task security groups, a log group and the execution role.
- Use unique image tags with an immutable ECR repository.
- Plan with real read-only credentials and budget for the NAT gateway.
Before your first AWS Fargate apply, read the plan line by line. For the container side of a Python service in more depth, see deploying a machine learning model with Docker. For Terraform-style infrastructure on AWS tested against a mock, see deploy and manage AWS resources using Terraform.
Comments
Post a Comment