Skip to content

Commit d26bd82

Browse files
Merge pull request #8 from rachit89/release-feature
Release feature for adding custom user password.
2 parents 53e6d8f + 1ddd52f commit d26bd82

File tree

7 files changed

+42
-31
lines changed

7 files changed

+42
-31
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ module "rds-pg" {
5353
slack_username = "John"
5454
slack_channel = "skaf-dev"
5555
slack_webhook_url = "https://hooks/xxxxxxxx"
56+
custom_user_password = "postgresqlpasswd"
5657
}
5758
```
5859
Refer [examples](https://github.com/squareops/terraform-aws-rds-postgresql/tree/main/examples) for more details.
@@ -121,6 +122,7 @@ The required IAM permissions to create resources from this module can be found [
121122
| <a name="input_cloudwatch_metric_alarms_enabled"></a> [cloudwatch\_metric\_alarms\_enabled](#input\_cloudwatch\_metric\_alarms\_enabled) | Boolean flag to enable/disable CloudWatch metrics alarms | `bool` | `false` | no |
122123
| <a name="input_create_db_subnet_group"></a> [create\_db\_subnet\_group](#input\_create\_db\_subnet\_group) | Whether to create a database subnet group | `bool` | `true` | no |
123124
| <a name="input_create_security_group"></a> [create\_security\_group](#input\_create\_security\_group) | Whether to create a security group for the database | `bool` | `true` | no |
125+
| <a name="input_custom_user_password"></a> [custom\_user\_password](#input\_custom\_user\_password) | Custom password for the RDS master user | `string` | `""` | no |
124126
| <a name="input_cw_sns_topic_arn"></a> [cw\_sns\_topic\_arn](#input\_cw\_sns\_topic\_arn) | The username to use when sending notifications to Slack. | `string` | `""` | no |
125127
| <a name="input_db_name"></a> [db\_name](#input\_db\_name) | The name of the automatically created database on cluster creation | `string` | `""` | no |
126128
| <a name="input_deletion_protection"></a> [deletion\_protection](#input\_deletion\_protection) | Specifies whether accidental deletion protection is enabled | `bool` | `true` | no |

examples/complete-psql-replica/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ locals {
1010
replica_enable = true
1111
replica_count = 1
1212
current_identity = data.aws_caller_identity.current.arn
13+
custom_user_password = ""
1314
allowed_security_groups = ["sg-0a680afd35"]
1415
additional_tags = {
1516
Owner = "Organization_Name"
@@ -111,4 +112,5 @@ module "rds-pg" {
111112
slack_username = "Admin"
112113
slack_channel = "postgresql-notification"
113114
slack_webhook_url = "https://hooks/xxxxxxxx"
115+
custom_user_password = local.custom_user_password
114116
}

examples/complete/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ This example will be very useful for users who are new to a module and want to q
2727
| Name | Source | Version |
2828
|------|--------|---------|
2929
| <a name="module_kms"></a> [kms](#module\_kms) | terraform-aws-modules/kms/aws | n/a |
30-
| <a name="module_rds-pg"></a> [rds-pg](#module\_rds-pg) | squareops/rds-postgresql/aws | n/a |
30+
| <a name="module_rds-pg"></a> [rds-pg](#module\_rds-pg) | ../../ | n/a |
3131
| <a name="module_vpc"></a> [vpc](#module\_vpc) | squareops/vpc/aws | n/a |
3232

3333
## Resources

examples/complete/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ locals {
99
storage_type = "gp3"
1010
current_identity = data.aws_caller_identity.current.arn
1111
allowed_security_groups = ["sg-0a680afd35"]
12+
custom_user_password = ""
1213
additional_tags = {
1314
Owner = "Organization_Name"
1415
Expires = "Never"
@@ -125,4 +126,5 @@ module "rds-pg" {
125126
slack_username = "Admin"
126127
slack_channel = "postgresql-notification"
127128
slack_webhook_url = "https://hooks/xxxxxxxx"
129+
custom_user_password = local.custom_user_password
128130
}

main.tf

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module "db" {
1616
port = var.port
1717
engine = var.engine
1818
username = var.master_username
19-
password = var.manage_master_user_password ? null : random_password.master[0].result
19+
password = var.custom_user_password != "" ? var.custom_user_password : var.manage_master_user_password ? null : length(random_password.master) > 0 ? random_password.master[0].result : null
2020
multi_az = var.multi_az
2121
subnet_ids = var.subnet_ids
2222
kms_key_id = var.kms_key_arn
@@ -152,6 +152,33 @@ module "security_group_rds" {
152152
)
153153
}
154154

155+
resource "aws_secretsmanager_secret" "secret_master_db" {
156+
name = format("%s/%s/%s", var.environment, var.name, "rds-postgresql-pass")
157+
tags = merge(
158+
{ "Name" = format("%s/%s/%s", var.environment, var.name, "rds-mysql-pass") },
159+
local.tags,
160+
)
161+
}
162+
163+
resource "random_password" "master" {
164+
count = var.manage_master_user_password ? 0 : var.custom_user_password == "" ? 1 : 0
165+
length = var.random_password_length
166+
special = false
167+
}
168+
169+
resource "aws_secretsmanager_secret_version" "rds_credentials" {
170+
count = length(random_password.master) > 0 ? 1 : 0
171+
secret_id = aws_secretsmanager_secret.secret_master_db.id
172+
secret_string = <<EOF
173+
{
174+
"username": "${module.db.db_instance_username}",
175+
"password": length(random_password.master) > 0 ? element(random_password.master, 0).result : var.custom_password,
176+
"engine": "${var.engine}",
177+
"host": "${module.db.db_instance_endpoint}"
178+
}
179+
EOF
180+
}
181+
155182
# Cloudwatch alarms
156183
resource "aws_cloudwatch_metric_alarm" "cache_cpu" {
157184
count = var.cloudwatch_metric_alarms_enabled ? 1 : 0
@@ -290,31 +317,3 @@ resource "aws_lambda_permission" "sns_lambda_slack_invoke" {
290317
principal = "sns.amazonaws.com"
291318
source_arn = aws_sns_topic.slack_topic[0].arn
292319
}
293-
294-
295-
resource "aws_secretsmanager_secret" "secret_master_db" {
296-
name = format("%s/%s/%s", var.environment, var.name, "rds-postgresql-pass")
297-
tags = merge(
298-
{ "Name" = format("%s/%s/%s", var.environment, var.name, "rds-mysql-pass") },
299-
local.tags,
300-
)
301-
}
302-
303-
resource "random_password" "master" {
304-
count = var.manage_master_user_password ? 0 : 1
305-
length = var.random_password_length
306-
special = false
307-
}
308-
309-
resource "aws_secretsmanager_secret_version" "rds_credentials" {
310-
count = var.manage_master_user_password ? 0 : 1
311-
secret_id = aws_secretsmanager_secret.secret_master_db.id
312-
secret_string = <<EOF
313-
{
314-
"username": "${module.db.db_instance_username}",
315-
"password": "${random_password.master[0].result}",
316-
"engine": "${var.engine}",
317-
"host": "${module.db.db_instance_endpoint}"
318-
}
319-
EOF
320-
}

outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ output "db_instance_username" {
2525

2626
output "db_instance_password" {
2727
description = "Password for accessing the database."
28-
value = nonsensitive(random_password.master[0].result)
28+
value = var.custom_user_password != "" ? var.custom_user_password : nonsensitive(random_password.master[0].result)
2929
}
3030

3131
output "master_credential_secret_arn" {

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,9 @@ variable "performance_insights_retention_period" {
305305
type = number
306306
default = 7
307307
}
308+
309+
variable "custom_user_password" {
310+
description = "Custom password for the RDS master user"
311+
default = ""
312+
type = string
313+
}

0 commit comments

Comments
 (0)