Home

Deploying to ECS Fargate with ALB

The dockerized app i created was deployed to fargate. Initially, i didn't understand fargate very much. Thus i had a very hard time with it. After a lot of trial and error, i feel like i understand some of it. Fargate should be used as a stateless autoscaling server. Each task is that -- a task. So you'd use a different frontend and backend task. To communicate between the two, or outside world, you have to setup a load balancer. All communication should happen through the load balancer.

The fargate config i used is for frontend. For backend, i used EC2 service with configured postgres. The backend's security group only takes connection from specific security group, thus i needed to make farget infra host on the same SG.

{
  "family": "flask-task",
  "networkMode": "awsvpc",
  "executionRoleArn": "arn:aws:iam::078805859964:role/ecs",
  "containerDefinitions": [
    {
      "name": "flask-fe",
      "image": "078805859964.dkr.ecr.ap-south-1.amazonaws.com/flask-fe:latest",
      "portMappings": [
        {
          "containerPort": 5000,
          "protocol": "tcp"
        }
      ],
      "essential": true,
      "environment": [
        { "name": "POSTGRES_HOST", "value": "10.0.2.6" },
        { "name": "POSTGRES_PASSWORD", "value": "ubuntu" },
        { "name": "POSTGRES_PORT", "value": "5432" }
      ]
    }
  ],
  "requiresCompatibilities": [ "FARGATE" ],
  "cpu": "256",
  "memory": "512"
}

This article really helped me understand and use fargate: https://github.com/atulkamble/ecs-webapp-deployment