Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Attempt to get attribute from null value" error #390

Open
1 task
fatmcgav opened this issue Dec 3, 2024 · 3 comments
Open
1 task

"Attempt to get attribute from null value" error #390

fatmcgav opened this issue Dec 3, 2024 · 3 comments

Comments

@fatmcgav
Copy link

fatmcgav commented Dec 3, 2024

Description

I am attempting to create a wrapper module around this alb module, and as part of this am defining a listeners var which includes a number of object definitions:

variable "listeners" {
  description = "Listeners to attach to ALB. A http-https-redirect-listener will be created automatically if 'enable_http_https_redirect = true'"
  default     = {}

  type = map(object({
    port     = number
    protocol = optional(string, "HTTP")

    # Default behaviours
    fixed_response = optional(object({
      content_type = optional(string, "text/plain")
      message_body = optional(string)
      status_code  = optional(string)
    }))

    forward = optional(object({
      target_groups = list(object({
        name   = optional(string, null)
        weight = optional(number)
      }))
      stickiness = object({
        duration = optional(number)
        enabled  = optional(bool, false)
      })
    }))

    redirect = optional(object({
      host        = optional(string, "#{host}")
      path        = optional(string, "#{path}")
      port        = optional(string, "#{port}")
      protocol    = optional(string, "#{protocol}")
      query       = optional(string, "#{query}")
      status_code = optional(string, "HTTP_301")
    }))

    # Listener rules
    rules = map(object({
      type = optional(string)
      fixed_response = optional(object({
        content_type = optional(string)
        message_body = optional(string)
        status_code  = optional(string)
      }))

      forward = optional(object({
        target_groups = list(object({
          name   = string
          weight = optional(number)
        }))
        stickiness = object({
          duration = number
          enabled  = optional(bool, false)
        })
      }))

      redirect = optional(object({
        host        = optional(string, "#{host}")
        path        = optional(string, "#{path}")
        port        = optional(string, "#{port}")
        protocol    = optional(string, "#{protocol}")
        query       = optional(string, "#{query}")
        status_code = optional(string, "HTTP_301")
      }))
    }))
  }))

  validation {
    condition = alltrue([
      for listener in var.listeners : contains([80, 443], listener.port)
    ])
    error_message = "listener 'port' value should be '80' or '443'"
  }

  validation {
    condition = alltrue([
      for listener in var.listeners : contains(["HTTP", "HTTPS"], listener.protocol)
    ])
    error_message = "listener 'protocol' value should be 'HTTP' or 'HTTPS'"
  }
}

However the plan is failing with:

╷
│ Error: Attempt to get attribute from null value
│
│   on .terraform/modules/alb/main.tf line 203, in resource "aws_lb_listener" "this":
│  203:         status_code = default_action.value.status_code
│     ├────────────────
│     │ default_action.value is null
│
│ This value is null, so it does not have any attributes.
  • ✋ I have searched the open/closed issues and my issue is not listed.

⚠️ 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

  • Module version [Required]: latest

  • Terraform version: 1.5.7

  • Provider version(s): 5.79.0

Reproduction Code [Required]

https://gist.github.com/fatmcgav/82606b5ceef39b92d08398a0b08ec0a4

Steps to reproduce the behavior:

Expected behavior

ALB should be created

Actual behavior

Terraform plan failed

Terminal Output Screenshot(s)

Additional context

Changes in #389 fix the issue

@bryantbiggs
Copy link
Member

Cross posting from linked PR

You should change your variables in your wrapper to make it work with the existing implementation. I would start with not using null and instead default to an empty list.

@rruenroengYahara
Copy link

rruenroengYahara commented Dec 16, 2024

Hi @bryantbiggs,
I'm not totally following your suggestion for @fatmcgav . I am running into the same error when trying to setup this module. I have the following implemented as a first pass at getting things set up.

listeners = {
    http-main-example = {
      port     = 80
      protocol = "HTTP"
      forward = {
        target_group_key = "tg-default"
      }
      rules = {
        auth-rule = {
          priority = 1
          actions = [{
            type             = "forward"
            target_group_key = "auth-tg"
          }]
          conditions = [{
            path_pattern = {
              values = ["/auth/*"]
            }
          }]
        }
        secrets-rule = {
          priority = 2
          actions = [{
            type             = "forward"
            target_group_key = "secrets-tg"
          }]
          conditions = [{
            path_pattern = {
              values = ["/secrets/*"]
            }
          }]
        }
      }
    }
  }

What are you suggesting I provide to the module to get it to work?

@bryantbiggs
Copy link
Member

this variable for listeners https://gist.github.com/fatmcgav/82606b5ceef39b92d08398a0b08ec0a4#file-variables-tf-L1-L80

does not match our variable for listeners

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

If you are running into an issue, I would suggest opening a separate issue with steps to reproduce and any error messages/details

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants