Created
June 14, 2023 15:54
-
-
Save oshea00/4a87c2b18b47707c5628dd39c681c585 to your computer and use it in GitHub Desktop.
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
# Type declaration | |
variable "users" { | |
type = map(object({ | |
role = string | |
})) | |
default = { | |
"mike" = { "role" = "admin" } | |
"steve" = { "role" = "user" } | |
"joe" = { "role" = "admin" } | |
} | |
} | |
# Type inferred | |
variable "items" { | |
default = { | |
"mike" = { "role" = "admin" } | |
"steve" = { "role" = "user" } | |
"joe" = { "role" = "admin" } | |
} | |
} | |
locals { | |
# Group values by chosen key | |
users_by_role = { | |
for name, user in var.users : user.role => name... | |
} | |
# Conditionally choose elements returned as objects | |
admin_users = { | |
for name, user in var.users : name => user if user.role == "admin" | |
} | |
# Conditionally choose elements returns as tuple | |
regular_users = [ | |
for name, user in var.users : name if user.role == "user" | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment