Skip to content

Error when using lifecycle rule with null values #308

Open
@GusAntoniassi

Description

@GusAntoniassi

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions