Skip to content

Commit 2ec6acb

Browse files
committed
feat: add support for AWS-managed master password
This update introduces the `manage_master_user_password` variable, allowing the module to use AWS Secrets Manager for managing the DocumentDB master user password. The logic in `main.tf` is updated to handle three cases: 1) AWS-managed password, 2) user-provided password, and 3) auto-generated password. Also updates the AWS provider version constraint and fixes a typo in the random_password resource.
1 parent 3705896 commit 2ec6acb

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

src/main.tf

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
locals {
22
enabled = module.this.enabled
3-
create_password = local.enabled && (var.master_password == null || var.master_password == "")
3+
create_password = local.enabled && var.master_password == null && var.manage_master_user_password == null
4+
# 1. If manage_master_user_password is not null, AWS manages the password (master_password must be null)
5+
# 2. If master_password is provided, that value is used (manage_master_user_password must be null)
6+
# 3. If both are null, the module creates a random password
7+
master_password = local.create_password ? one(random_password.master_password[*].result) : var.master_password
8+
49
}
510

611
module "documentdb_cluster" {
@@ -27,9 +32,10 @@ module "documentdb_cluster" {
2732
apply_immediately = var.apply_immediately
2833
auto_minor_version_upgrade = var.auto_minor_version_upgrade
2934

30-
db_port = var.db_port
31-
master_username = var.master_username
32-
master_password = local.create_password ? one(random_password.master_password[*].result) : var.master_password
35+
db_port = var.db_port
36+
master_username = var.master_username
37+
master_password = var.manage_master_user_password != null ? null : local.master_password
38+
manage_master_user_password = var.manage_master_user_password
3339

3440
vpc_id = module.vpc.outputs.vpc_id
3541
subnet_ids = module.vpc.outputs.private_subnet_ids

src/ssm.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ resource "random_password" "master_password" {
1515
special = false
1616
upper = true
1717
lower = true
18-
number = true
18+
numeric = true
1919

2020
min_special = 0
2121
min_upper = 1

src/variables.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ variable "master_password" {
4040
description = "(Required unless a snapshot_identifier is provided) Password for the master DB user. Note that this may show up in logs, and it will be stored in the state file. Please refer to the DocumentDB Naming Constraints"
4141
}
4242

43+
variable "manage_master_user_password" {
44+
type = bool
45+
description = "Whether to manage the master user password using AWS Secrets Manager."
46+
default = null
47+
validation {
48+
condition = var.manage_master_user_password == null || var.manage_master_user_password == true
49+
error_message = "Error: `manage_master_user_password` must be set to `true` or `null`"
50+
}
51+
}
52+
4353
variable "retention_period" {
4454
type = number
4555
default = 5

src/versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 3.0, < 6.0.0"
7+
version = ">= 5.29.0, < 6.0.0"
88
}
99
random = {
1010
source = "hashicorp/random"

0 commit comments

Comments
 (0)