Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validator: Provide validation for mismatching types #1442

Open
3 tasks
radeksimko opened this issue Oct 4, 2023 · 0 comments
Open
3 tasks

validator: Provide validation for mismatching types #1442

radeksimko opened this issue Oct 4, 2023 · 0 comments

Comments

@radeksimko
Copy link
Member

Context

One of the sources of misunderstanding and friction in the Terraform language is type (constraint) mismatch, which is in part because types were added in a later version of HCL/Terraform and - relatedly - because Terraform is still relatively permissive when comparing and converting types.

Wrong Type

resource "aws_instance" "name" {
  ami = compact(["ami-1", "ami-2"])
}

Here ami has constraint of cty.String and we're supplying a function with return type of cty.List(cty.String).

resource "aws_instance" "name" {
  ami = compact({ foo = "ami-1", bar = "ami-2" })
}

Here the compact function expects a cty.List(cty.String) but we're supplying a cty.Object().

Convertible Types (bad practice)

resource "aws_instance" "name" {
  count                       = "0"
  associate_public_ip_address = "false"
}

Here count expects cty.Number and we're passing cty.String, then associate_public_ip_address expects cty.Bool and we're passing cty.String.

In either case the value happens to be convertible to the constraint type (i.e. "0" converts to 0 and "false" converts to false). So this is accepted but does not represent good practice and users should avoid it.

Proposal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant