terraform-cdk icon indicating copy to clipboard operation
terraform-cdk copied to clipboard

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

Open zoonage opened this issue 10 months ago • 3 comments

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

  • [X] I'm interested in contributing a fix myself

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

zoonage avatar Apr 19 '24 00:04 zoonage

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

zoonage avatar Apr 19 '24 09:04 zoonage

I am extremely interested in this feature as well. I am hoping by providing the example Hashi provides for HCL here, the complexity is less than you think.

The optional modifier takes one or two arguments.

Type: (Required) The first argument specifies the type of the attribute. Default: (Optional) The second argument defines the default value that Terraform should use if the attribute is not present. This must be compatible with the attribute type. If not specified, Terraform uses a null value of the appropriate type as the default.

variable "buckets" {
  type = list(object({
    name    = string
    enabled = optional(bool, true)
    website = optional(object({
      index_document = optional(string, "index.html")
      error_document = optional(string, "error.html")
      routing_rules  = optional(string)
    }), {})
  }))
}

chrcaleb avatar Aug 06 '24 21:08 chrcaleb

I don't think it would be too difficult to add a simple version of this. If you look at https://github.com/hashicorp/terraform-cdk/blob/main/packages/cdktf/lib/terraform-variable.ts there are some static functions meant for building complex types. A new method for optional could be created as a quick version. Building better default support would take some more thought.

Actually using complex variables effectively within cdktf is another issue, but having basic support may help make things easier.

jsteinich avatar Aug 31 '24 01:08 jsteinich