hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.85k stars 9.19k forks source link

[Bug]: Merging Nested Input Variable with a Local Variable results in null value - only for apply and when type is specified #29877

Closed jaxonawain closed 1 year ago

jaxonawain commented 1 year ago

Terraform Core Version

1.3.0

AWS Provider Version

4.5.0

Affected Resource(s)

Input Variables, Local Variables

Expected Behavior

Should be able to pass in an input variable with nested configuration, and merge it into a desired set of default values for said nested configuration.

The merged result should contain the default values, with any overrides or extra configuration listed in the input variable.

This works fine, if the input variable does not have a type assigned, or is assigned any

Actual Behavior

Once the type is specified in the variable declaration, the resulting configuration will show null values for values in final map.

Relevant Error/Panic Output Snippet

No response

Terraform Configuration Files

Module:

variable "desired_configuration" {
  description = "Desired Configuration. Fall back to local.default_configuration values if data not passed"
  type = object({
    az_configuration : object({}),
    region_prim : string,
    supernet : string,
    flow_log_retention_in_days = optional(number)
  })
}

locals {
  default_configuration = {
    flow_log_retention_in_days = 90
  }
  merged_config = merge(local.default_configuration, var.desired_configuration)
}

output "merged_config" {
  value = local.merged_config
}

Instantiation:

####
# Provider Declaration
####
provider "aws" {}

terraform {
  required_version = "~> 1.3.0"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.31.0"
    }
  }
}

locals {
  desired_configuration =  {
    region_prim = "us-x-x"
    supernet    = "x.x.x.x/xx"
    az_configuration = {
      us-x-xy = {
        main = "x.x.x.x/xx"
      }
      us-x-xz = {
        main = "x.x.x.x/xx"
      }
    }
  }
}

module "test_configuration" {
  source = "./module"
  desired_configuration = local.desired_configuration
}

output "merged_config" {
  value = module.test_configuration.merged_config
}

Output:

merged_config = {
  "az_configuration" = {}
  "flow_log_retention_in_days" = tonumber(null)
  "region_prim" = "us-x-x"
  "supernet" = "x.x.x.x/xx"
}

WORKAROUND: Change input variable in module to specify type = any

New Module:

variable "desired_configuration" {
  description = "Desired Configuration. Fall back to local.default_configuration values if data not passed"
  type = any
}

locals {
  default_configuration = {
    flow_log_retention_in_days = 90
  }
  merged_config = merge(local.default_configuration, var.desired_configuration)
}

output "merged_config" {
  value = local.merged_config
}

Instantiation remains unchanged

Output:

  "az_configuration" = {
    "us-x-xy" = {
      "main" = "x.x.x.x/xx"
    }
    "us-x-xz" = {
      "main" = "x.x.x.x/xx"
    }
  }
  "flow_log_retention_in_days" = 90
  "region_prim" = "us-x-x"
  "supernet" = "x.x.x.x/xx"
}

scratch 2.zip

Steps to Reproduce

  1. Unzip attached scratch 2 folder
  2. Init terraform
  3. Run terraform apply
  4. Note output
  5. Change variable
    variable "desired_configuration" {
    description = "Desired Configuration. Fall back to local.default_configuration values if data not passed"
    type = object({
    az_configuration : object({}),
    region_prim : string,
    supernet : string,
    flow_log_retention_in_days = optional(number)
    })
    }

to

variable "desired_configuration" {
  description = "Desired Configuration. Fall back to local.default_configuration values if data not passed"
  type = any
}

in the module/main.tf file

  1. Re-apply
  2. Note difference in output.

Debug Output

No response

Panic Output

No response

Important Factoids

No response

References

No response

Would you like to implement a fix?

None

github-actions[bot] commented 1 year ago

Community Note

Voting for Prioritization

Volunteering to Work on This Issue

jaxonawain commented 1 year ago

Note: this issue is not specific to aws provider. I have moved to terraform support

https://github.com/hashicorp/terraform/issues/32813

github-actions[bot] commented 1 year ago

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.