Search code examples
amazon-web-servicesdockerflaskamazon-ecs

Deploying flask application on AWS ECS


I created a sample flask application by following Corey Schafer videos available on youtube. I am able to run in local environment and its working fine. I want to deploy it in AWS ECS cluster with fargate option so that I don't have to manage EC2 instances. I was able to build a sample cluster and service using terraform. Below is the code(Application load balancer part is missing)

provider "aws" {
  region = "us-east-2"
}


resource "aws_ecs_cluster" "sample_cluster" {
  name = "sample_cluster"

  setting {
    name  = "containerInsights"
    value = "enabled"
  }
}

data "aws_iam_role" "ecs_task_execution_role" {
  name = "ecsTaskExecutionRole"
}

resource "aws_ecs_task_definition" "sample_cluster_task_definition" {
  container_definitions = file("templates/container_definition.json")
  family                = "sample_terraform_task_definition"
  cpu                   = 256
  memory                = 512
  execution_role_arn    = data.aws_iam_role.ecs_task_execution_role.arn
  network_mode          = "awsvpc"
  runtime_platform {
    operating_system_family = "LINUX"
    cpu_architecture        = "X86_64"
  }
  requires_compatibilities = ["FARGATE"]
}

data "aws_subnets" "private" {
  filter {
    name   = "vpc-id"
    values = ["vpc-ac6d734yrbfjd7ebc7"]
  }

}




resource "aws_ecs_service" "sample_service" {
  name            = "sample_ecs_service"
  cluster         = aws_ecs_cluster.sample_cluster.arn
  task_definition = aws_ecs_task_definition.sample_cluster_task_definition.arn
  launch_type     = "FARGATE"
  desired_count = 1
  network_configuration {
    subnets          = toset(data.aws_subnets.private.ids)
    security_groups = ["sg-0044394c6e4b485738762f7"]
    assign_public_ip = "true"
  }
  depends_on = [aws_ecs_task_definition.sample_cluster_task_definition,data.aws_subnets.private ,aws_ecs_cluster.sample_cluster]
}

I am struggling with how(if required) to configure a web server in AWS since local environment has a dev server which is not suitable for production environment. So my question is

  1. For a webserver e.g. nginx will it be separate docker container or does AWS has any managed service which can be utilized as a web server. For instance I am using AWS RDS instead of a separate container for DB.
  2. If a separate docker container is required will it be part of same task definition or different. Which one is recommended.

I just want to give deployment more like a actual production setup so in case if anyone has deployed flask application in aws ecs in production could you please help me out. Thank you


Solution

  • Since you are asking about "best practice" then it is generally considered best practice to run Nginx in front of Flask. To accomplish this on ECS/Fargate you would run two containers in the same ECS task (define them both in the same task definition). You would configure Nginx to proxy to Flask at 127.0.0.1:5000 (or whatever port you have Flask using), since multiple containers in the same task share 127.0.0.1 in Fargate.