Skip to content

ECS Submodule container-definition with port_mapping #122

@alexiskat

Description

@alexiskat

Description

I am using the ecs service submodule to create the service. I am also using the container-definition submodule to generate the definition for the service. I am trying to assign the container definition to the service which works up to a point

the following is the service sub-module config:

module "ecs_tr_service" {
  source  = "terraform-aws-modules/ecs/aws//modules/service"
  version = "5.2.2"

  name        = "terrareg-serv"
  cluster_arn = var.ecs_cluster_arn

  cpu    = 512
  memory = 1024

  launch_type  = "FARGATE"
  network_mode = "awsvpc"

  desired_count            = 1
  enable_autoscaling       = true
  autoscaling_min_capacity = 1
  autoscaling_max_capacity = 1

  # Container definition(s)
  container_definitions = {
    terrareg = module.ecs_container_tr_def.container_definition
  }

  load_balancer = {
    service = {
      target_group_arn = aws_lb_target_group.this.arn
      container_name   = "terrareg"
      container_port   = var.tr_container_port
    }
  }

  subnet_ids = var.vpc_pri_sub_ids
  security_group_rules = {
    alb_ingress_5000 = {
      type                     = "ingress"
      from_port                = var.tr_container_port
      to_port                  = var.tr_container_port
      protocol                 = "tcp"
      description              = "Service port"
      source_security_group_id = var.alb_https_sg_id
    }
    egress_all = {
      type        = "egress"
      from_port   = 0
      to_port     = 0
      protocol    = "-1"
      cidr_blocks = ["0.0.0.0/0"]
    }
  }

  tags = var.tags
}

The following is the container-definition using the submodule

module "ecs_container_tr_def" {
  source  = "terraform-aws-modules/ecs/aws//modules/container-definition"
  version = "5.2.2"

  name      = "terrareg"
  cpu       = 256
  memory    = 512
  essential = true
  image     = "1234567890.dkr.ecr.eu-west-2.amazonaws.com/terrareg:latest"
  port_mappings = [
    {
      containerPort = 5000
      hostPort      = 5000
      protocol      = "tcp"
    }
  ]
  # Requires access to write to root filesystem
  readonly_root_filesystem = false
  tags = var.tags
}

When I do a terraform run I notice that portMappings is empty even though above port 5000 etc have been defined

#terraform run 
.........
# module.ecs_tr_service.aws_ecs_task_definition.this[0] will be created
  + resource "aws_ecs_task_definition" "this" {
      + arn                      = (known after apply)
      + arn_without_revision     = (known after apply)
      + container_definitions    = jsonencode(
            [
              + {
                  + cpu                    = 256
                  + environment            = []
                  + essential              = true
                  + image                  = "12345678.dkr.ecr.eu-west-2.amazonaws.com/terrareg:latest"
                  + interactive            = false
                  + logConfiguration       = {
                      + logDriver = "awslogs"
                      + options   = {
                          + awslogs-group         = "/aws/ecs/terrareg-serv/terrareg"
                          + awslogs-region        = "eu-west-2"
                          + awslogs-stream-prefix = "ecs"
                        }
                    }
                  + memory                 = 512
                  + mountPoints            = []
                  + name                   = "terrareg"
                  + portMappings           = []            <---- this should NOT be an empty list
                  + privileged             = false
                  + pseudoTerminal         = false
                  + readonlyRootFilesystem = true

When I do apply the following error which I believe is because portMappings is empty

module.ecs_tr_service.aws_ecs_service.this[0]: Creating...
╷
│ Error: creating ECS Service (terrareg-serv): InvalidParameterException: The container terrareg did not have a container port 5000 defined.
│ 
│   with module.ecs_tr_service.aws_ecs_service.this[0],
│   on .terraform/modules/ecs_tr_service/modules/service/main.tf line 30, in resource "aws_ecs_service" "this":
│   30: resource "aws_ecs_service" "this" {

The output of the variable module.ecs_container_tr_def.container_definition looks correct:

terragrunt console                                                                                            
> module.ecs_container_tr_def.container_definition
{
  "cpu" = 256
  "environment" = tolist([])
  "essential" = true
  "image" = "123456789.dkr.ecr.eu-west-2.amazonaws.com/test:latest"
  "interactive" = false
  "logConfiguration" = {
    "logDriver" = "awslogs"
    "options" = {
      "awslogs-group" = "/aws/ecs//test"
      "awslogs-region" = "eu-west-2"
      "awslogs-stream-prefix" = "ecs"
    }
  }
  "memory" = 512
  "mountPoints" = tolist([])
  "name" = "terrareg"
  "portMappings" = tolist([
    {
      "containerPort" = 5000
      "hostPort" = 5000
      "protocol" = "tcp"
    },
  ])
  "privileged" = false
  "pseudoTerminal" = false
  "readonlyRootFilesystem" = false
  "startTimeout" = 30
  "stopTimeout" = 120
  "volumesFrom" = tolist([])
} 

Versions

  • Terraform version:
    Terraform v1.5.3
    on darwin_amd64

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions