Skip to content

feat: allow secrets manager option #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jun 12, 2025
26 changes: 26 additions & 0 deletions src/asm.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
resource "aws_secretsmanager_secret" "default" {
count = local.asm_enabled ? 1 : 0

name = local.mysql_admin_password_key
description = format("%s admin creds", module.cluster.id)

# policy = "{}"
# kms_key_id = null # "aws/secretsmanager"
# recovery_window_in_days = null # 30

tags = module.this.tags
}

resource "aws_secretsmanager_secret_version" "default" {
count = local.asm_enabled ? 1 : 0

secret_id = one(aws_secretsmanager_secret.default[*].id)
secret_string = jsonencode({
cluster_domain = local.cluster_domain
db_host = module.aurora_mysql.master_host
db_port = module.aurora_mysql.port
cluster_name = module.aurora_mysql.cluster_identifier
username = local.mysql_admin_user
password = local.mysql_admin_password
})
}
2 changes: 1 addition & 1 deletion src/cluster-regional.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module "aurora_mysql" {
instance_type = var.mysql_instance_type

db_name = local.mysql_db_name
db_port = 3306
db_port = var.mysql_db_port
admin_password = local.mysql_admin_password
admin_user = local.mysql_admin_user

Expand Down
3 changes: 3 additions & 0 deletions src/main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
locals {
enabled = module.this.enabled

asm_enabled = local.enabled && var.secrets_store_type == "ASM"
ssm_enabled = local.enabled && var.secrets_store_type == "SSM"

vpc_outputs = module.vpc.outputs
dns_delegated_outputs = module.dns-delegated.outputs
vpc_id = local.vpc_outputs.vpc_id
Expand Down
11 changes: 8 additions & 3 deletions src/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,21 @@ output "aurora_mysql_master_hostname" {
}

output "aurora_mysql_master_password" {
value = local.mysql_db_enabled ? "Password for admin user ${module.aurora_mysql.master_username} is stored in SSM at ${local.mysql_admin_password_key}" : null
description = "Location of admin password in SSM"
value = local.mysql_db_enabled ? "Password for admin user ${module.aurora_mysql.master_username} is stored in ${var.secrets_store_type} at ${local.mysql_admin_password_key}" : null
description = "Location of admin password"
sensitive = true
}

output "aurora_mysql_master_password_ssm_key" {
value = local.mysql_db_enabled ? local.mysql_admin_password_key : null
value = local.ssm_enabled && local.mysql_db_enabled ? local.mysql_admin_password_key : null
description = "SSM key for admin password"
}

output "aurora_mysql_master_password_asm_key" {
value = local.asm_enabled && local.mysql_db_enabled ? local.mysql_admin_password_key : null
description = "ASM key for admin password"
}

output "aurora_mysql_master_username" {
value = local.enabled ? module.aurora_mysql.master_username : null
description = "Aurora MySQL username for the master DB user"
Expand Down
4 changes: 3 additions & 1 deletion src/ssm.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ locals {
},
{
name = format("%s/%s", local.ssm_path_prefix, "db_port")
value = "3306"
value = module.aurora_mysql.port
description = "Aurora MySQL DB Master TCP port"
type = "String"
overwrite = true
Expand Down Expand Up @@ -75,6 +75,8 @@ module "parameter_store_write" {
source = "cloudposse/ssm-parameter-store/aws"
version = "0.13.0"

enabled = local.ssm_enabled

# kms_arn will only be used for SecureString parameters
kms_arn = module.kms_key_rds.key_arn

Expand Down
24 changes: 23 additions & 1 deletion src/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,21 @@ variable "mysql_name" {

variable "mysql_db_name" {
type = string
description = "Database name (default is not to create a database"
description = "Database name (default is not to create a database)"
default = ""
}

variable "mysql_db_port" {
type = number
description = "Database port"
default = 3306

validation {
condition = var.mysql_db_port >= 1 && var.mysql_db_port <= 65535
error_message = "mysql_db_port must be between 1 and 65535."
}
}

variable "mysql_admin_user" {
type = string
description = "MySQL admin user name"
Expand Down Expand Up @@ -225,3 +236,14 @@ variable "vpc_component_name" {
default = "vpc"
description = "The name of the VPC component"
}

variable "secrets_store_type" {
type = string
description = "Secret Store type to save database credentials. Valid values: `SSM`, `ASM`"
default = "SSM"

validation {
condition = contains(["SSM", "ASM"], var.secrets_store_type)
error_message = "secrets_store_type must be one of: SSM, ASM."
}
}
Loading