terraform-aws-modules / terraform-aws-alb

Terraform module to create AWS Application/Network Load Balancer (ALB/NLB) resources πŸ‡ΊπŸ‡¦
https://registry.terraform.io/modules/terraform-aws-modules/alb/aws
Apache License 2.0
425 stars 661 forks source link

[bug] - waiting `target_failover` for other load_balancer than `gateway` type #373

Open remi-f-artelia opened 4 days ago

remi-f-artelia commented 4 days ago

Description

Please provide a clear and concise description of the issue you are encountering, and a reproduction of your configuration (see the examples/* directory for references that you can copy+paste and tailor to match your configs if you are unable to copy your exact configuration). The reproduction MUST be executable by running terraform init && terraform apply without any further changes.

If your request is for a new feature, please use the Feature request template.

⚠️ Note

Before you submit an issue, please perform the following first:

  1. Remove the local .terraform directory (! ONLY if state is stored remotely, which hopefully you are following that best practice!): rm -rf .terraform/
  2. Re-initialize the project root to pull down modules: terraform init
  3. Re-attempt your terraform plan or apply and check if the issue still persists

Versions

tf -version
Terraform v1.8.5

Successfully configured the backend "s3"! Terraform will automatically use this backend unless the backend configuration changes. Initializing modules...

Initializing provider plugins...

Reproduction Code [Required]

The code is available in the following gist

Steps to reproduce the behavior:

Are you using workspaces? No Have you cleared the local cache (see Notice section above)?: Yes List steps in order that led up to the issue you encountered

terraform init
terraform plan -var-file terraform-tfvars

Expected behavior

The plan should work without errors because:

Actual behavior

The plan fails with the following errors:

1st error

β”‚ Error: Attempt to get attribute from null value
β”‚
β”‚   on .terraform\modules\service_alb\main.tf line 530, in resource "aws_lb_target_group" "this":
β”‚  530:       type            = var.load_balancer_type == "network" ? "source_ip" : stickiness.value.type
β”‚     β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚     β”‚ stickiness.value is null
β”‚
β”‚ This value is null, so it does not have any attributes.

I'm not sure why this is failing right now. The only thing I know is that with the provided example it fails so we do not have a proper example to rely on.

If I override the code as below, my plan pass this issue but fails on another one described in 3rd error block

# .terraform\modules\service_alb\main.tf line 523
dynamic "stickiness" {
    for_each = var.load_balancer_type == "network" ? try([each.value.stickiness], []) : []

    content {
      cookie_duration = try(stickiness.value.cookie_duration, null)
      cookie_name     = try(stickiness.value.cookie_name, null)
      enabled         = try(stickiness.value.enabled, true)
      type            = var.load_balancer_type == "network" ? "source_ip" : stickiness.value.type
    }
  }

2nd error

β•·
β”‚ Error: Invalid dynamic for_each value
β”‚
β”‚   on .terraform\modules\service_alb\main.tf line 535, in resource "aws_lb_target_group" "this":
β”‚  535:     for_each = try(each.value.target_failover, [])
β”‚     β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚     β”‚ each.value.target_failover is null
β”‚
β”‚ Cannot use a null value in for_each.

IMHO, the foreach should condition the var.load_balancer_type as gateway since it's written this in the doc

This block is only applicable for a Gateway Load Balancer (GWLB). The two attributes on_deregistration and on_unhealthy cannot be set independently. The value you set for both attributes must be the same.

# .terraform\modules\service_alb\main.tf line 534
dynamic "target_failover" {
    for_each = var.load_balancer_type == "gateway" ? try(each.value.target_failover, []) : []

    content {
      on_deregistration = target_failover.value.on_deregistration
      on_unhealthy      = target_failover.value.on_unhealthy
    }
  }

3rd issue

When I override the .terraform\modules\service_alb\main.tf block code as described into 1st error , I fail on the following issue, even if I hard code the vpc_id

β”‚ Error: Invalid Attribute Combination
β”‚ 
β”‚ Attribute "vpc_id" must be specified when "target_type" is "instance".
β”‚ target_type
β”‚
β”‚   with module.service_alb.aws_lb_target_group.this["ex-instance"],
β”‚   on .terraform\modules\service_alb\main.tf line 487, in resource "aws_lb_target_group" "this":
β”‚  487: resource "aws_lb_target_group" "this" {
β”‚

Terminal Output Screenshot(s)

Additional context

remi-f-artelia commented 4 days ago

OK so my listeners and target_groups variables are not perfect yet to modelize each possible options matching the module... Moreover, there were based on a previous version of the module so it's failing later due to this...

If you want to reproduce, replace them with the default variables as in the module

variable "listeners" {
  description = "Map of listener configurations to create"
  type        = any
  default     = {}
}

variable "target_groups" {
  description = "Map of target group configurations to create"
  type        = any
  default     = {}
}

But then it's failing with another issue:

β”‚ Error: Error in function call
β”‚
β”‚   on .terraform\modules\service_alb\main.tf line 346, in resource "aws_lb_listener_rule" "this":
β”‚  346:             arn    = try(target_group.value.arn, aws_lb_target_group.this[target_group.value.target_group_key].arn)
β”‚     β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚     β”‚ aws_lb_target_group.this is object with 1 attribute "ex-instance"
β”‚     β”‚ target_group.value is object with 2 attributes
β”‚     β”‚ target_group.value.target_group_key is "ex-lambda-with-trigger"
β”‚
β”‚ Call to function "try" failed: no expression succeeded:
β”‚ - Unsupported attribute (at .terraform\modules\service_alb\main.tf:346,44-48)
β”‚   This object does not have an attribute named "arn".
β”‚ - Invalid index (at .terraform\modules\service_alb\main.tf:346,74-111)
β”‚   The given key does not identify an element in this collection value.
β”‚
β”‚ At least one expression must produce a successful result.

I don't really understand why the example is failing and why I can't find any other issue showing this