-
-
Save chanux/8afa8c25bc198187eee892ab54063f3c to your computer and use it in GitHub Desktop.
Terraform: Typo in parameter name for object with two optionals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #--- | |
| # When using type definitions, Terraform validation does not help catching | |
| # typos because Terraform discards values not specified in the type, and | |
| # without warning! | |
| # | |
| # https://github.com/hashicorp/terraform/issues/29204 | |
| # | |
| # See the demonstration of this behaviour: | |
| # terraform plan -var-file test-type-check.tfvars | |
| # | |
| # See the suggested fix in action (Should error out complaining about the typo) | |
| # terraform plan -var-file test-solution.tfvars | |
| # | |
| #-- | |
| variable "complex" { | |
| description = "Complex object with two optionals" | |
| type = map(object({ | |
| email = optional(string) | |
| phone = optional(string) | |
| })) | |
| validation { | |
| condition = alltrue( | |
| flatten([ | |
| for o in var.complex : | |
| [for k in keys(o) : contains(["name", "email", "phone"], k)] | |
| ]) | |
| ) | |
| error_message = "Did you make a typo" | |
| } | |
| } | |
| # The solution suggested in the Github issue linked above is to set type to any | |
| # and do the validations. | |
| variable "whatever" { | |
| type = any | |
| default = null | |
| validation { | |
| condition = var.whatever == null ? true : alltrue( | |
| flatten([ | |
| for o in var.whatever : | |
| [for k in keys(o) : contains(["name", "email", "phone"], k)] | |
| ]) | |
| ) | |
| error_message = "Did you make a typo" | |
| } | |
| } | |
| output "complex" { | |
| value = merge( | |
| var.complex, | |
| { message = "You see that the type system did not complain about the type 'phon'" } | |
| ) | |
| } | |
| output "whatever" { | |
| value = var.whatever | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| complex = { | |
| one = { | |
| name = "jack" | |
| email = "j@example.com" | |
| phon = "121212" # intentional typo | |
| } | |
| } | |
| whatever = { | |
| one = { | |
| name = "jill" | |
| email = "ji@example.com" | |
| phon = "121219" # intentional typo | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| complex = { | |
| one = { | |
| name = "jack" | |
| email = "j@example.com" | |
| phon = "121212" # intentional typo | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment