ã¯ããã«
ä»å¹´åº¦åãã¦AWS CDKã«ãããã¼ã éçºãè¡ã£ã¦ã¿ãããã®éã«æ¡ç¨ããã½ã¼ã¹ã³ã¼ãã®ããã¸ã§ã¯ã管çæ¹æ³ãããããã¤ã®ããã¼ã«ã¤ãã¦èãããã¨å«ãæ´çã¨ãã¦ã¾ã¨ãã¦ããã
â»ãããæé©ã ã¨ã¯æã£ã¦ããªãã®ã§ãã¤ãã¤ããªç¹ãªã©ããã°æè¦ããããã¨å¬ããã§ãã
ãªããèãããã¨ã以ä¸ã®æ¸ç±ã§è¨èªåããã¦ããç®æããã£ãã®ã§ãå¿ è¦ã«å¿ãã¦å¼ç¨ããã¦ããã ãï¼ãªã©ã¤ãªã¼ã®Infrastructure as Codeã®ç¬¬2çã2023/1ç¾å¨ã§ãæ®å¿µãªããé¦è¨³ã¯ç¬¬1çã®ã¿ãï¼ã
- ã¯ããã«
- åæ
- å°å ¥æ¤è¨æã®è©±ï¼ããããè¨èªã©ãããåé¡ï¼
- å ¨ä½å
- 1. CDKã®ããã¸ã§ã¯ã管ç
- 2. éçºã®ããã¼
- çµããã«
åæ
ä»åã®ä»çµã¿æ§ç¯ã«ããã£ã¦ã¯ä»¥ä¸ãåæã¨ãã¦ããï¼å¤åãããã話ï¼ã
- ã¤ã³ãã©ï¼AWSï¼ã®æ§ç¯ãè¡ãã¡ã³ããè¤æ°äººããã
- ãã¼ã å
¨ä½ã§ä½¿ç¨ããç°å¢ã¨ãã¦ã¯ä»¥ä¸ã®3ã¤ããã
- éçºç°å¢ï¼devï¼ï¼éçºç¨ã®è³æããããã¤ããç°å¢ã
- 試é¨ç°å¢ï¼stgï¼ï¼ä¸»ã«ãã¹ããæ¬çªãªãªã¼ã¹åã®è³æã®æ¤è¨¼ã«ä½¿ç¨ããç°å¢ã
- æ¬çªç°å¢ï¼prdï¼ï¼æ¬çªãªãªã¼ã¹æã®è³æããããã¤ããç°å¢ã
å°å ¥æ¤è¨æã®è©±ï¼ããããè¨èªã©ãããåé¡ï¼
ã¤ã³ãã©æ å½è ã¯ç§å«ãå ¨å¡TypeScriptæªçµé¨è ã§ããããããæ å ±éã®å¤ããªã©ãè¸ã¾ããã¨TypeScriptãè¯ãã¨èãæ¡ç¨ããã
çµæçã«ã¯è¯ãã£ãã¨æããããã°ã©ãã³ã°è¨èªã§æ¸ãã¨ã¯ãããé«æ©è½ãªè¨å®ãã¡ã¤ã«ãæ¸ãã¦ããæè¦ã«è¿ãã®ã§TypeScriptã触ã£ããã¨ããªãã¦ããªãã¨ããªãã
å ¨ä½å
åæãè¸ã¾ãä»åã®ãã¼ã éçºã§æ¡ç¨ãã¦ããããæ¹ã¯ä»¥ä¸ã§ããã
ãã¤ã³ãã¨ãã¦ã¯ä»¥ä¸2ç¹ã§ããã
- CDKã®ããã¸ã§ã¯ã管çï¼1ãªãã¸ããªã§ã³ã¼ãã管çãã¾ããã©ã³ãéç¨ã¯åºæ¬çã«mainã®ä¸æ¬ãã¤ã³ãã©ä½æ¥è ã¯åå¥ã«ãã©ã³ããåã£ã¦ä½æ¥ããä½æ¥ãçµãã£ããmainã«ãã¼ã¸ããã
- éçºã®ããã¼ï¼ã¤ã³ãã©ä½æ¥è
ãCDKã®éçºãè¡ãå ´åã¯ãã©ã³ããä½æããå°ç¨ã®ç°å¢ã«å¯¾ãã¦æåã§
cdk deploy
ã§ãããã¤ããã³æ¤è¨¼ãããä½æ¥å¾mainã«ãã¼ã¸ããã¨ããã¤ãã©ã¤ã³ãããªã¬ã¼ãdev, stg, prdã«èªåã§ãããã¤ããã
以ä¸ãããã詳細ãè¨è¼ããã
ãªããä»åè¨è¼ããæ¹å¼ã«ããCDKããã¸ã§ã¯ãã®ãµã³ãã«ã¯ä»¥ä¸ã«æ ¼ç´ãã¦ããã
1. CDKã®ããã¸ã§ã¯ã管ç
ã³ã¼ã管çããã¸ã§ã¯ãã«ã¤ãã¦
1ã¤ã®ã³ã¼ã管çããã¸ã§ã¯ãããè¤æ°ç°å¢ããããã¤ããå½¢ã¨ãã¦ãããå½ããåã ã1å 管çããæ¹ã管çã楽ã ããã§ããã
ããã¯åèæ¸ç±ã® Patterns for Building Environments
ã§ç´¹ä»ããã¦ãããã¿ã¼ã³ã®å
ãPattern: Reusable Stack
ã«ç¸å½ããã
A reusable stack is an infrastructure source code project that is used to create multiple instances of a stack. (p72)
ä¸è¨ãè¸ã¾ãã1ã¤ã®CDKããã¸ã§ã¯ããã以ä¸ã®ç°å¢ã«å¯¾ãã¦ãããã¤ãè¡ããããå®è£ ãã¦ããã
- ã¤ã³ãã©ä½æ¥è
å°ç¨ã®ç°å¢ï¼å³ã®
infraA
,infraB
ï¼ï¼ã¤ã³ãã©ä½æ¥è ï¼CDKéçºè ï¼ãããããå æããç°å¢ãCDKéçºä¸ã¯è¨å®å¤æ´ããªã½ã¼ã¹ã®çæã»ç ´å£ãªã©ãç¹°ãè¿ãããä½æ¥è åä½ã§ç°å¢ããããã¤å¯è½ã¨ããã - ãã¼ã å ¨ä½ã§ä½¿ç¨ããç°å¢ï¼dev, stg, prdï¼ï¼ã¢ããªéçºè ãªã©å«ããã¼ã å ¨ä½ã§ä½¿ãç°å¢ãCDKã®å®è£ å 容ãããç¨åº¦ãã£ãã¯ã¹ãã段éã§ãã¡ãã«åæ ããã
ãã ãããããå®ç¾ããä¸ã§ã¯ç°å¢ãã¨ã®ãã©ã¡ã¼ã¿ãåãæ¿ããæ¹å¼ãå¿ è¦ã§ããããã®æ¹æ³ã¯å¾è¿°ããã
ãããã¤å ã®ç°å¢ã®åãæ¿ãã«ã¤ãã¦
ãã©ã®ç°å¢ã«å¯¾ãã¦ãããã¤ããããã¯ãcdkã³ãã³ãå®è¡æã«contextã§æå®ãããç°å¢å¤æ°ããåå¾ãçãå¤ãã¨æãã ä»åã¯åè ãæ¡ç¨ããã
å ·ä½çã«ã¯Appã®å®è£ ä¸ä»¥ä¸ã®ããã«contextå¤ãåå¾ããããã«ãã¦ãã
const app = new cdk.App(); // ãããã¤å ã®ç°å¢ã¯contextããåå¾ãã const envType = app.node.tryGetContext('env');
ã³ãã³ãå®è¡æã«ä»¥ä¸ã®ããã«æå®ããã
cdk deploy -c env=dev
ãã®ä¸ã§ã¹ã¿ãã¯çææã«ãããã¤å ã®ç°å¢åãå¼ã渡ããç°å¢åã«ããå種ãã©ã¡ã¼ã¿ãåãæ¿ããããããã«ãã¦ãã
ã¾ãstackName
ã«ããã¦contextã§æå®ããç°å¢åãä»ä¸ãããã¨ã§ãç°å¢éã§ã¹ã¿ãã¯åã競åããªãããã«ãã¦ããã
// ã¹ã¿ãã¯ãä½æ new AppStack(app, 'CDKPipelinesStack', { stackName: `${envType}-App-Stack`, // Stackåã«ç°å¢åãå«ãéè¤é²æ¢ env: envConfig, // ã¢ã«ã¦ã³ãIDããªã¼ã¸ã§ã³ãè¨å® terminationProtection: true, // Stackã®åé¤ä¿è·ãæå¹å envType: envType // contextãStackã«å¼ãç¶ã })
ãã©ã¡ã¼ã¿ã®åãæ¿ãæ¹æ³ã«ã¤ãã¦
Appçµç±ã§å¼ãç¶ãããcontextã®ãããã¤å ç°å¢åãå ã«ãç°å¢ãã¨ã®ãã©ã¡ã¼ã¿ã®åãæ¿ããè¡ãæ¹å¼ã¨ãã¦ããã
ããã®åãæ¿ãæ¹å¼ãcdk.json
ã使ããç°å¢å¤æ°ã使ãããªãã¸ã§ã¯ãã使ãçãããããä»åã¯ãªãã¸ã§ã¯ãã使ãæ¹å¼ãæ¡ç¨ãããçç±ã¨ãã¦ã¯TypeScriptã®ç¹æ§ãæ´»ããã¦åå®å
¨ã§éçºãã§ããããã§ããã
å ·ä½çã«ã¯ä»¥ä¸è¨äºã®configãã¡ã¤ã«ãä½æããç°å¢ã«å¿ããè¨å®å¤ãåå¾ã§ããé¢æ°ã使ç¨ããããæ¹ãåèã«ããã¦ããã ããã
æç« ã ãã ã¨ãããã¥ããããå®è£ ä¾ãåèã«è©³ç´°ãè¨è¼ããã
. âââ appStack.ts âââ config â âââ vpcConstructConfig.ts âââ constructs âââ vpcConstruct.ts
å®è£
ä¾ã®ããã¸ã§ã¯ãã§ã¯VPCã«é¢ããå®ç¾©ãã³ã³ã¹ãã©ã¯ãã¨ãã¦ä½æããããã«å¯¾å¿ããè¨å®ãã¡ã¤ã«ãconfig
é
ä¸ã«ä½æãã¦ãããããã§ã¯VPCã®ã¿ã ãå®ããã¸ã§ã¯ãã§ã¯è¤æ°ã®ã³ã³ã¹ãã©ã¯ããã¡ã¤ã«ã¨è¨å®ãã¡ã¤ã«ãä½æããå½¢ã¨ãªãã
è¨å®å¤ã¯ã³ã³ã¹ãã©ã¯ããã¡ã¤ã«ã®ä¸ã«å®ç¾©ãã¦ãè¯ãããã³ã³ã¹ãã©ã¯ãã®å®è£
ãé·ããªãã±ã¼ã¹ãããã®ã§ãè¨å®ã¯config
é
ä¸ã«å¥ã ããåç°å¢ã®è¨å®å¤ãä¸ç®ã§ãããï¼å¤æ´ãããå ´åãconfig
é
ä¸ã®ã¿ãããã°è¯ãã®ã§çµ±å¶ãã¨ãããããããã®å½¢ã¨ããã
â»ã³ã³ã¹ãã©ã¯ãã«ããæ§é åã¯ä»¥ä¸ãåèã«ãªãã
â è¨å®ãã¡ã¤ã«ã®å®è£
以ä¸ã®ããã«è¨å®å¤ã®åãå®ç¾©ããæå®ããç°å¢ã®è¨å®å¤ãè¿ãé¢æ°ãå®è£ ãã¦ããã®ã¿ã
é¢æ°ã«ãããswitchæã§ç°å¢ãã¨ã«åå²ãã¦ãããinfraA
ãinfraB
ã¯ã¤ã³ãã©ä½æ¥è
å°ç¨ã®ç°å¢ã§ãããä½æ¥è
ãå¢ããå ´åã¯ãããå¢ããã¦ããå½¢ã¨ãªãã
ãªããã¤ã³ãã©ä½æ¥è
å°ç¨ã®ç°å¢ã¯ããããã¤æã«ã¤ã³ãã©ä½æ¥è
ãæåã§contextå¤ãã¿ã¤ãï¼env=infraA
ãªã©ï¼ããå½¢ã§ããããåèæ¸ç±ã§ã¯æåã§ãã©ã¡ã¼ã¿ãä¸ããã®ã¯Manual Stack Parameters
ï¼p81ï¼ã¨ãã¦ã¢ã³ããã¿ã¼ã³ã§ããã¨è¨è¼ããã¦ããï¼çç±ã¨ãã¦ã¯æåã ã¨ãªããã¹ãçãå¾ãããï¼ã
ãããã¤ã³ãã©ä½æ¥è ç¨ã®ç°å¢ã¯æåãããã¤ã¨ãããã£ãï¼çç±ã¯å¾è¿°ï¼ã®ãããåå¨ããªãç°å¢ãcontextå¤ã§æå®ããå ´åã¯ã¨ã©ã¼ã¨ããããå®è£ ãã¦ããã
// åãå®ç¾© export type VpcConstructConfig = { cidr: string, // VPCã«å²ãå½ã¦ãcidr maxAzs: number // VPCã®æ大AZæ° } // ç°å¢åãå¼æ°ã¨ããæå®ããç°å¢ã®è¨å®å¤ãè¿ãé¢æ°ãå®è£ // VPCã®IPã¬ã³ã¸ã«ã¤ãã¦ããã¼ã å ¨ä½ã§ä½¿ãdevãªã©ã¯ã¯ã©ã¹Aãã¤ã³ãã©ä½æ¥è ã使ãç°å¢ã¯ã¯ã©ã¹Bãã¨ããããã«åºåã«ãã£ã¦åããããã«ãã¦ãã export function getVpcConstructConfig(envType: string): VpcConstructConfig { switch (envType) { case 'dev': return { cidr: '10.0.0.0/16', maxAzs: 2 } case 'stg': return { cidr: '10.1.0.0/16', maxAzs: 2 } case 'prd': return { cidr: '10.2.0.0/16', maxAzs: 2 } case 'infraA': return { cidr: '172.16.0.0/16', maxAzs: 1 } case 'infraB': return { cidr: '172.16.1.0/16', maxAzs: 1 } // åå¨ããªãç°å¢åãæå®ããå ´åã¯ã¨ã©ã¼ default: throw new Error( `The VPC config in "${envType}" environment does not exist.` ) } }
â ã³ã³ã¹ãã©ã¯ãã®å®è£
ä¸è¨ãå ã«å¯¾å¿ããconstructã«ããã¦ãè¨å®å¤ãåå¾ãã¦è©²å½ç®æã«è¨å®ããã å ·ä½çã«ã¯ä»¥ä¸ã®ãããªå½¢ã§ããã
// ----------------------- Config ------------------------------ // ç°å¢åãconfigã§å®ç¾©ããé¢æ°ã«æ¸¡ããè¨å®å¤ãåå¾ãã const vpcConfig: VpcConstructConfig = getVpcConstructConfig(props.envType) // ----------------------- VPC ------------------------------ // åå¾ããè¨å®å¤ãå ã«ãªã½ã¼ã¹ãå®ç¾©ããã new ec2.Vpc(scope, 'Vpc', { ipAddresses: ec2.IpAddresses.cidr(vpcConfig.cidr), // è¨å®å¤ããåæ maxAzs: vpcConfig.maxAzs, // è¨å®å¤ããåæ natGateways: 0, vpcName: `${props.envType}-vpc` } )
ä¾ã§ã¯ ipAddresses
ã¨maxAzs
ãç°å¢ãã¨ã§åãæ¿ããå½¢ã¨ãã¦ãããè¨å®å¤ãåã§å®ç¾©ãã¦ããããåå®å
¨ã§éçºãè¡ããã¨ãã§ããIDEã«ããè£å®ãå¹ãã誤ã£ãé
ç®ãåç
§ãã¦ããå ´åã¯ã¨ã©ã¼ãåºããªã©æ©æµãåããããã
â Stackã®å®è£
ã³ã³ã¹ãã©ã¯ããä½æãããstackããnewããã®ã¿ã§ããã
// StackPropsãæ¡å¼µãAppããenvTypeãæ³¨å ¥ interface AppStackProps extends StackProps { envType: string } export class AppStack extends Stack { constructor(scope: Construct, id: string, props: AppStackProps) { super(scope, id, props); // VPCãçæ new VpcConstruct(this, 'VpcConstruct', { envType: props.envType }) } }
2. éçºã®ããã¼
大ããåããã¨ãã¤ã³ãã©ä½æ¥è ã®éçºããããã¤ãã¨ããã¼ã å ¨ä½ã®ç°å¢ã¸ã®ãããã¤ãã®2ã¤ã«åããã
ã¤ã³ãã©ä½æ¥è ã®éçºããããã¤
以ä¸ã®ããã«ã¤ã³ãã©ä½æ¥è ã¯ããããå°ç¨ã®ç°å¢ãæã¡ãCDKéçºãå®æ½ããã
å ·ä½çãªæµãã¨ãã¦ã¯ä»¥ä¸ã§ãããCDKéçºæã®ãããã¤ã¾ã§ãã¤ãã©ã¤ã³çµç±ã«ããã¨ç ©ããããããæåã§ãããã¤ããå½¢ã¨ãã¦ããã
main
ããä½æ¥ç¨ã®ãã©ã³ããåãåºãã- 対象ã®ãã©ã³ãã«ããã¦éçºãè¡ããèªåå°ç¨ã®ç°å¢ã«æåã§ãããã¤ãè¡ãæ¤è¨¼ããã
- ãã¹ãå®è£
å«ãéçºãçµãã£ãã
main
ã«ãã¼ã¸ããã
ãã¼ã å ¨ä½ã®ç°å¢ã¸ã®ãããã¤
main
ã«ãã¼ã¸ãè¡ãã¨ãã¤ãã©ã¤ã³ãèªåã§ããªã¬ã¼ãããåç°å¢ï¼dev, stg, prdï¼ã«å¯¾ãã¦ãããã¤ãè¡ãããã
ãã¤ãã©ã¤ã³çµç±ã§ã®ãããã¤ã¨ãã¦ããã®ã¯ä»¥ä¸ã®ããã§ããã
- æåãããã¤ãæããããã®ã§ãªããã¹ãé²æ¢ã§ããã誤ã£ã¦
main
以å¤ã®ãã®ãé©ç¨ãã¦ãã¾ã£ããçãèµ·ãããªãã - ãã¤ãã©ã¤ã³ã«ãã¹ãã¹ãã¼ã¸ãæ¿èªã¹ãã¼ã¸ãè¨ãããã¨ã§ãä¸å®å ¨ãªè³æããããã¤ãããã®ãé²æ¢ã§ããã
- éçºè ã®æ¨©éãå¼±ãã§ãããã¨ã§ããã³ã³ä¸ã§ã®ãªããã¹ãé²æ¢ã§ãããç¹ã«æ¬çªç°å¢ã§ã¯ã¢ã«ã¦ã³ããã¨åé¢ãããã¤ãã©ã¤ã³çµç±ä»¥å¤ã§ã¯ãªã½ã¼ã¹ãä½æã§ããªãããã«ããçãå¯è½ã¨ãªãã
ãªãããã¤ãã©ã¤ã³ã¯ä»¥ä¸è¨äºã®ãã®ã使ç¨ãã¦ããã
ãã©ã³ãæ¦ç¥ã«ã¤ãã¦
CDKã®ã³ã¼ãã®ãã©ã³ãã¯main
ã®ã¿ãå®å¸¸çã«åå¨ããéç¨ã¨ãããéçºç¨ã®ãã©ã³ãã¯åºæ¬çã«çå½ã§ãããæ°æ¥åä½ã§ãã¼ã¸ãè¡ãï¼ãã©ã³ã¯ãã¼ã¹éçºã«è¿ãï¼ã
ä¸è¨ã®ããã«ããçç±ã¯ä»¥ä¸ã§ããã
- mainã®ã¿ã§åç°å¢åãã®è¨å®å¤åãæ¿ããè¡ããããå®è£
ãã¦ãããããä¾ãã°devã®è¨å®å¤ã ãå¤æ´ãããå ´åã§ãã
config
é ä¸ã§devã«è©²å½ããè¨å®å¤ã®ç®æã®ã¿ä¿®æ£ããã°ãmainãæ´æ°ãã¦ãä»ã®ç°å¢ï¼stg, prdï¼ã«ã¯å½±é¿ããªãï¼ãã¤ãã©ã¤ã³èªä½ã¯èµ·åããã®ã§ãå®æ ã¨ãã¦ã¯å·®åãªãã§ãããã¤ã空æ¯ãããå½¢ã¨ãªãï¼ã - ã¤ã³ãã©ã«ããã¦å¾æ¹äºææ§ããªããªããããªå¤æ´ã¯ããªãæ³å®ã§ãã£ããã¨ãããåºæ¬çã«ææ°ã®CDKã³ã¼ããé©ç¨ããç¶æ ã¨ãã¦ãããããããè¤æ°ãã©ã³ãéç¨ã«ããå ´åãåæ æ¼ããå¤ãã®å¤æ´ãä¸åº¦ã«prdã«åæ ããã¨ããã±ã¼ã¹ãçºçãå¾ãã®ã§ããããããã§ããã°mainä¸æ¬ã«ãã¦åç¨ç°å¢å«ãå°ããåä½ã§å¤æ´ãåæ ããæ¹ãè¯ãã¨å¤æã
- æ°ãããªã½ã¼ã¹ãçããå ´åãªã©ã«ã©ããã¦ãåç¨ç°å¢ã«åæ ããããªããã®ãåºã¦ããå ´åã¯ããã£ã¼ãã£ã¼ãã©ã°çãªãã®ãå®è£ ãã¦ãåç¨ç°å¢ï¼prdï¼ã«ã¯ãããã¤ãããªãããã«å¶å¾¡ããã°è¯ãã¨å¤æã
çµæçã«ã¯è¯ãã£ãã¨æããæ¬çªç°å¢ï¼prdï¼ã«ããã¦ã¤ã³ãã©ã®å¤æ´ãè¡ãã®ã¯ã©ããã¦ãç·å¼µæã®ããä½æ¥ã§ããã ãã®ããããã¨ãprdã®è¨å®ãä¸åå¤ãã£ã¦ããã空æ¯ãã«çµãã£ãã¨ãã¦ãã常ã«ææ°ã®CDKã®ã³ã¼ããé©ç¨ããã¦ããç¶æ ã§ããã¨ããã®ã¯ç²¾ç¥çã«è¯ãã£ãã
çµããã«
ã¾ã¨ããã¨ä»åæ¡ç¨ããAWS CDKã®ãã¼ã éçºã®ä»çµã¿ã¨ãã¦ã¯ä»¥ä¸ã§ããã
- 1ãªãã¸ããªã§ã³ã¼ãã管çããã©ã³ãã¯åºæ¬mainã®ã¿ã
- CDKéçºæã¯éçºç¨ãã©ã³ããåããåä½æ¥è å°ç¨ã®ç°å¢ã«æåã§ãããã¤ãæ¤è¨¼ã
- éçºç¨ãã©ã³ãã§ã®ä½æ¥ãå®äºãããmainã«ãã¼ã¸ããã¤ãã©ã¤ã³ãããªã¬ã¼ãããã¼ã å ¨ä½ã®ç°å¢ï¼dev, stg, prdï¼ã«èªåã§é©ç¨ã