terraform-aws-ecs-alb-service-task icon indicating copy to clipboard operation
terraform-aws-ecs-alb-service-task copied to clipboard

ecs_load_balancers optional attributes

Open HebertCL opened this issue 2 years ago • 4 comments

Describe the Bug

I am trying to provision an ECS service + ECS task using an ALB. To configure the options for load balancer, I am using this configuration:

locals {
  alb = [{
    container_name   = coalesce(var.alb_container_name, module.this.id)
    container_port   = var.container_port
    elb_name         = null
    target_group_arn = var.target_group_arn
  }]
}

Such local configuration goes directly to the module input like so:

module "ecs_alb_service_task" {
  source  = "cloudposse/ecs-alb-service-task/aws"
  version = "0.64.1"

  ...
  ecs_load_balancers                 = local.alb
...

  context = module.this.context
}

Both validation and plan runs as expected, but during the apply operation a TF error is returned:

module.ecs_alb_service_task.aws_ecs_service.ignore_changes_task_definition[0]: Creating...
╷
│ Error: error creating ECS service (mcoins-sandbox-analytics-denormalizer): InvalidParameterException: Load Balancer Name can not be blank.
│
│   with module.ecs_alb_service_task.aws_ecs_service.ignore_changes_task_definition[0],
│   on .terraform/modules/ecs_alb_service_task/main.tf line 351, in resource "aws_ecs_service" "ignore_changes_task_definition":
│  351: resource "aws_ecs_service" "ignore_changes_task_definition" {
│
╵

Which means I'm sending an empty string to AWS API and causing the issue. This led me to try getting rid of the elb_name value, and getting this other error:

│ Error: Invalid value for input variable
│
│   on main.tf line 110, in module "ecs_alb_service_task":
│  110:   ecs_load_balancers                 = local.alb
│
│ The given value is not suitable for
│ module.ecs_alb_service_task.var.ecs_load_balancers declared at
│ .terraform/modules/ecs_alb_service_task/variables.tf:11,1-30: element 0:
│ attribute "elb_name" is required.

I've looked into AWS API documentation and Terraform Provider and make sure that only container_port and container_name are required arguments so you can choose between classic ELB or ALB/NLB.

I suggest the option here is to add the optional keyword to both elb_name and target_group_arn arguments, so whenever values are supplied here it's not mandatory to supply both. I can also supply a PR with the suggested change if you're ok with it.

Expected Behavior

Terraform should be able to apply with either Classic or ALB/NLB values, but not require both.

Steps to Reproduce

Steps to reproduce the behavior:

  1. Run an example that supplies ecs_load_balancers value with either classic ELB name or ALB target group ARN.
  2. Run terraform init && terraform apply

Screenshots

Code snippets and errors above

Environment (please complete the following information):

  • OS: OSX, arm64
  • Version: Monterrey 12.4
  • Terraform version:
Terraform v1.2.4
on darwin_arm64
+ provider registry.terraform.io/hashicorp/aws v4.22.0
+ provider registry.terraform.io/hashicorp/local v2.2.3
+ provider registry.terraform.io/hashicorp/null v3.1.1

Additional Context

None.

HebertCL avatar Jul 19 '22 14:07 HebertCL

@HebertCL were you able to resolve this ?

kunalsawhney avatar Oct 17 '22 06:10 kunalsawhney

Hi @kunalsawhney, since my use case was very specific and not likely to use classic LB I decided to fork on this repo and simplify the configuration to only expect ALB connectivity. I know this is not flexible nor the most ideal solution, but it was enough for me to move forward. I think for now this issue can be closed.

HebertCL avatar Oct 17 '22 13:10 HebertCL

I was able to use this successfully by setting elb_name = null

prelegalwonder avatar Aug 25 '23 15:08 prelegalwonder

confirming elb_name: null works!

jeshicawang avatar Sep 27 '23 03:09 jeshicawang