ããã«ã¡ã¯ãXï¼ã¯ãã¹ï¼ã¤ããã¼ã·ã§ã³æ¬é¨ ã½ããã¦ã§ã¢ãã¶ã¤ã³ã»ã³ã¿ã¼ ã»ãã¥ãªãã£ã°ã«ã¼ãã®è¿ã§ãã
Terraform 㧠Amazon RDS ã¤ã³ã¹ã¿ã³ã¹/ã¯ã©ã¹ã¿ã¼ãä½ãæã«ãpassword
ã¾ã㯠master_password
å±æ§ã«æå®ãããã¹ã¿ã¼ã¦ã¼ã¶ã¼ã®ãã¹ã¯ã¼ãã tfstate
ãã¡ã¤ã«ã«å¹³æã§æ®ã£ã¦ãã¾ãåé¡ãããã¾ããã
ããããããéå»ã®è©±ãTerraform AWS Provider v4.61.0 ãããã®åé¡ã解æ¶ããæ¹æ³ãæä¾ããã¦ããã®ã§ãããã«ã¤ãã¦ãç´¹ä»ãã¾ãã
- RDS 㨠Secrets Manager ã®çµ±å
- manage_master_user_password ã使ããªãå ´å
- manage_master_user_password ã使ã£ãå ´å
- aws_rds_cluster ã«ãå©ç¨ã§ãã
- KMSãã¼ãæå®ãã
- æ¢åã® Secrets Manager ã·ã¼ã¯ã¬ãããå©ç¨ããå ´å
- æ¢åã®DBã«ä½¿ã£ããã©ããªãã
- ã¹ãããã·ã§ãããããªã¹ãã¢ããæã®æå
- ãããã«
RDS 㨠Secrets Manager ã®çµ±å
äºã®å§ã¾ã㯠2022å¹´12æã«çºè¡¨ããã Amazon RDS 㨠AWS Secrets Manager ã®çµ±åã¨ããã¢ãããã¼ãã§ãããDB ä½ææã«ãRDS ã¸ã® API ã³ã¼ã«ã«ã¦ãã¹ã¿ã¼ã¦ã¼ã¶ã¼ã®ãã¹ã¯ã¼ãã Secrets Manager ã§ä½æã»ä¿åãã¦ãããããã«ãªãã¾ããã
Terraform AWS Provider ã§ãããã¸ã®å¯¾å¿ã¨ãã¦ã2023å¹´3æã«ãªãªã¼ã¹ããã v4.61.0
㧠manage_master_user_password
å±æ§ã aws_rds_cluster
㨠aws_db_instance
ã§å©ç¨ã§ããããã«ãªãã¾ããï¼
å®éã«ãªã½ã¼ã¹ãä½æãã¦è©¦ãã¦ã¿ã¾ãã
manage_master_user_password ã使ããªãå ´å
以ä¸ã®ããã«å¹³æ㧠password
ãæå®ãã aws_db_instance
ãªã½ã¼ã¹ãä½æãã¦ã¿ã¾ããï¼BADãã©ã¯ãã£ã¹ã§ãï¼
resource "aws_db_instance" "my-db-1" { allocated_storage = 10 engine = "mysql" engine_version = "5.7" instance_class = "db.t3.micro" username = "master" # password ã«å¹³æã§ãã¹ã¯ã¼ããè¨è¿°ããæªãä¾ password = "MyPassword123" vpc_security_group_ids = [aws_security_group.db.id] skip_final_snapshot = true db_subnet_group_name = "db-subnet-group" }
ãããã¤å¾ã® terraform.tfstate
ãã¡ã¤ã«ã«ã¯ã次ã®ã¨ããå¹³æã§ãã¹ã¯ã¼ãæååãè¨é²ããã¦ãã¾ã£ã¦ãã¾ãã
ä¾ã®ããã« .tf
ãã¡ã¤ã«ã«ç´æ¥ password
ãæ¸ãã®ã¯ææªã§ãããä»ã®åé¿çï¼Variablesãå©ç¨ã㦠apply
æã«ã¿ã¼ããã«ã§æå®ãããSecrets Manager ã·ã¼ã¯ã¬ããã«å
ã«ç»é²ãã¦ãã Datasource ã§åå¾ãããetcï¼ãã¨ã£ãã¨ããã§ã©ããã¦ã tfstate
ãã¡ã¤ã«ã«è¨é²ããã¦ãã¾ãã®ã¯åé¡ã§ããã
manage_master_user_password ã使ã£ãå ´å
password
å±æ§ãåé¤ããmanage_master_user_password
å±æ§ã« true
ãè¨å®ãã¾ãã
resource "aws_db_instance" "my-db-2" { allocated_storage = 10 engine = "mysql" engine_version = "5.7" instance_class = "db.t3.micro" username = "master" # manage_master_user_password ãå©ç¨ãã manage_master_user_password = true vpc_security_group_ids = [aws_security_group.db.id] skip_final_snapshot = true db_subnet_group_name = "db-subnet-group" }
ãããã¤ããã¨ãSecrets Manager ã«ã·ã¼ã¯ã¬ãããä½æããã¦ãããã¨ããããã¾ãã
ã·ã¼ã¯ã¬ããã®ãã¼ã«ã¯ username
㨠password
ãç»é²ããã¦ãããpassword
㯠RDS ãçæããã©ã³ãã ãªå¤ã«ãªã£ã¦ãã¾ãããã¾ã 7æ¥éã®ééã§ã·ã¼ã¯ã¬ãããã¼ãã¼ã·ã§ã³ãè¨å®ããã¦ãã¾ããã
ãããã¤å¾ã® terraform.tfstate
ãã¡ã¤ã«ã§ã¯ãpassword
㯠null
ã¨ãªã£ã¦ãããå®éã®ãã¹ã¯ã¼ãæååã¯ã©ãã«ãè¨é²ããã¦ãã¾ããã
aws_rds_cluster ã«ãå©ç¨ã§ãã
aws_rds_cluster
ãªã½ã¼ã¹ã«å¯¾ãã¦ããå¾æ¥ã® master_password
ã®ä»£ããã« manage_master_user_password
ãå©ç¨ã§ããããã«ãªã£ã¦ãã¾ãã
KMSãã¼ãæå®ãã
master_user_secret_kms_key_id
å±æ§ãå©ç¨ãããã¨ã§ãããã©ã«ããã¼ã§ã¯ãªãæå®ãã KMS ãã¼ã§ã·ã¼ã¯ã¬ããã®æå·åããããã¨ãã§ãã¾ãã
ï¼åèï¼
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/db_instance#managed-master-passwords-via-secrets-manager-specific-kms-key
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/rds_cluster#rdsaurora-managed-master-passwords-via-secrets-manager-specific-kms-key
æ¢åã® Secrets Manager ã·ã¼ã¯ã¬ãããå©ç¨ããå ´å
ä»åã¯è©¦ãã¦ãã¾ãããã master_user_secret
ã®è¨å®ãããã¯ã使ãã¨ãæ¢åã® Secrets Manager ã·ã¼ã¯ã¬ããããã¹ã¿ã¼ã¦ã¼ã¶ã¼ã®ãã¹ã¯ã¼ãã«ã§ããããã§ãã
ï¼åèï¼
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/db_instance#master_user_secret
æ¢åã®DBã«ä½¿ã£ããã©ããªãã
æ¢ã« password
ã master_password
ã使ã£ã¦ä½æãã DB ã«å¯¾ãã¦ãmanage_master_user_password
ãå©ç¨ããæ¹æ³ã«åãæ¿ãããã¨ã¯å¯è½ã§ããDB ã¯åä½æããã¾ãããããã¹ã¿ã¼ã¦ã¼ã¶ã¼ã®ãã¹ã¯ã¼ãã¯æ°è¦ã«ä½æããï¼ç¾å¨ã®ãã¹ã¯ã¼ãã¯å¼ãç¶ãããã«ï¼ Secrets Manager ã«æ°ããã·ã¼ã¯ã¬ããã¨ãã¦ä¿åãããããã§ããæ¢åã® DB 㧠manage_master_user_password
ã使ãæ¹æ³ã«åãæ¿ããå ´åã¯ãã¢ããªã±ã¼ã·ã§ã³ã®åä½ãéç¨ã«å½±é¿ããªããæ
éã«ç¢ºèªãã¾ãããã
ã¹ãããã·ã§ãããããªã¹ãã¢ããæã®æå
manage_master_user_password
ãå©ç¨ãã¦ä½æãã DB ã®ã¹ãããã·ã§ãããããªã¹ãã¢ããå ´åããã¹ã¿ã¼ã¦ã¼ã¶ã¼ã®ãã¹ã¯ã¼ãã¯å°ãç¹æ®ãªç¶æ
ã«ãªãããã§ãã次ã®ããã« manage_master_user_password = true
ã使ã£ã¦ã¹ãããã·ã§ãããã DB ã¤ã³ã¹ã¿ã³ã¹ãä½æãã¦ããSecrets Manager ã«ãã¹ã¯ã¼ãã¯çµ±åããããRDS ããã¹ã¯ã¼ãã管çãã¦ããç¶æ
㧠DB ã復å
ãã¾ããããã¹ã¯ã¼ãã¯ã¹ãããã·ã§ããåå¾æç¹ã®ãã®ã§ããã
data "aws_db_snapshot" "my-snapshot" { db_snapshot_identifier = "snapshot1" } resource "aws_db_instance" "my-db-from-snapshot" { allocated_storage = 10 engine = "mysql" engine_version = "5.7" instance_class = "db.t3.micro" username = "master" # snapshot_identifier ãå©ç¨ãã¦ãªã¹ãã¢ããæã # manage_master_user_password å±æ§ã使ã£ã¦ã # ãã¹ã¯ã¼ãã Secrets Manager ã§ç®¡çãããªãã manage_master_user_password = true skip_final_snapshot = true db_subnet_group_name = "db-subnet-group" # ã¹ãããã·ã§ãããã復å ãã snapshot_identifier = "${data.aws_db_snapshot.my-snapshot.id}" }
ãã®ç¾è±¡ã¯ GitHub ã® Issue ã«ãå ±åããã¦ãããè¨äºå·çæç¹ã§ã¯æªè§£æ±ºã§ãããã ã tfstate
ãã¡ã¤ã«ã«è¨é²ããã password
㯠null
ã ã£ããããTerraform AWS Provider ã®ãã°ã¨ãããã㯠RDS ã®å¶ç´ãªã®ããããã¾ããã
ãªã¹ãã¢ãã DB ã®ãã¹ã¯ã¼ãã Secrets Manager ã§ç®¡çããããã«ã¯ãä»ã®ã¨ããã®æ¬¡ã®ã¯ã¼ã¯ã¢ã©ã¦ã³ããè¯ãããã§ãã
manage_master_user_password
ãä»ããã« Terraform ã§ã¹ãããã·ã§ããããªã¹ãã¢ï¼apply
ï¼- ã¹ãããã·ã§ããåå¾æç¹ã®ãã¹ã¯ã¼ãããRDS ã管çãã¦ããç¶æ ã§ãªã¹ãã¢ããã
manage_master_user_password = true
ãä»ãã¦ããä¸åº¦apply
ãã- ãã¹ã¿ã¼ã¦ã¼ã¶ã¼ã®ãã¹ã¯ã¼ãã¯æ°è¦ã«ä½æãã㦠Secrets Manager ã«ä¿åããã
data "aws_db_snapshot" "my-snapshot" { db_snapshot_identifier = "snapshot1" } resource "aws_db_instance" "my-db-from-snapshot" { allocated_storage = 10 engine = "mysql" engine_version = "5.7" instance_class = "db.t3.micro" username = "master" # ãªã¹ãã¢æ㯠manage_master_user_password ãä»ããã« apply # ãªã¹ãã¢å¾ manage_master_user_password ãä»ãã¦å度 apply # manage_master_user_password = true skip_final_snapshot = true db_subnet_group_name = "db-subnet-group" snapshot_identifier = "${data.aws_db_snapshot.my-snapshot.id}" }
ãããã«
Terraform ã«ãã㦠tfstate
ãã¡ã¤ã«ã«å¹³æã® DB ãã¹ã¯ã¼ããæ®ã£ã¦ãã¾ãåé¡ãç°¡åã«è§£æ¶ãã manage_master_user_password
å±æ§ã«ã¤ãã¦ãç´¹ä»ãã¾ãããä»å¾æ°ãã« Terraform 㧠RDS ã¤ã³ã¹ã¿ã³ã¹/ã¯ã©ã¹ã¿ã¼ãä½ãæã«ã¯ããã²å©ç¨ãæ¤è¨ãã¾ãããããã ãã¹ãããã·ã§ãããããªã¹ãã¢ããæã®åãã¯ï¼è¨äºå·çæç¹ã§ï¼å°ãç¹æ®ãªã®ã§è¦æ³¨æã§ãã
ãèªã¿ããã ãã¦ãããã¨ããããã¾ããã
ç§ãã¡ã¯åããã¼ã ã§åãã¦ããã仲éã大åéãã¦ãã¾ãï¼ããããã®ãå¿åããå¾ ã¡ãã¦ãã¾ãã
ã»ãã¥ãªãã£ã¨ã³ã¸ãã¢å·çï¼@kou.kinyoãã¬ãã¥ã¼ï¼å¯ºå±± è¼ (@terayama.akira)
ï¼Shodoã§å·çããã¾ããï¼