Reputation: 389
I have a problem trying to rewrite a list to work in Terraform 0.12 that works in Terraform 0.11.
This is my list:
variable "master_authorized_networks_config" {
type = list(string)
description = "The list of CIDR blocks of master authorized networks."
default = [
{
cidr_blocks = [
{
cidr_block = "XXXXXX/32"
display_name = "XXXXX"
},
{
cidr_block = "XXXXXX/32"
display_name = "XXXXX"
},
{
cidr_block = "XXXXXX/32"
display_name = "XXXXX"
},
{
cidr_block = "XXXXXX/32"
display_name = "XXXXX"
},
]
},
]
I added it to module:
master_authorized_networks_config = var.master_authorized_networks_config
After running terraform apply
I get the following error:
Error: Invalid default value for variable
on ../../modules/xxx/xxx/variables.tf line 71, in variable "master_authorized_networks_config":
71: default = [
72: {
73: cidr_blocks = [
74: {
75: cidr_block = "XXXXXX/32"
76: display_name = "XXXXX"
77: },
78: {
79: cidr_block = "XXXXXX/32"
80: display_name = "XXXXX"
81: },
82: {
83: cidr_block = "XXXXXX/32"
84: display_name = "XXXXX"
85: },
86: {
87: cidr_block = "XXXXXX/32"
88: display_name = "XXXXX"
89: },
102: ]
103: },
104: ]
This default value is not compatible with the variable's type constraint:
element 0: string required.
I can't resolve this problem. Can you help me?
Upvotes: 1
Views: 1177
Reputation: 56859
The error is telling you that your default value doesn't match the type constraint that you've told Terraform about with type = list(string)
. That type constraint says that it must be a list of strings and is the default coming from 0.11 via the 0.12upgrade
tool that you may have ran to get that output if you had type = list
previously.
You could change your type constraint to be list(object)
. If you simplified your data structure so that you just had a list of objects with cidr_block
and display_name
keys instead of a list of objects with key cidr_blocks
that has a list of the aforementioned objects then you could even go as far as validating that more precisely:
variable "master_authorized_networks_config" {
type = list(object{
cidr_block = string
display_name = string
})
description = "The list of CIDR blocks of master authorized networks."
default = [
{
cidr_block = "XXXXXX/32"
display_name = "XXXXX"
},
# ...
]
}
Or, much simpler, when providing a default you can choose to let Terraform infer the expected object type automatically by removing the type
parameter from the variable. Then if anyone attempts to pass in a variable that doesn't match the type of the default Terraform will tell you at plan or validate time.
Upvotes: 3