diff --git a/README.md b/README.md index 17ab5b9..49cc740 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ module "rds-pg" { slack_username = "John" slack_channel = "skaf-dev" slack_webhook_url = "https://hooks/xxxxxxxx" + custom_user_password = "postgresqlpasswd" } ``` 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 [ | [cloudwatch\_metric\_alarms\_enabled](#input\_cloudwatch\_metric\_alarms\_enabled) | Boolean flag to enable/disable CloudWatch metrics alarms | `bool` | `false` | no | | [create\_db\_subnet\_group](#input\_create\_db\_subnet\_group) | Whether to create a database subnet group | `bool` | `true` | no | | [create\_security\_group](#input\_create\_security\_group) | Whether to create a security group for the database | `bool` | `true` | no | +| [custom\_user\_password](#input\_custom\_user\_password) | Custom password for the RDS master user | `string` | `""` | no | | [cw\_sns\_topic\_arn](#input\_cw\_sns\_topic\_arn) | The username to use when sending notifications to Slack. | `string` | `""` | no | | [db\_name](#input\_db\_name) | The name of the automatically created database on cluster creation | `string` | `""` | no | | [deletion\_protection](#input\_deletion\_protection) | Specifies whether accidental deletion protection is enabled | `bool` | `true` | no | diff --git a/examples/complete-psql-replica/main.tf b/examples/complete-psql-replica/main.tf index 13a8ffa..8b74987 100644 --- a/examples/complete-psql-replica/main.tf +++ b/examples/complete-psql-replica/main.tf @@ -10,6 +10,7 @@ locals { replica_enable = true replica_count = 1 current_identity = data.aws_caller_identity.current.arn + custom_user_password = "" allowed_security_groups = ["sg-0a680afd35"] additional_tags = { Owner = "Organization_Name" @@ -111,4 +112,5 @@ module "rds-pg" { slack_username = "Admin" slack_channel = "postgresql-notification" slack_webhook_url = "https://hooks/xxxxxxxx" + custom_user_password = local.custom_user_password } diff --git a/examples/complete/README.md b/examples/complete/README.md index 5381319..d056d1b 100644 --- a/examples/complete/README.md +++ b/examples/complete/README.md @@ -27,7 +27,7 @@ This example will be very useful for users who are new to a module and want to q | Name | Source | Version | |------|--------|---------| | [kms](#module\_kms) | terraform-aws-modules/kms/aws | n/a | -| [rds-pg](#module\_rds-pg) | squareops/rds-postgresql/aws | n/a | +| [rds-pg](#module\_rds-pg) | ../../ | n/a | | [vpc](#module\_vpc) | squareops/vpc/aws | n/a | ## Resources diff --git a/examples/complete/main.tf b/examples/complete/main.tf index 4b298b6..3223e4c 100644 --- a/examples/complete/main.tf +++ b/examples/complete/main.tf @@ -9,6 +9,7 @@ locals { storage_type = "gp3" current_identity = data.aws_caller_identity.current.arn allowed_security_groups = ["sg-0a680afd35"] + custom_user_password = "" additional_tags = { Owner = "Organization_Name" Expires = "Never" @@ -125,4 +126,5 @@ module "rds-pg" { slack_username = "Admin" slack_channel = "postgresql-notification" slack_webhook_url = "https://hooks/xxxxxxxx" + custom_user_password = local.custom_user_password } diff --git a/main.tf b/main.tf index 30f7e9f..bc343f3 100644 --- a/main.tf +++ b/main.tf @@ -16,7 +16,7 @@ module "db" { port = var.port engine = var.engine username = var.master_username - password = var.manage_master_user_password ? null : random_password.master[0].result + 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 multi_az = var.multi_az subnet_ids = var.subnet_ids kms_key_id = var.kms_key_arn @@ -152,6 +152,33 @@ module "security_group_rds" { ) } +resource "aws_secretsmanager_secret" "secret_master_db" { + name = format("%s/%s/%s", var.environment, var.name, "rds-postgresql-pass") + tags = merge( + { "Name" = format("%s/%s/%s", var.environment, var.name, "rds-mysql-pass") }, + local.tags, + ) +} + +resource "random_password" "master" { + count = var.manage_master_user_password ? 0 : var.custom_user_password == "" ? 1 : 0 + length = var.random_password_length + special = false +} + +resource "aws_secretsmanager_secret_version" "rds_credentials" { + count = length(random_password.master) > 0 ? 1 : 0 + secret_id = aws_secretsmanager_secret.secret_master_db.id + secret_string = < 0 ? element(random_password.master, 0).result : var.custom_password, + "engine": "${var.engine}", + "host": "${module.db.db_instance_endpoint}" +} +EOF +} + # Cloudwatch alarms resource "aws_cloudwatch_metric_alarm" "cache_cpu" { count = var.cloudwatch_metric_alarms_enabled ? 1 : 0 @@ -290,31 +317,3 @@ resource "aws_lambda_permission" "sns_lambda_slack_invoke" { principal = "sns.amazonaws.com" source_arn = aws_sns_topic.slack_topic[0].arn } - - -resource "aws_secretsmanager_secret" "secret_master_db" { - name = format("%s/%s/%s", var.environment, var.name, "rds-postgresql-pass") - tags = merge( - { "Name" = format("%s/%s/%s", var.environment, var.name, "rds-mysql-pass") }, - local.tags, - ) -} - -resource "random_password" "master" { - count = var.manage_master_user_password ? 0 : 1 - length = var.random_password_length - special = false -} - -resource "aws_secretsmanager_secret_version" "rds_credentials" { - count = var.manage_master_user_password ? 0 : 1 - secret_id = aws_secretsmanager_secret.secret_master_db.id - secret_string = <