hashicorp / terraform-cdk

Define infrastructure resources using programming constructs and provision them using HashiCorp Terraform
https://www.terraform.io/cdktf
Mozilla Public License 2.0
4.79k stars 443 forks source link

COMPONENT: Add support for optional objects in variable definitions with non-trivial default values #3595

Open zoonage opened 2 months ago

zoonage commented 2 months ago

Description

I'd like to be able to define a variable like this in cdktf, where a is optional but b is not.

variable "complex" {
  type = object({
    a = optional(number, 1)
    b = string
  })
  default = {}
}

Background context: I'm looking into cdktf adoption to try and simplify a large existing Terraform codebase.

The first area I'm looking at is removing duplicated complex variable type signatures. These can be up to ~30 lines that we manually keep in sync to work around this issue.

I'm planning on doing this by synthesising a bit of the module (such as specific variable definitions) by using a shared library but keeping the rest of the existing Terraform in place.

Happy to try and implement this

References

No response

Help Wanted

Community Note

zoonage commented 2 months ago

Happy to have a shot at implementing this. The only question I have is I'm not sure if there is a way to build a complex type for a default like

{
  otel-agent = { image = "quay.io/zoonage/otel-agent:latest" }
  some-sidecar = { image = "quay.io/zoonage/otel-agent:latest", resources = { cpu = 1 } }
}

or if that's some extra functionality that'll be needed for this