Skip to content

replication_configuration variable does not handle null #297

@tcarreira

Description

@tcarreira

Description

With the current setup it is not possible to conditionally configure an S3 Bucket with replication_configuration.
The true and false result expressions can't have consistent types, because the module code is not designed for that.

If the condition is true (replication_configuration should be defined), then an empty object ({}) as false result is not accepted and raises a Terraform error The true and false result expressions must have consistent types. The 'true' value includes object attribute "role", which is absent in the 'false' value., only null is possible.

If the condition is false (replication_configuration should not be defined), null is not accepted by the module code, resulting in the error Invalid value for "inputMap" parameter: argument must not be null..

Versions

  • Module version [Required]: 4.2.2

  • Terraform version: Terraform v1.9.8 on linux_amd64

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

Reproduction Code [Required]

Steps to reproduce the behavior:

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = ">=5.73.0"
    }
  }
}

variable "should_replicate" {
  default = false
}

locals {
  replication_configuration = var.should_replicate ? {
    role = "arn:aws:iam::1234567890:role/myreplicationrole"
    rules = [
      {
        id     = "everything-without-filters"
        status = "Enabled"

        delete_marker_replication = true

        destination = {
          bucket        = "arn:aws:s3:::destinationbucketname"
          storage_class = "STANDARD"
        }
      },
    ]
  } : null
}

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

  bucket                    = "mybucketname"
  force_destroy             = true
  replication_configuration = local.replication_configuration
}

Expected behavior

The module should not give an error and should not set replication if var.replication_configuration == null

Actual behavior

It is not possible to conditionally set replication_configuration

Terminal Output Screenshot(s)

╷
│ Error: Invalid function argument
│ 
│   on .terraform/modules/s3_bucket/main.tf line 373, in resource "aws_s3_bucket_replication_configuration" "this":
│  373:   count = local.create_bucket && length(keys(var.replication_configuration)) > 0 ? 1 : 0
│     ├────────────────
│     │ while calling keys(inputMap)
│     │ var.replication_configuration is null
│ 
│ Invalid value for "inputMap" parameter: argument must not be null.

note: initially reported at #285

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions