0

I'm trying to create an ECS service using Terraform. I have some modules defined to create some necessary resources (like the alb, vpc, subnets, etc). All of those have been created successfully, but the aws_ecs_service is not being created.

This is the Terraform code I'm using:

terraform {
  required_version = ">= 0.13"
}

resource "aws_ecs_task_definition" "main" {
  family                   = "task-definition"
  execution_role_arn       = aws_iam_role.ecs_task_execution_role.arn
  network_mode             = "awsvpc"
  requires_compatibilities = ["FARGATE"]
  cpu                      = var.fargate_cpu
  memory                   = var.fargate_memory

  container_definitions = jsonencode([
    {
      name    = "container-definition"
      image   = var.container_image
      cpu     = var.fargate_cpu
      memory  = var.fargate_memory
      command = ["python3", "manage.py", "runserver", "0.0.0.0:8000"]
      port_mappings = [
        {
          container_port = var.app_port
          host_port      = var.app_port
        }
      ]
      logConfiguration = {
        logDriver = "awslogs"
        options = {
          awslogs-group         = "/ecs/task-definition"
          awslogs-region        = var.aws_region
          awslogs-stream-prefix = "ecs"
        }
      }
    }
  ])
}

module "load_balancer" {
  source = "../alb"

  vpc_id             = var.vpc_id
  app_port           = var.app_port
  public_subnets_ids = var.public_subnets_ids
  health_check_path  = "/"
}

resource "aws_ecs_service" "main" {
  name            = "testing-service"
  cluster         = var.ecs_cluster_id
  task_definition = aws_ecs_task_definition.main.arn
  desired_count   = 1
  launch_type     = "FARGATE"

  network_configuration {
    security_groups  = [module.load_balancer.sg_id]
    subnets          = var.private_subnet_ids
    assign_public_ip = true
  }

  load_balancer {
    target_group_arn = module.load_balancer.alb_tg_arn
    container_name   = "container-definition"
    container_port   = var.app_port
  }

  depends_on = [
    module.load_balancer
  ]
}

I'm fully aware that fragment of code is not enough to reproduce the problem, but I have not been able to make a smaller example reproducing the problem. If you need the rest of the files, I can create a public repo or something like with the rest of the code.

The error I'm getting is:

╷
│ Error: error creating testing-service service: error waiting for ECS service (testing-service) creation: InvalidParameterException: The container container-definition did not have a container port 8000 defined.
│ 
│   with module.service.aws_ecs_service.main,
│   on service/main.tf line 47, in resource "aws_ecs_service" "main":
│   47: resource "aws_ecs_service" "main" 

Update

Taking a look at the generated resources, I have seen that the port mapping has not been generated! Even though I have it specified in the terraform code:

enter image description here

That's a screenshot from the task definition created by that code.

2
  • You hardcoded port 8000 in your python command, and used var.app_port elsewhere. Do they match ?
    – aherve
    Commented Aug 14, 2021 at 8:52
  • Yes, that's something I have to change, but they match Commented Aug 14, 2021 at 8:55

1 Answer 1

2

You have a typo in your container definition. Instead of this:

      port_mappings = [
        {
          container_port = var.app_port
          host_port      = var.app_port
        }
      ]

You should have:

      portMappings = [
        {
          containerPort = var.app_port
          hostPort      = var.app_port
        }
      ]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.