IaaS/퍼블릭클라우드

(AWS) CloudFormation을 통한 파이프라인 템플릿 만들기

armyost 2022. 1. 13. 08:48
728x90

CloudFormation을 통한 파이프라인 템플릿 만들기

 

아래 Github Repository에는 Source Stage인 CodeCommit부터 Test를 위한 CodeBuild와 UAT 프로세스와 Manual Approval Stage가 포함되어 있다.

 

 

https://github.com/aws-samples/codepipeline-nested-cfn

 

GitHub - aws-samples/codepipeline-nested-cfn: CloudFormation templates, CodeBuild build specification & Python scripts to perfor

CloudFormation templates, CodeBuild build specification & Python scripts to perform unit tests of a nested CloudFormation template. - GitHub - aws-samples/codepipeline-nested-cfn: CloudFormatio...

github.com

 

 

수행과정은 다음과 같다. 

 

1. VPC Stack을 생성

  • 위 링크의 cfn-nested-repo 경로에는 다수의 YAML, JSON파일이 있다.
  • 그 중 vpc-stack.yml 파일은 기본 VPC를 생성한다. (Subnet, NAT GW, 등 포함)
  • vpc-params.json은 CFN 템플릿을 위한 파라미터 파일이다. ProdApprovalEmail과 UATApprovalEmail의 Value를 적절한 값으로 변경하길 바란다. 
  • 이제 Pull 받은 cfn을 실행하자.
    $ cd cfn-nested-repo
    $ aws cloudformation create-stack --stack-name NestedCFN-BaseStack --template-body file://vpc-stack.yml --parameters file://vpc-params.json​

2. CloudFormation 파라미터 입력

cfn-nested-repo 경로에는 다음 3개의 JSON 파일이 있고 각 JSON 파일을 수정한다. 

config-test.json: - CloudFormation parameter configuration file for test stack 

config-uat.json: - CloudFormation parameter configuration file for UAT stack 

config-prod.json: - CloudFormation parameter configuration file for Prod stack

 

위 3개의 Configuration 파일에 생성한 1번 에서 생성한  VPCID, PrivateSubnet1/2, PublicSubnet1/2, S3버킷 이름, DB SubnetGroup 값을 업데이트 한다. 그리고 사용할 KeyPair 값으로 업데이트 한다. 

 

3. CodeCommit Repository를 만들자

아래의 AWS CLI를 이용하여 두개의 CodeCommit Repository를 만들자

$ aws codecommit create-repository --repository-name cfn-nested-repo --repository-description "Repository for CloudFormation templates"
$ aws codecommit create-repository --repository-name validate-resources --repository-description "Repository for unit testing CloudFormation resources"

Repository가 완성되면 위 Repository를 복제하고 각 Repository에 맞는 cfn-nested-repo와 validate-resources 경로의 컨텐츠를 업로드 한다. 

 

4. CloudFormation을 사용한 CodePipeline 생성

1번에서 생성된 main stack의 Output의 값을 참고하여 적절한 ArtifactStoreS3Location, UATTopic, ProdTopic

 codepipeline-cfn-codebuild.json 을 업데이트 하고, 3번에서 생성한 Repository의 값을 CFNTemplateRepoName과 ValidateResourcesRepoName에 입력한다. 환경설정 파일이 모두 업데이트 되면 아래 커맨드로 CloudFormation Stack을 을 유발하여 CodePipe을 생성한다. 

 

CloudFormation이 성공적으로 생성되면 아래와 같은 파이프라인이 생성될 것이다. 

※ 참고 : 리소스를 삭제하는 동안 pipeline으로 생성된 Prod와 UAT stacks을 삭제한다.