Created
July 14, 2022 19:55
-
-
Save sergsoares/c4e415a322245bcbefdb3ab651beb262 to your computer and use it in GitHub Desktop.
Validation of complex variables in terraform
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
# terraform.vars | |
rules = [ | |
{ | |
api_groups = [""] | |
resources = ["pods","services","deployments","jobs"] | |
verbs = ["*"] | |
}, | |
{ | |
api_groups = [""] | |
resources = ["namespaces"] | |
verbs = ["get", "watch", "list"] | |
} | |
] | |
# variables.tf | |
variable "rules" { | |
type = list(object({ | |
api_groups = list(string) | |
resources = list(string) | |
verbs = list(string) | |
})) | |
} | |
# main.tf | |
resource "kubernetes_cluster_role" "this" { | |
metadata { | |
name = "${var.app}" | |
labels = { | |
k8s-addon = "${var.app}.addons.k8s.io" | |
k8s-app = "${var.app}" | |
} | |
} | |
dynamic "rule" { | |
for_each = var.rules | |
content { | |
api_groups = rule.value["api_groups"] | |
resources = rule.value["resources"] | |
verbs = rule.value["verbs"] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If incorrect variables are provided: