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

Error when using lifecycle rule with null values #308

Open
1 task done
GusAntoniassi opened this issue Jan 24, 2025 · 0 comments · May be fixed by #309
Open
1 task done

Error when using lifecycle rule with null values #308

GusAntoniassi opened this issue Jan 24, 2025 · 0 comments · May be fixed by #309

Comments

@GusAntoniassi
Copy link

GusAntoniassi commented Jan 24, 2025

Description

When trying to use a lifecycle rule while passing null values for transition, noncurrent_version_transition and filter inputs, Terraform throws a "null value has no attributes" error. I believe this is due to how the for_each blocks are implemented:

        for_each = try(flatten([rule.value.expiration]), [])

Since there is no compact, the flatten call will return [null] which is a valid list with one value.

In my case the null values are coming from a variable with optional fields, so it would be best if the module could handle null values correctly.

  • ✋ I have searched the open/closed issues and my issue is not listed.

Versions

  • Module version [Required]: 4.5.0

  • Terraform version: 1.7.5

  • Provider version(s): 5.84.0

Reproduction Code [Required]

module "s3_bucket" {
  source = "terraform-aws-modules/s3-bucket/aws"

  bucket = "tftest-foobar-bucket"

  lifecycle_rule = [
    {
      id      = "foobar"
      enabled = true

      expiration = null

      transition = null

      noncurrent_version_expiration = null

      noncurrent_version_transition = null

      filter = {
        tags = null
      }
    }
  ]
}

Steps to reproduce the behavior:

  • run terraform plan

Expected behavior

The plan to create a bucket with an empty lifecycle rule.

Actual behavior

Error:

│ Error: Attempt to get attribute from null value
│ 
│   on .terraform/modules/s3_bucket/main.tf line 281, in resource "aws_s3_bucket_lifecycle_configuration" "this":
│  281:           storage_class = transition.value.storage_class
│     ├────────────────
│     │ transition.value is null
│ 
│ This value is null, so it does not have any attributes.
╵
╷
│ Error: Attempt to get attribute from null value
│ 
│   on .terraform/modules/s3_bucket/main.tf line 302, in resource "aws_s3_bucket_lifecycle_configuration" "this":
│  302:           storage_class             = noncurrent_version_transition.value.storage_class
│     ├────────────────
│     │ noncurrent_version_transition.value is null
│ 
│ This value is null, so it does not have any attributes.
╵
╷
│ Error: Invalid function argument
│ 
│   on .terraform/modules/s3_bucket/main.tf line 317, in resource "aws_s3_bucket_lifecycle_configuration" "this":
│  317:         for_each = [for v in try(flatten([rule.value.filter]), []) : v if max(length(keys(v)), length(try(rule.value.filter.tags, rule.value.filter.tag, []))) == 1]
│ 
│ Invalid value for "inputMap" parameter: argument must not be null.
╵
╷
│ Error: Invalid function argument
│ 
│   on .terraform/modules/s3_bucket/main.tf line 337, in resource "aws_s3_bucket_lifecycle_configuration" "this":
│  337:         for_each = [for v in try(flatten([rule.value.filter]), []) : v if max(length(keys(v)), length(try(rule.value.filter.tags, rule.value.filter.tag, []))) > 1]
│ 
│ Invalid value for "inputMap" parameter: argument must not be null.
@GusAntoniassi GusAntoniassi linked a pull request Jan 25, 2025 that will close this issue
3 tasks
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.

1 participant