Skip to content

ECS

In this section you will create the Elastic Container Service to run your application.

ECS Cluster

First we need to create the cluster

  1. Create a resource block with type aws_ecs_cluster named this with following parameters:
    1. name = var.service_name
    2. capacity_providers = ["FARGATE"]

ECS Task Definition

ECS Task Definition is a collection of containers that run in a service. It has all the container configuration parameters like CPU, Memory, Networking etc.

  1. Create a resource block with type aws_ecs_task_definition named this with following parameters:
    1. family = var.service_name
    2. required_compatibilities = ["FARGATE"]
    3. network_mode = "awsvpc"
    4. cpu = "256"
    5. memory = "512"
    6. execution_role_arn = aws_iam_role.this.arn
    7. Container Definition:
        container_definitions = jsonencode([
          {
            name      = "frontend"
            image     = "<your application image path>"
            cpu       = 10
            memory    = 512
            essential = true
            portMappings = [
              {
                containerPort = 80
                hostPort      = 80
              }
            ]
          }
        ])
      

Help

terraform/ecs.tf

resource "aws_ecs_task_definition" "this" {
  family = var.service_name
  requires_compatibilities = ["FARGATE"]
  network_mode = "awsvpc"
  cpu = "256"
  memory = "512"
  execution_role_arn = aws_iam_role.this.arn
  container_definitions = jsonencode([
    {
      name      = "frontend"
      image     = "nginxdemos/hello"
      cpu       = 10
      memory    = 512
      essential = true
      portMappings = [
        {
          containerPort = 80
          hostPort      = 80
        }
      ]
    }
  ])
}

ECS Service

ECS Service is what bundles everything together in ECS.

  1. Create a resource block with type aws_ecs_service named this with following parameters
    1. name = var.service_name
    2. cluster = aws_ecs_cluster.this.id
    3. task_definition = aws_ecs_task_definition.this.id
    4. desired_count = 1
    5. launch_type = "FARGATE"
    6. Network Configuration
        network_configuration {
          subnets         = data.aws_subnet_ids.private.ids
          security_groups = [aws_security_group.ecs_tasks.id]
        }
      
    7. Load Balancer:
        load_balancer {
          target_group_arn = aws_alb_target_group.frontend.arn
          container_name   = "frontend"
          container_port   = 80
        }
      
    8. Depends On:
        depends_on = [
          aws_alb_listener.frontend,
        ]
      

Output

For us to know where we can check our deployment we need to tell Terraform to output the ALB DNS address

  1. Create a output block named alb_address with the following parameters
    1. value = aws_alb.this.dns_name

Push everything to production

Now you are ready to apply everything to ECS.

terraform apply

It will show you the resources that Terraform will create,delete or change.

Check that every resource looks OK, and write yes to apply the changes.


Answer

terraform/ecs.tf

resource "aws_ecs_cluster" "this" {
  name = var.service_name
  capacity_providers = ["FARGATE"]
}


resource "aws_ecs_task_definition" "this" {
  family = var.service_name
  requires_compatibilities = ["FARGATE"]
  network_mode = "awsvpc"
  cpu = "256"
  memory = "512"
  execution_role_arn = aws_iam_role.this.arn
  container_definitions = jsonencode([
    {
      name      = "frontend"
      image     = "<your application image path>"
      cpu       = 10
      memory    = 512
      essential = true
      portMappings = [
        {
          containerPort = 80
          hostPort      = 80
        }
      ]
    }
  ])
}

resource "aws_ecs_service" "this" {
  name            = var.service_name
  cluster         = aws_ecs_cluster.this.id
  task_definition = aws_ecs_task_definition.this.id
  desired_count   = 1
  launch_type = "FARGATE"
  network_configuration {
    subnets         = data.aws_subnet_ids.private.ids
    security_groups = [aws_security_group.ecs_tasks.id]
  }
  load_balancer {
    target_group_arn = aws_alb_target_group.frontend.arn
    container_name   = "frontend"
    container_port   = 80
  }

  depends_on = [
    aws_alb_listener.frontend,
  ]

}

output "alb_address" {
  value = aws_alb.this.dns_name
}