diff --git a/postgres/variables.tf b/postgres/variables.tf index 886fd59..f1906fa 100644 --- a/postgres/variables.tf +++ b/postgres/variables.tf @@ -55,10 +55,26 @@ variable "postgres_instance_region" { # Postgres User and DB Configs variable "postgres_databases" { - description = "list of users and databases" - type = list(object({ - db_name = string # db name inside the instance - user_name = string # username and owner for postgres db - user_roles = list(string) # List of database access levels for the user. Supported values are: login, createdb. + description = "list of users and databases" + type = list(object({ + db_name = string # db name inside the instance + user_name = string # username and owner for postgres db + user_roles = list(string) # List of database access levels for the user. Supported values are: login, createdb. })) -} \ No newline at end of file + + # ----------------------------------------------------------------- + # Validation: each db_name must be unique + # ----------------------------------------------------------------- + validation { + condition = length(distinct([for db in var.postgres_databases : db.db_name])) == length(var.postgres_databases) + error_message = "Each db_name must be unique." + } + + # ----------------------------------------------------------------- + # Validation: each user_name must be unique + # ----------------------------------------------------------------- + validation { + condition = length(distinct([for db in var.postgres_databases : db.user_name])) == length(var.postgres_databases) + error_message = "Each user_name must be unique." + } +}