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.8k stars 443 forks source link

Convert: variables / locals / referenced maps need to use accessor syntax #2670

Closed DanielMSchmidt closed 11 months ago

DanielMSchmidt commented 1 year ago

Community Note

cdktf & Language Versions

Affected Resource(s)

Summary

Converting this example leads to an invalid access to the project key in the map, where a Fn.lookup call would have been necessary

variable "default_tags" {
  type        = map(string)
  description = "Map of default tags to apply to resources"
  default = {
    project = "Learning Live with AWS & HashiCorp"
  }
}

resource "aws_eip" "nat" {
  vpc = true
  tags = {
    "Name" = "${var.default_tags.project}-nat-eip"
  }
}
ansgarm commented 1 year ago

While this might not be that much better than Fn.lookup – we might think about exposing the PropertyAccess expression. The good thing about that way, would be that it supports a path, which might be more readable for nested access:

Fn.Lookup(Fn.Lookup(Fn.Lookup(mapRef, "c"), "b"), "a")
// vs.
Todo.propertyAccess(mapRef, ["a", "b", "c"])

(Sidenote: I left out the defaultValue param for lookup for simplicity, as that one is actually still optional but discouraged since TF 0.7; docs)

ansgarm commented 1 year ago

We might even expose this via TF functions 🤔 Maybe Fn.property(mapRef, ["a", "b", "c"]) (like lodash) or some better wording. Another idea would be to do the following:

Fn.lookup(ref, property, defaultValue) // => lookup() TF call
Fn.lookup(ref, property) // => ref[property] TF expression
Fn.lookupNested(ref, [propertyA, propertyB]) // => ref[propertyA][propertyB] TF expression

What do you think? 🤔

DanielMSchmidt commented 1 year ago

I think that's a good idea

github-actions[bot] commented 10 months 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've 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.