diff --git a/environment/prod/variables.tf b/environment/prod/variables.tf index 997232e..af55f8c 100644 --- a/environment/prod/variables.tf +++ b/environment/prod/variables.tf @@ -92,6 +92,7 @@ variable "additional_db_users" { database = string privileges = list(string) })) + sensitive = true } variable "key_name" { diff --git a/environment/stage/main.tf b/environment/stage/main.tf index 05baf67..4921402 100644 --- a/environment/stage/main.tf +++ b/environment/stage/main.tf @@ -20,9 +20,6 @@ module "stage_stack" { # 인스턴스 스펙 instance_type = var.server_instance_type - # RDS 미사용 (Docker container로 대체) - enable_rds = false - # 보안 그룹 규칙 api_ingress_rules = var.api_ingress_rules diff --git a/modules/app_stack/db_users.tf b/modules/app_stack/db_users.tf new file mode 100644 index 0000000..1eb6033 --- /dev/null +++ b/modules/app_stack/db_users.tf @@ -0,0 +1,27 @@ +locals { + db_user_names = var.enable_rds || var.enable_db_ec2 ? toset(nonsensitive(keys(var.additional_db_users))) : toset([]) +} + +resource "mysql_user" "users" { + for_each = local.db_user_names + + user = each.key + host = "%" + plaintext_password = var.additional_db_users[each.key].password + + depends_on = [ + aws_db_instance.default, + aws_instance.db_server, + ] +} + +resource "mysql_grant" "user_grants" { + for_each = local.db_user_names + + user = each.key + host = "%" + database = var.additional_db_users[each.key].database + privileges = var.additional_db_users[each.key].privileges + + depends_on = [mysql_user.users] +} diff --git a/modules/app_stack/rds.tf b/modules/app_stack/rds.tf index c9ed1d1..88671d3 100644 --- a/modules/app_stack/rds.tf +++ b/modules/app_stack/rds.tf @@ -1,4 +1,3 @@ -# 5. RDS resource "aws_db_instance" "default" { count = var.enable_rds ? 1 : 0 @@ -21,29 +20,3 @@ resource "aws_db_instance" "default" { Name = var.rds_identifier } } - -# 6. MySQL 추가 유저 생성 -resource "mysql_user" "users" { - for_each = var.enable_rds || var.enable_db_ec2 ? var.additional_db_users : {} - - user = each.key - host = "%" - plaintext_password = each.value.password - - depends_on = [ - aws_db_instance.default, - aws_instance.db_server, - ] -} - -# 7. MySQL 권한 부여 -resource "mysql_grant" "user_grants" { - for_each = var.enable_rds || var.enable_db_ec2 ? var.additional_db_users : {} - - user = each.key - host = "%" - database = each.value.database - privileges = each.value.privileges - - depends_on = [mysql_user.users] -} diff --git a/modules/app_stack/variables.tf b/modules/app_stack/variables.tf index b143250..e7a2e2b 100644 --- a/modules/app_stack/variables.tf +++ b/modules/app_stack/variables.tf @@ -9,7 +9,7 @@ variable "instance_type" { variable "enable_rds" { description = "RDS 사용 여부" type = bool - default = true + default = false } variable "enable_db_ec2" { @@ -107,7 +107,8 @@ variable "additional_db_users" { database = string privileges = list(string) })) - default = {} + sensitive = true + default = {} } variable "db_engine_version" {