skip to Main Content

appreciate your support in solving this issue i have main.tf file like below

resource "aws_ecs_service" "nodejs-service" {
  name            = "nodejs-service"
  cluster         = aws_ecs_cluster.project_cluster.id
  task_definition = aws_ecs_task_definition.nodejs.arn
  launch_type     = "FARGATE"
  desired_count   = 1

  load_balancer {
    target_group_arns = module.alb.target_group_arns
    container_name   = "${aws_ecs_task_definition.nodejs.family}"
    container_port   = 8080 # Specifying the container port
  }

  network_configuration {
    subnets          = var.vpc.public_subnets
    assign_public_ip = true
  }
}

module "alb" {
  source             = "terraform-aws-modules/alb/aws"
  version            = "~> 8.0"
  name               = var.namespace
  load_balancer_type = "application"
  vpc_id             = var.vpc.vpc_id
  subnets            = var.vpc.public_subnets
  security_groups    = [var.sg.lb]

  http_tcp_listeners = [
    {
      port               = 80
      protocol           = "HTTP"
      target_group_index = 0
    }
  ]

  target_groups = [
    { name_prefix      = "nodejs-service"
      backend_protocol = "HTTP"
      backend_port     = 8080
      target_type      = "instance"
    }
  ]
}

i receive error

│ Error: Unsupported argument
│
│   on modules/ecs/main.tf line 58, in resource "aws_ecs_service" "nodejs-service":
│   58:     target_group_arns = module.alb.target_group_arns
│
│ An argument named "target_group_arns" is not expected here. Did you mean "target_group_arn"?

even if i changed target_groups on the service parameters to be target_group_arn i receive error "target_group_arn" is not defined
also with module.alb.target_groups[0] the same error appear with terraform plan

  load_balancer {
    target_group_arn   = module.alb.target_groups[0]
    container_name   = "${aws_ecs_task_definition.nodejs.family}"
    container_port   = 8080 # Specifying the container port
  }

Error:

│ Error: Unsupported attribute
│
│   on modules/ecs/main.tf line 58, in resource "aws_ecs_service" "nodejs-service":
   58:     target_group_arn   = module.alb.target_groups[0]
     ├────────────────
     │ module.alb is a object

 This object does not have an attribute named "target_groups".

as per main.tf file how can i select the target group which is defined in alb module

Thanks,

tried: terraform plan and expected alb with target group pointing on nodejs-service container

terraform {
  required_version = ">= 1.0"

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = ">= 4.27"
    }
    null = {
      source  = "hashicorp/null"
      version = ">= 2.0"
    }
  }
}

2

Answers


  1. output from the module alb is an array.

    In your case it would be module.alb.target_group_arns[0]

    Replace it with this code

    load_balancer {
        target_group_arns = module.alb.target_group_arns[0]
        container_name   = "${aws_ecs_task_definition.nodejs.family}"
        container_port   = 8080 # Specifying the container port
      }
    
    Login or Signup to reply.
  2. The issue is not in the module, rather in the argument you are trying to use in the aws_ecs_service resource. You are currently setting it to target_group_arns while the argument is singular, i.e., target_group_arn [1]:

      load_balancer {
        target_group_arn = module.alb.target_group_arns[0]
        container_name   = "${aws_ecs_task_definition.nodejs.family}"
        container_port   = 8080 # Specifying the container port
      }
    

    The example is with the first of the target groups returned from the module, so make sure you are using the correct one.


    [1] https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_service#target_group_arn

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search