Skip to content

Commit a573336

Browse files
committed
feat: allow setting roles for users
1 parent 1d5e690 commit a573336

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

src/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ module "additional_users" {
4141
service_name = each.key
4242
db_user = each.value.db_user
4343
db_password = each.value.db_password
44+
roles = each.value.roles
4445
grants = each.value.grants
4546
ssm_path_prefix = local.ssm_path_prefix
4647
kms_key_id = local.kms_key_arn

src/modules/postgresql-user/main.tf

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
locals {
22
enabled = module.this.enabled
33

4-
db_user = length(var.db_user) > 0 ? var.db_user : var.service_name
5-
db_password = length(var.db_password) > 0 ? var.db_password : join("", random_password.db_password[*].result)
6-
4+
db_user = length(var.db_user) > 0 ? var.db_user : var.service_name
5+
db_password = length(var.db_password) > 0 ? var.db_password : join("", random_password.db_password[*].result)
6+
db_roles = length(var.roles) > 0 ? var.roles : null
77
save_password_in_ssm = local.enabled && var.save_password_in_ssm
88

99
db_password_key = format("%s/%s/passwords/%s", var.ssm_path_prefix, var.service_name, local.db_user)
@@ -15,6 +15,7 @@ locals {
1515
overwrite = true
1616
} : null
1717

18+
1819
parameter_write = local.save_password_in_ssm ? [local.db_password_ssm] : []
1920

2021
# ALL grant always shows Terraform drift:
@@ -40,6 +41,7 @@ resource "postgresql_role" "default" {
4041
name = local.db_user
4142
password = local.db_password
4243
login = true
44+
roles = local.db_roles
4345
}
4446

4547
# Apply the configured grants to the user

src/modules/postgresql-user/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ variable "db_password" {
1515
default = ""
1616
}
1717

18+
variable "roles" {
19+
type = list(string)
20+
description = "Roles that will be granted to the created user."
21+
default = null
22+
}
23+
1824
variable "grants" {
1925
type = list(object({
2026
grant : list(string)

src/variables.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ variable "additional_users" {
6464
type = map(object({
6565
db_user : string
6666
db_password : string
67+
roles : optional(list(string), [])
6768
grants : list(object({
6869
grant : list(string)
6970
db : string

0 commit comments

Comments
 (0)