ããã«ã¡ã¯ãAWS CLIã好ããªç¦å³¶ã§ãã
ã¯ããã«
ä»åã¯ã以ä¸ã®ãã³ãºãªã³ãå®æ½ããããããã³ãºãªã³ã®å 容ãåºã«CI/CD for Amazon ECSã®èªåãããã¤ã®æµããã¾ã¨ãã¦ã¿ã¾ãã
âAWS CI/CD for Amazon ECS ãã³ãºãªã³
https://pages.awscloud.com/rs/112-TZM-766/images/AWS_CICD_ECS_Handson.pdf
ã¾ãã以ä¸ã®ããã°ãèªãã å¾ã«æ¬ããã°ããèªã¿ããã ãã¨è¯ããã¨åãã¾ãã https://blog.serverworks.co.jp/cicd-ecs-build-deploy-files
æ¦è¦å³
æµãã®èª¬æ
â ã³ã¼ãã®Push
ãã¼ã«ã«ãéçºç°å¢ã§éçºããã³ã¼ããCodeCommitã«Pushãã¾ãã
â¡CodeCommitã¸ã®Pushãæ¤ç¥ããCodePipelineãèµ·å
CodeCommitã¸ã®Pushãæ¤ç¥ããCodePipelineãèµ·åãã¾ãã
ããã¯CodePipelineä½ææã®ä»¥ä¸ã®è¨å®ã«ãããã¾ãã
â¢CodePipelineç¨ã®S3ãã±ãããä½æãã(ååå®è¡ã®ã¿)ãCodeCommitã®ã³ã¼ããè¤è£½
CodePipelineç¨ã®S3ãã±ãããä½æãã(ååå®è¡ã®ã¿)ãCodeCommitã«Pushããã³ã¼ããè¤è£½ããã¾ãã
CodePipelineã®ä½ææã«ä½æããS3ãã±ããã®è¨å®ãªã©ãè¡ãã¾ãããããcodepipeline-ap-northeast-1-ä»»æã®æ°åãã¨ããååã§èªåã§S3ãã±ãããä½æããã¾ãã
- è£è¶³
CodePipelineãå®è¡å¾ãå®è¡å±¥æ´ã®ã¢ã¼ãã£ãã¡ã¯ãã§S3ã確èªãããã¨ãå¯è½ã§ãã
SourceArtifactãã¯ãªãã¯ããã¨ãS3ãã±ããã表示ããã¾ãã
â£CodePipelineããCodeBuildãå¼ã³åºããããã«ãç°å¢ãèµ·å(å©ç¨è ãã確èªä¸å¯)
CodePipelineããCodeBuildãå¼ã³åºããããã«ãç°å¢ãèµ·åããã¾ãã
(ãã«ãç°å¢ã¯å©ç¨è
ããã¯ç¢ºèªãããã¨ã¯ã§ãã¾ããã)
ãã«ãç°å¢ã¯ãCodeBuildã®ãã«ãããã¸ã§ã¯ãä½ææã®ãç°å¢ãã§è¨å®ããå 容ã§èµ·åããã¾ãã
ã¾ããIAMãã¼ã«ãèªåä½æããå ´åããã¼ã«ã«ã¯ä»¥ä¸ã®ãããªã«ã¹ã¿ã ããªã·ã¼ãã¢ã¿ããããã¾ãã ãªãä»åå®æ½ãããã³ãºãªã³ã®å ´åããã«ãç°å¢ããECRã«ãã¢ã¯ã»ã¹ãå¿ è¦ãªãããAmazonEC2ContainerRegistryPowerUser(ããã¼ã¸ãããªã·ã¼)ãå¥éä»ä¸ãã¾ããã
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Resource": [ "arn:aws:logs:ap-northeast-1:123456789012:log-group:/aws/codebuild/php-sample-build", "arn:aws:logs:ap-northeast-1:123456789012:log-group:/aws/codebuild/php-sample-build:*" ], "Action": [ "logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents" ] }, { "Effect": "Allow", "Resource": [ "arn:aws:s3:::codepipeline-ap-northeast-1-*" ], "Action": [ "s3:PutObject", "s3:GetObject", "s3:GetObjectVersion", "s3:GetBucketAcl", "s3:GetBucketLocation" ] }, { "Effect": "Allow", "Action": [ "codebuild:CreateReportGroup", "codebuild:CreateReport", "codebuild:UpdateReport", "codebuild:BatchPutTestCases", "codebuild:BatchPutCodeCoverages" ], "Resource": [ "arn:aws:codebuild:ap-northeast-1:123456789012:report-group/php-sample-build-*" ] } ] }
â¤S3ã«è¤è£½ãããã³ã¼ãããã«ãç°å¢ã«è¤è£½
S3ã«è¤è£½ãããã³ã¼ãããã«ãç°å¢ã«è¤è£½ãã¾ãã
- ä½è«
CodeCommitããç´æ¥ã³ã¼ããè¤è£½ãã¦ãè¯ãæ°ããã¾ãããS3ã«ã³ã¼ããä¸æ¦è¤è£½ãã¦ããã®ã¯ãéå»ã«å®è¡ãããã«ãã§å©ç¨ããã³ã¼ãã管çãããããªã®ããªã¨èãã¾ããã
â¥ECRã«ãã°ã¤ã³
â¦ã¤ã¡ã¼ã¸ã®ä½æ,
â§ECRã«ã¤ã¡ã¼ã¸ãPush(imageDetail.jsonãä½æ)
â¥,â¦,â§ã¾ã¨ãã¦è¨è¼ãã¾ãããä»åã®ãã³ãºãªã³ã®å ´åãbuildspec.ymlã«å¾ããã£ãã
- ECRã«ãã°ã¤ã³
- ã¤ã¡ã¼ã¸ã®ä½æ
- ECRã«ã¤ã¡ã¼ã¸ãPush
ãè¡ãã¾ãã
buildspec.ymlã«ã¯ãã«ãæã®ã³ãã³ããè¨è¼ããã¦ãããå©ç¨ãããã¡ã¤ã«ã¯ããã«ãããã¸ã§ã¯ãä½ææã®ãBuildspecãã§è¨å®å¯è½ã§ãã
â¨BuildArtifactãS3ã«æ ¼ç´
BuildArtifactã¨ã¯ãBuildSpec.ymlã«å®ç¾©ãããã¡ã¤ã«ã¨ãªãã¾ããä»åã¯ãimageDetail.jsonã¨ãªãã¾ãã
ããããããã¨ã§ãã¼ã«ããã¯ãå¯è½ã«ãã¦ãããã¨åãã¾ãã
ã¢ããªã±ã¼ã·ã§ã³ã®ãªãã¸ã§ã³ãããã¼ã¸ã§ã³ã確èªãããã¨ãå¯è½ã§ããã®å®æ ãBuildArtifactãªã®ããªã¨æã£ã¦ããã¾ãã
â©CodePipelineããCodeDeployãå¼ã³åºããã
CodeBuildã®å¦çãå®äºããããCodeDeployãå¼ã³åºããã¾ãã
âªtaskdef.jsonã®å 容ã§ã¿ã¹ã¯å®ç¾©ã®æ´æ°(å©ç¨ããã¤ã¡ã¼ã¸ã®æ¸ãæã)
CodeBuildã§ä½æããã¤ã¡ã¼ã¸ãå©ç¨ããããã¿ã¹ã¯å®ç¾©ãtaskdef.jsonãåºã«æ´æ°ãã¾ãã
ã¿ã¹ã¯å®ç¾©æ´æ°ã«å©ç¨ãããã¡ã¤ã«(taskdef.json)ã¯ãCodePipelineã®ãããã¤ã®ç·¨éç»é¢ã®ä»¥ä¸ã§è¨å®ãã¾ãã
â«æ´æ°ããã¿ã¹ã¯å®ç¾©ã§ã³ã³ããããããã¤(Greenã®ç°å¢ãä½æ)
appspec.ymlãåºã«æ´æ°ããã¿ã¹ã¯å®ç¾©ã§ã¿ã¹ã¯(ã³ã³ãã)ããããã¤ãã¾ãã(Greenã®ç°å¢ãä½æ)
ãã®æ®µéã§ECSã®ç»é¢ã確èªããã¨ãã¿ã¹ã¯(ã³ã³ãã)ãå¢ãã¦ãããã¨ãåããã¾ãã
ã¿ã¹ã¯ç»é¢(åè)
â¬æ¢åã®ã³ã³ãããåé¤(Blueã®ç°å¢ãåé¤)
appspec.ymlãåºã«æ¢åã®ã³ã³ãããåé¤ãã¾ãã(Blueã®ç°å¢ãåé¤)
ãã®æ®µéã§ECSã®ç»é¢ã確èªããã¨ãã¿ã¹ã¯(ã³ã³ãã)ãæ¸ã£ã¦ãããã¨ãåããã¾ãã
ã¿ã¹ã¯ç»é¢(åè)
è£è¶³
â«,â¬æã®ALBå´ã®åã
â«,â¬ã®éã®ALBã®åããè¨è¼ãã¾ãã ã¾ããECSã®ãµã¼ãã¹ãä½æããéã«ãããã¤ã¡ã³ãã¿ã¤ããè¨å®ããç®æãããã¾ãã ãããã¤ã¡ã³ãã¿ã¤ãã§ãBlue/Green ãããã¤ã¡ã³ã (AWS CodeDeploy ã使ç¨)ããé¸æãããã¼ããã©ã³ãµã¼ã«ALBãé¸æããã¨ã 以ä¸ã®ããã«ã¿ã¼ã²ããã°ã«ã¼ãã2ã¤ç¨æãããã¨ã«ãªãã¾ãã
ãã®2ã¤ã®ã¿ã¼ã²ããã°ã«ã¼ããCodeDeployãèªåã§ãå ¥ãæ¿ãããã¨ã§Blue/Greenãããã¤ãè¡ã£ã¦ãã¾ãã ãã®ããããããã¤åå¾ã§ALBã®ã¿ã¼ã²ããã°ã«ã¼ããå ¥ãæ¿ãã£ã¦ãããã¨ãåãããã¨åãã¾ãã
- ãããã¤å
- ãããã¤å¾
IMAGE1_NAME,TASK_DEFINITIONãå®ç¾©ãããæµã
ãã®2ã¤ã®å¤æ°ãå®ç¾©ãããæµããæ··ä¹±ãããããã¾ã¨ãã¦ã¿ã¾ããã
â CodeBuildæã«ã¢ã¼ãã£ãã¡ã¯ã(imageDetail.json)ãçæ
- imageDetail.jsonã®ä¸èº«(ä¾)
{ "ImageURI": "ACCOUNTID.dkr.ecr.us-west-2.amazonaws.com/dk-image-repo@sha256:example3" }
â¡çæããã¢ã¼ãã£ãã¡ã¯ã(imageDetail.json)ã«è¨è¼ãããImageURIããIMAGE1_NAMEãã«å®ç¾©ããã
â¢ã½ã¼ã¹ã«å«ã¾ããtaskdef.jsonã¨ãIMAGE1_NAMEãã«å®ç¾©ãããæ
å ±ãåºã«ã¿ã¹ã¯å®ç¾©ãçæ
- taskdef.jsonã«è¨å®ãããIAMGE1_NAME(ä¾)
"image": "<IMAGE1_NAME>",
â£çæãããã¿ã¹ã¯å®ç¾©ããTASK_DEFINITIONãã«å®ç¾©ããã
â¤ã¿ã¹ã¯å®ç¾©ãã½ã¼ã¹ã«å«ã¾ããappspec.ymlã¨ãTASK_DEFINITIONãã«å®ç¾©ãããæ
å ±ãåºã«ECSã¸ãããã¤
- appsepec.ymlã«è¨å®ãããTASK_DEFINITION(ä¾)
TaskDefinition: "<TASK_DEFINITION>"
çµããã«
ä»åã¯ãåå¿é²ã¨ãã¦ECSã¸ã®èªåãããã¤ã®æµããã¾ã¨ãã¦ã¿ã¾ããã
ã©ãªããã®ãå½¹ã«ç«ã¦ãã°å¹¸ãã§ãã