ããã«ã¡ã¯ãSRE ã® @chaspy ã§ãã
ç¾å¨ãæ°ãµã¼ãã¹ãä½æããéããã®ãã¼ã¿ãã¼ã¹ã«ã¯ AWS Aurora(Postgres) ã使ç¨ãã¦ãã¾ãã
ãã®ä½æã«ã¯ Terraform ãç¨ãã¦ãããããã¤ãé©å½ãªå¼æ°ãå ¥ãã㨠Terraform ã®ã³ã¼ããèªåçæãããä»çµã¿ãç¨æãã¦ãã¾ãã
ããã§ç¤ç³...ã¨æã£ã1å¹´åã®ç§ã§ããããã®ä»çµã¿ã«ã¯ããã¤ãåé¡ç¹ãããã¾ãããæ¬è¨äºã§ã¯ãã®åé¡ç¹ã¨ãããæ¹åãã話ããã¾ãã
æ¢åã®ä»çµã¿ã®åé¡ç¹
以ä¸ã大ãã2ã¤ã®åé¡ç¹ãããã¾ããã
éçºç°å¢ç¨ã®ãã¼ã¿ãã¼ã¹ãæ¬çªç°å¢ç¨ã®ãã¼ã¿ãã¼ã¹ã®ã¹ãããã·ã§ãããããªã¹ãã¢ããããã¨ãèæ ®ããã¦ããªã
éçºç°å¢ç¨ã®ãã¼ã¿ãã¼ã¹ã«ã¯2種é¡ããã¾ãã
- Terraform ã§ä½æããããã®å¾ãæ¬çªç°å¢ç¨ã®ãã¼ã¿ãã¼ã¹ã®ã¹ãããã·ã§ãããããªã¹ãã¢ãããªã "注é: æ¬çªç°å¢ç¨ã®ãã¼ã¿ãã¼ã¹ã«ã¯å人æ å ±ãå«ã¾ããå ´åãããã®ã§ããªã¹ãã¢æã«ã¯ãããã®æ å ±ããã¹ã¯ãã¦ãã¾ã"
- script ã«ãã£ã¦æ¬çªç°å¢ç¨ã®ãã¼ã¿ãã¼ã¹ã®ã¹ãããã·ã§ãããããªã¹ãã¢ããã
æ¬çªç°å¢ç¨ã®ãã¼ã¿ãã¼ã¹ã®ã¹ãããã·ã§ãããããªã¹ãã¢ãããã¨ãããã¨ã¯å½ç¶å ã«æ¬çªç¨ãã¼ã¿ãã¼ã¹ãå¿ è¦ã§ãããããç¹ã«ä½ãæ示ããªããã°é常ãéçºç°å¢ç¨ã®ãã¼ã¿ãã¼ã¹ããå ã«ä½æããã§ãããã
ãã®ããã«ã¦ã¼ã¹ã±ã¼ã¹ã¨éç¨ããã¼ãååã«èæ ®ããã¦ããªãã£ããããTerraform ã§éçºç°å¢ç¨ã®ãã¼ã¿ãã¼ã¹ãä½æãããããã¨ã§ãªã¹ãã¢ããã£ãã»ãã便å©ã ã¨æ°ã¥ããscript ã§ãªã¹ãã¢ãè¡ãããTerraform ã§ä½æãã¦ããããäºéå®ç¾©ã«ãªãã1度åé¤ãããããªã©ç ©éãªæéãçºçãã¦ãã¾ããã
ãã®ç¹ã¯ãã¨ãã¨ãã£ãããã¥ã¡ã³ãã« Overview 㨠Example Workflow ã®ã»ã¯ã·ã§ã³ã追è¨ãããã¨ã§æ´çãã¾ããã以ä¸ãå®éã®ããã¥ã¡ã³ãã§ãã社å åºæã®å称ã¯é©å½ãªä¸è¬çãªååã«å¤æ´ãã¦ãã¾ãã
Overview
There are several ways to create it, depending on your requirements.
Production Database
Create with Terraform.
Staging Database
If you want to make the data persistent
Create with Terraform
If you want to create from a snapshot of Production Database
Use Restore Script.
Example Workflow
If you want Database for these environments: - production - support (restored from production snapshot) - develop (restored from production snapshot) - qa (NOT restored from production snapshot)
production and support and develop
- Create a Production Database by Terraform
- Create an application role by Jenkins Job
- Set restoring script for support and develop
- Set database password to aws-secret-manager
- For production, use artifact by the Jenkins Job and set it as vault
- For develop and edge (and staging), set database password as text
qa
- Create qa Database by Terraform
- Create an application role by Jenkins Job
ãã®çµé¨ãããåã«èªååã®ä»çµã¿ãæä¾ããã ãã§ã¯ä¸ååã§ãå©ç¨è ã®ã¦ã¼ã¹ã±ã¼ã¹ããã¢ãªã³ã°ãã¹ãã§ãã£ãã¨å¦ã³ã¾ããã
æ¬çªç°å¢ç¨ã®ãã¼ã¿ãã¼ã¹ã® Password ãè¨å®ããããã« SRE ã®ä½æ¥ãå¿ è¦
Terraform ã®ã³ã¼ããç°¡åã«çæã§ããããã«ãªã£ããããã§åã£ã - ããæã£ã¦ãã1å¹´åã®èªåãæªçã ã¨æ°ã¥ãããã¨ã¯ãæé·ããã¨ãè¨ããããããã¾ããã
ãããããªã RDS ã®ã»ã«ããµã¼ãã¹åããããã£ããã¨ããã¨ãæè¿ã® SRE Team ã®ããã·ã§ã³ - èªå·±å®çµå - ã®ããã« SRE ã®æãä»ããã«ã¯ã©ã¦ããªã½ã¼ã¹ã Developer èªèº«ã§ä½ããä»çµã¿ãå¿ è¦ã ã£ãããã§ããç¹ã« RDS ã¯æ°ãµã¼ãã¹ä½ææã«ã¯ããªãã®å ´åã§å¿ è¦ã«ãªãã¾ãã
ããããå®éã«ãã£ã¦ã¿ãã¨ã以ä¸ã®2ã¤ã®å ´é¢ã§ SRE ã®æä½æ¥ãå¿ è¦ã§ããã
- æ¬çªç°å¢ç¨ã®ãã¼ã¿ãã¼ã¹ãä½æããéã«ãmaster user ç¨ã®ãã¹ã¯ã¼ããçæãã¦æå·åãã
- æ¬çªç°å¢ç¨ã®ãã¼ã¿ãã¼ã¹ãä½æå¾ãmaster user ã¨å¥ã«ãã¢ããªã±ã¼ã·ã§ã³ã使ç¨ãã role ãçæãã
æ¬çªç°å¢ç¨ã®ãã¼ã¿ãã¼ã¹ãä½æããéã«ãmaster password ãçæãã¦æå·åãã
AWS 㧠RDS(Aurora) ãä½æããã¨ãã¯ãåæç¶æ ã§çæããã master user ã®ãã¹ã¯ã¼ããè¨å®ããå¿ è¦ãããã¾ãã
\ Terraform ã§ä½æããéããæ¬çªç°å¢ã®ãã¼ã¿ãã¼ã¹ãã¹ã¯ã¼ããã³ã¼ãã«æ¸ãããã«ããããªããããSRE ããã£ãã強度ã®é«ããã¹ã¯ã¼ããçæããCircleCI ã®ç°å¢å¤æ°ã«è¨å®ãããã®ã使ç¨ãã¦ãã¾ãã(注é:CircleCI ã®ç°å¢å¤æ°ã¯èªä½ã®ãã¼ã«ãç¨ãã¦ã³ã¼ã管çããã¦ãããansible vault ã使ã£ã¦æå·åãã¦ãã¾ã)
ãã®ã¾ã¾ã§ã¯ããã Terraform ã®ã³ã¼ãçæãèªååããã¨ãã¦ããç°å¢å¤æ°ã®çæã SRE ãè¡ããªããã°ãªãããå®å ¨ãªã»ã«ããµã¼ãã¹åã«ã¯ãªãã¾ããã
ãã®åé¡ã«å¯¾ãã¦ã¯ãããããã master user ã®ãã¹ã¯ã¼ããä¿ç®¡ããªãããã¨ããçµè«ã«éãã¾ããã
Terraform ã®ã³ã¼ãã«ã¯é©å½ãªå¤ã§å¹³æã§ä½æå¾ãå¾è¿°ããä½æå¾ã«ã¢ããªã±ã¼ã·ã§ã³ã使ç¨ãã role ãä½æããéã«ãã¹ã¯ã¼ããæå·å¼·åº¦ã®é«ãå¥ã®å¤ã«å¤æ´ããã©ãã«ãä¿åãããªããã¨ããæ¹æ³ãåãã¾ããã
ããããRDS(Aurora) ã® master user ã®ãã¹ã¯ã¼ã㯠AWS ã® RDS(Aurora) ã«å¯¾ããå¤æ´æ¨©éãããã°ãcli(aws rds modify-db-cluster) çã使ã£ã¦å®¹æã«å¤æ´å¯è½ã§ãããã®ãããæ¨æ¸¬ã®é£ããå¤ãã»ãããã¦ããã¦ãå¿ è¦ãªæã«å¤æ´ããã°ååã ã¨å¤æãã¾ããã
æ¬çªç°å¢ç¨ã®ãã¼ã¿ãã¼ã¹ãä½æå¾ãmaster user ã¨å¥ã«ãã¢ããªã±ã¼ã·ã§ã³ã使ç¨ãã role ãçæãã
ãã¼ã¿ãã¼ã¹ã®ä½æå¾ã権éãé©åã«çµãããã«ãmaster user ã¨ã¯å¥ã«ãã¢ããªã±ã¼ã·ã§ã³ããæ¥ç¶ããããã®ããã¼ã¿ãã¼ã¹ã®ãªã¼ãã¼ã¨ãªã role ãä½æããå¿ è¦ãããã¾ããå ·ä½çã«ã¯ä»¥ä¸ã®ã³ãã³ããæã£ã¦ããã ãã§ããï¼Aurora Postgres ã®å ´åï¼
# ç°å¢å¤æ°ã¯ãã®ååã®éãã®ãã®ãå ¥ãã¾ã CREATE ROLE ${APP_USERNAME} WITH LOGIN PASSWORD '${APP_PASSWORD}'; ALTER DATABASE ${DATABASE_NAME} OWNER TO ${APP_USERNAME};
ããã¯åãªãã³ãã³ãã§ãã®ã§ã対象㮠RDS ã«æ¥ç¶ã§ããå ´æããå¦çãèªååã§ãã¾ãã
ããã§ãåè¿°ãã master user ã®ãã¹ã¯ã¼ãã®å¤æ´ãå«ããã以ä¸ã®å¦çãè¡ã Jenkins Job(注é: Jenkins 㯠Production ã® VPC ã«æ¥ç¶ã§ããå¥ã® VPC ã« EC2 ã¤ã³ã¹ã¿ã³ã¹ã¨ãã¦åå¨ãã¾ãï¼ãä½æãã¾ããã
- ã¢ããªã±ã¼ã·ã§ã³ãæ¥ç¶ããããã® role ãä½æ
- ä¸è¨ role ã®ãã¹ã¯ã¼ããæå·åããJob ã® Artifact å
- master user ã®ãã¹ã¯ã¼ããæ´æ°
ãã® Jenkins Job ã Terraform ã«ããæ¬çªç°å¢ç¨ã®ãã¼ã¿ãã¼ã¹ä½æå¾ã« Developer ã«å©ãã¦ããããã¨ã§ãSRE ã®æä½æ¥ãï¼ããããï¼å®å ¨ã«æé¤ã§ãã¾ãã
工夫ããç¹
ä»åã®å¤æ´ã§æèãããã¨ã¯ä»¥ä¸ã®2ç¹ã§ãã
- ã²ã¨ã¤ã®ãã¨ããã¾ãããï¼UNIX å²å¦ï¼
- æ¢åã®éç¨ãå¯è½ãªéãå¤ããªã
ã²ã¨ã¤ã®ãã¨ããã¾ãããï¼UNIX å²å¦ï¼
èªååã¨ãã£ã¦ãããã®å¯¾è±¡ã¯ AWS - RDS/Aurora, PostgreSQL, AWS Secret Manager ã¨è¤æ°ã®ãªã½ã¼ã¹ãé¢ä¿ãã¦ãããèªååã®æ段ã»ã¯ã¼ã¯ããã¼ãæ§ã ã§ãã
ä»åã®ä¾ã§ã¯ããã¼ã¿ãã¼ã¹ã®ä½æï¼Terraformï¼ãPostgresSQL ä¸ã® role ä½æï¼Jenkins Jobï¼ãã¢ããªã±ã¼ã·ã§ã³ãã使ãããã« AWS Secret Manager ã¸ã®ãã¹ã¯ã¼ãã®æ ¼ç´ã¨ãã£ããã¨ãããããã¾ãã
ãã®ãããä»åã¯ä¸è¨ã®3ã¤ã®ãªã½ã¼ã¹ã«å¯¾ãã¦ããããèªååãå¥ã«ãããã¨ã§ãåä¸ã®ã¸ã§ãã§ã¯åä¸ã®ãã¨ãè¡ããã¨ã«ããã¯ã¼ã¯ããã¼ããã£ãããã¾ããã
- Terraform ã«ãããã¼ã¿ãã¼ã¹ã®ä½æ
- Jenkins Job ã«ãã PostgreSQL ã§ã® role ä½æ
- AWS Secret Manager ã¸ã®ã¢ããªã±ã¼ã·ã§ã³ç¨ã®ãã¹ã¯ã¼ãã®æ ¼ç´
仮㫠Terraform ã«ãããã¼ã¿ãã¼ã¹ã®ä½æãããªã¬ã¼ã«ãã¢ããªã±ã¼ã·ã§ã³ç¨ã®ãã¹ã¯ã¼ãã®æ ¼ç´ã¾ã§èªååãããã¨ãã§ããããããã¾ããããããããã®å¦çã«æ¸¡ãããã®æ å ±ãå¤ããªã£ã¦ãã¾ããè¤éãªããã°ã©ãã³ã°ãå¿ è¦ã«ãªã£ã¦ãã¾ãã¾ãã
ä»å㯠Jenkins Job ã«ãã PostgreSQL ã§ã® role ä½æ
ã ããè¡ããã¨ã«ããansible-vault ã§æå·åãããã¹ã¯ã¼ãã Job ã® Artifact ã¨ãã¦åãåºããAWS Secret Manager ã¸ã®æ ¼ç´ã¯å¯¾è±¡ã®ãªãã¸ããªã«äººéãæä½æ¥ã§ãã«ãªã¯ã¨ã¹ããéãå¿
è¦ãããã¾ãã確ãã«æéã¯å¢ããã®ã§ãããåå¦çãççµåã«ãªãå¦çãã·ã³ãã«ã«ãªãã®ã§ãç¾å¨ã®é »åº¦ãèããã¨ãã®ãããã®æä½æ¥ã¯åå許容ã§ããã¨å¤æãã¾ããã
æ¢åã®éç¨ãå¯è½ãªéãå¤ããªã
åè¿°ããããã«ãæ¢ã« Terraform ã§ã®ãã¼ã¿ãã¼ã¹ä½æ
㨠AWS Secret Manager ã¸ã®ã¢ããªã±ã¼ã·ã§ã³ç¨ã®ãã¹ã¯ã¼ãã®æ ¼ç´
ã«é¢ãã¦ã¯æ¢åã®ä»çµã¿ãåå¨ãã¾ããéç¨ãå¤ãããã¨ã¯ãå©ç¨è
ã«å¦ç¿ã³ã¹ããå¼·ãã¦ãã¾ããã¨ã«ä»ãªãã¾ããã
æ¢åã®éç¨ããå¤åããé¨åãå°ãªããã°ããã¼ã ã¡ã³ãã¼ããã®ç解ãå¾ãããã失æããã¨ãã®åãæ»ãã容æã¨ãªãã¾ãã
ãããã«
ããããã¦èãã¦ã¿ãã°æåããã§ãããã¨ããããã¾ãããããã£ã¦ã¿ãã°ç°¡åã§ãã£ã¨ã¯ããã§ãããã¨ããããã¾ããããã®ç¹ã¯éå»ã®èªåã®è³ããªãç¹ã ã¨åçãã¤ã¤ãä»çµã¿ãä½ãã¨ãã¯å®éã«ä½¿ç¨è ã®ç«å ´ã§ã®æ¤è¨¼ããã¦ã¼ã¹ã±ã¼ã¹ã®ãã¢ãªã³ã°ãéè¦ã§ãããã¨ããèªååãè¡ãã¨ãã¯å¿ è¦ã¨ãªãé »åº¦ã¨çç¥ã§ããæéï¼ã¡ãªããï¼ãèæ ®ããä¿å®æ§ã®ããã«ççµåã«è¨è¨ããã»ããè¯ãã¨ãã£ããã¨ãå¦ã¶ãã¨ãã§ãã¾ããã
ãããèªååãããã©èªååããã®åæãæ´ãè¯ãäºä¾ã§ãã£ãã¨æãã¾ãã
Quipper ã§ã¯ä¸çã®æã¦ã¾ã§å¦ã³ãå±ããã仲éãåéãã¦ãã¾ã