Closed mihaiplesa closed 3 years ago
This is a Terraform bug/feature after all https://github.com/hashicorp/terraform/issues/26265
I'm also having same exact issue:
Terraform v0.12.29
provider.aws v3.31.0
What if you set the undefined key values explicitly to null so the keys for each element were the same.
rules = [
{
type = "egress"
from_port = 0
to_port = 65535
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
self = null
},
{
type = "ingress"
from_port = 0
to_port = 65535
protocol = "tcp"
cidr_blocks = null
self = true
}
]
@nitrocode already tried and that crashes Terraform hard.
I have found a work around with optional values. (experimental future)
terraform {
experiments = [module_variable_optional_attrs]
}
define "rules" variable as following:
variable "rules" {
type = list(object({
description = optional(string)
type = string
from_port = number
to_port = number
protocol = string
cidr_blocks = optional(list(string))
ipv6_cidr_blocks = optional(list(string))
source_security_group_id = optional(string)
self = optional(bool)
}))
}
Which Terraform version? It's their bug after all.
Terraform 14 with the experimental flag enabled. It's experimental so it won't be enabled in this module until it's a stable feature presumably in terraform 15.
The issue https://github.com/hashicorp/terraform/issues/19898 tracks the optional feature
To the extent Hashicorp supports it, this is fixed in #15 (and maybe before).
Terraform requires that all the elements of the rules
list be exactly
the same type. This means you must supply all the same keys and, for each key,
all the values for that key must be the same type. Any optional key, such as
ipv6_cidr_blocks
, can be omitted from all the rules without problem. However,
if some rules have a key and other rules would omit the key if that were allowed
(e.g one rule has cidr_blocks
and another rule has self = true
, and neither
rule can include both cidr_blocks
and self
), instead of omitting the key,
include the key with value of null
, unless the value is a list type, in which case
set the value to []
(an empty list). (Cannot set list type to null
before Terraform
0.15 because of this Terraform bug)
So this will work going forward and probably works with earlier versions:
rules = [
{
type = "egress"
from_port = 0
to_port = 65535
protocol = "all"
cidr_blocks = ["0.0.0.0/0"]
self = null
},
{
type = "ingress"
from_port = 0
to_port = 65535
protocol = "tcp"
cidr_blocks = []
self = true
}
]
Passing rules as below will fail with:
Environment (please complete the following information):
Terraform 0.13.6 and module version 0.1.4 on macOS