0

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

  • 1
    Does this help? https://www.toptal.com/flask/flask-production-recipes – Nick.Mc Apr 13 '22 at 03:11
  • Not sure what do you expect to find for "AWS has any managed service which can be utilized as a web server"? Yes, aws has elastic beanstalk. – Marcin Apr 13 '22 at 03:30
  • @Marcin they are specifically asking about a reverse proxy HTTP server similar to Nginx. Amazon's analog to that would probably be an Application Load Balancer. However there are some differences, and it is still useful to run both a load balancer and an Nginx service, just like Elastic Beanstalk does. – Mark B Apr 13 '22 at 14:16
  • @Marcin - Thanks for the response. I just wanted to check the best practice to deploy flask application on AWS ECS. Whether a new task definition is required for nginx or in same task definition. Like these kind of best practice. Thank you – Deepak_Spark_Beginner Apr 13 '22 at 14:17

1 Answers1

0

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.

Mark B
  • 183,023
  • 24
  • 297
  • 295
  • Thanks so much and there would a WSGI on same container as flask app ? – Deepak_Spark_Beginner Apr 13 '22 at 14:29
  • Yes you would run something like Gunicorn in the Flask app container. That would be the entrypoint for the container. – Mark B Apr 13 '22 at 14:45
  • Thank you so much. This would really help me in deployment – Deepak_Spark_Beginner Apr 13 '22 at 14:46
  • A follow up question coming from https://stackoverflow.com/questions/58586141/does-nginx-becomes-redundant-if-we-have-aws-application-load-balancer-for-a-node . If I use AWS ALB would Nginx still be required ? – Deepak_Spark_Beginner Apr 13 '22 at 15:20
  • @Deepak_Spark_Beginner the answer to that is more of an opinion than a yes/no. The fact that Elastic Beanstalk still uses Nginx in addition to an ALB shows that Amazon doesn't think the ALB makes Nginx redundant. I specifically stated that "best practice" is to use Nginx in addition to the ALB. If you want to evaluate that and determine if you really need Nginx or not, that is up to you. – Mark B Apr 13 '22 at 15:24
  • 1
    Thank you for pointing me to the right direction . This would really help – Deepak_Spark_Beginner Apr 13 '22 at 17:02