So I have the following code and get the following error:
managed_rules = {
rds_storge_encrypted = {
identifier = "RDS_STORAGE_ENCRYPTED"
description = "Checks whether storage encryption is enabled for your RDS DB instances."
input_parameters = {
kmsKeyId : 123
}
enabled = "true"
applies_to_global_resources = "false"
},
encrypted_volumes = {
identifier = "VPC_SG_OPEN_ONLY_TO_AUTHORIZED_PORTS"
description = "Checks if security groups allowing unrestricted incoming traffic ('0.0.0.0/0' or '::/0') only allow inbound TCP or UDP connections on authorized ports."
input_parameters = {
authorizedTcpPorts : "456"
authorizedUdpPorts : "789"
}
enabled = "true"
applies_to_global_resources = "false"
}
}
resource "aws_config_organization_managed_rule" "rules" {
for_each = !var.create_account_rules ? local.rules_to_process :{}
name = each.key
}```
This results in the following error:
```Error: Inconsistent conditional result types
│
│ on .terraform/modules/baseline.aws_config_multi_region/modules/krrv-sca-aws-config-rules/main.tf line 81, in resource "aws_config_organization_managed_rule" "rules":
│ 81: for_each = !var.create_account_rules ? local.rules_to_process :{}
│ ├────────────────
│ │ local.rules_to_process is object with 8 attributes
│ │ var.create_account_rules is true
│
│ The true and false result expressions must have consistent types. The
│ 'true' value includes object attribute "encrypted_volumes", which is absent
│ in the 'false' value.
╵
╷
│ Error: Inconsistent conditional result types
│
│ on .terraform/modules/baseline.aws_config_multi_region/modules/krrv-sca-aws-config-rules/main.tf line 93, in resource "aws_config_config_rule" "rules":
│ 93: for_each = var.create_account_rules ? local.rules_to_process : {}
│ ├────────────────
│ │ local.rules_to_process is object with 8 attributes
│ │ var.create_account_rules is true
│
│ The true and false result expressions must have consistent types. The
│ 'true' value includes object attribute "encrypted_volumes", which is absent
│ in the 'false' value.```
Why does it think the local variable is an object with x attributes rather than a map and how to supply and empty map as the false value.
I am unable to reproduce the problem you are experiencing using the code you posted.
You have not posted the definition of local.rules_to_process, and guessing that you meant local.managed_rules (and left out the enclosing locals {} block around that), results in code that works fine for me.
rules_to_process = { for k, v in local.managed_rules : k => v if lookup(v, "enabled", true) && (v.applies_to_global_resources == false || var.enable_global_resource_rules) && !var.create_account_rules }