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

Consistent types error when using logging with conditional #314

Open
paxlo opened this issue Feb 17, 2025 · 0 comments
Open

Consistent types error when using logging with conditional #314

paxlo opened this issue Feb 17, 2025 · 0 comments

Comments

@paxlo
Copy link

paxlo commented Feb 17, 2025

Description

I create a whole bunch of buckets as follows. I need to optionally enable versioning or logging (with predefined bucket name and prefix) on each of them

locals {
  buckets = [
    {
      name        = "bucket1"
      versioning  = true
    },
    {
      name    = "bucket2"
      logging = false
    }
  ]
}

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

  for_each = { for bucket in local.buckets : bucket.name => bucket }

  bucket        = each.value.name

  # works
  versioning = lookup(each.value, "versioning", false) ? {
    enabled    = true
    mfa_delete = false
  } : {}

  # doesn't
  logging = lookup(each.value, "logging", false) ? {
    target_bucket = "logging-bucket-name"
    target_prefix = "/"
    target_object_key_format = {
      partitioned_prefix = {
        partition_date_source = "EventTime"
      }
    }
  } : {}
}

Enabling/disabling for versioning condition works fine. But the similar code for enabling/disabling logging fails with an error regardless of logging = true/false in locals

Error: Inconsistent conditional result types
The true and false result expressions must have consistent types. The 'true' value includes object attribute "target_bucket", which is absent in the 'false' value.

Same error when using try instead of lookup

Versions

  • Module version [Required]: 4.6.0

  • Terraform version: Terraform v1.10.5

  • Provider version(s): provider registry.terraform.io/hashicorp/aws v5.86.1

Reproduction Code [Required]

locals {
  buckets = [
    {
      name        = "bucket1"
      versioning  = true
    },
    {
      name    = "bucket2"
      logging = false
    }
  ]
}

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

  for_each = { for bucket in local.buckets : bucket.name => bucket }

  bucket        = each.value.name

  # works
  versioning = lookup(each.value, "versioning", false) ? {
    enabled    = true
    mfa_delete = false
  } : {}

  # doesn't
  logging = lookup(each.value, "logging", false) ? {
    target_bucket = "logging-bucket-name"
    target_prefix = "/"
    target_object_key_format = {
      partitioned_prefix = {
        partition_date_source = "EventTime"
      }
    }
  } : {}
}

Steps to reproduce the behavior:

run terraform plan or terraform validate

Expected behavior

I expect a logging configuration will be created in case logging = true in locals. If it's false or does not exist, logging will not be enabled

Actual behavior

│ Error: Inconsistent conditional result types
│ 
│   on s3.tf line 29, in module "s3_bucket":
│   29:   logging = lookup(each.value, "logging", false) ? {
│   30:     target_bucket = "logging-bucket-name"
│   31:     target_prefix = "/"
│   32:     target_object_key_format = {
│   33:       partitioned_prefix = {
│   34:         partition_date_source = "EventTime"
│   35:       }
│   36:     }
│   37:   } : {}
│ 
│ The true and false result expressions must have consistent types. The 'true' value includes object attribute "target_bucket", which is absent in the 'false' value.
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

No branches or pull requests

1 participant