terraform-aws-alb icon indicating copy to clipboard operation
terraform-aws-alb copied to clipboard

Conditionally setting attributes

Open evanstoddard23 opened this issue 1 year ago β€’ 6 comments

Is your request related to a problem? Please describe.

The try() function is used extensively throughout this module to evaluate if a key is set. However I find myself in many cases wanting to conditionally set these keys. When setting a key to null, it does not result in an error, so the for_each ends up trying to create a block it shouldn't, resulting in an error.

Here's an example:

module "alb" {
  source  = "terraform-aws-modules/alb/aws"
  version = "9.11.0"
  for_each = {...}
  listeners = {
    https = {
      forward = each.value.some_condition ? null : {
        target_group_key = "default"
      }

      fixed_response = each.value.some_condition ? {
        content_type = "text/plain"
        message_body = "Access denied"
        status_code  = "403"
      } : null
...

Results in errors like:

β”‚ Error: Attempt to get attribute from null value
β”‚ 
β”‚   on .terraform/modules/frontends.alb/main.tf line 141, in resource "aws_lb_listener" "this":
β”‚  141:         content_type = default_action.value.content_type
β”‚     β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚     β”‚ default_action.value is null
β”‚ 
β”‚ This value is null, so it does not have any attributes.

Describe the solution you'd like.

Maybe these blocks should look something like this to allow for null values?

dynamic "default_action" {
  for_each = try([each.value.fixed_response != null ? each.value.fixed_response : (1 / 0)], [])
...

Describe alternatives you've considered.

I haven't been able to think of other ways to handle this gracefully.

evanstoddard23 avatar Aug 28 '24 21:08 evanstoddard23

However I find myself in many cases wanting to conditionally set these keys.

Why? This seems like you are trying to re-wrap this module so you'd have to handle that differently

bryantbiggs avatar Aug 28 '24 22:08 bryantbiggs

@bryantbiggs In my case I'm using a for_each loop to create albs alongside other associated infrastructure such as cloudfront, waf, and route 53 for similar services. v8.7.0 worked well like this:

...
https_listeners = [{
  ...
  fixed_response = each.value.some_condition ? {
    content_type = "text/plain"
    message_body = "Access denied"
    status_code  = 403
  } : {}

I was able to get around this in v9.x by doing a higher level map merge, but I think it makes the code a bit messier and it seems (maybe just to me) that null is a valid value that should be able to be passed in.

...
https = merge(each.value.some_condition ? {
  fixed_response = {
    content_type = "text/plain"
    message_body = "Access denied"
    status_code  = "403"
  }
  } :
  {
    forward = {
      target_group_key = "default"
    }
  },
  {
    port            = 443
    ...

evanstoddard23 avatar Aug 29 '24 19:08 evanstoddard23

this feels like an x-y type problem so I'm not sure how much guidance I can provide given the narrow context. but in general, 99.9% of the time we've received issues like this - the answer has always been - "you should adjust how you are doing that"

bryantbiggs avatar Aug 30 '24 13:08 bryantbiggs

I was able to more or less get around it for what I’m doing, although it’s not pretty. It does seem to me like a valid part of HCL that’s not being properly handled by this module though given this in the docs β€œnullΒ is most useful in conditional expressions, so you can dynamically omit an argument if a condition isn't met.”

evanstoddard23 avatar Aug 30 '24 18:08 evanstoddard23

This issue has been automatically marked as stale because it has been open 30 days with no activity. Remove stale label or comment or this issue will be closed in 10 days

github-actions[bot] avatar Sep 30 '24 00:09 github-actions[bot]

Any solution for this?

AkshayB-ops avatar Oct 03 '24 18:10 AkshayB-ops

This issue has been automatically marked as stale because it has been open 30 days with no activity. Remove stale label or comment or this issue will be closed in 10 days

github-actions[bot] avatar Nov 03 '24 00:11 github-actions[bot]

This issue was automatically closed because of stale in 10 days

github-actions[bot] avatar Nov 14 '24 00:11 github-actions[bot]

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

github-actions[bot] avatar Dec 14 '24 02:12 github-actions[bot]