

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# CodeCommit의 모노리포지토리에 대한 변경 사항 자동 감지 및 다양한 CodePipeline 파이프라인 시작
<a name="automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit"></a>

*Helton Ribeiro, Petrus Batalha, Ricardo Morais, Amazon Web Services*

## 요약
<a name="automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit-summary"></a>

**알림**: AWS Cloud9 신규 고객은 더 이상를 사용할 수 없습니다. 의 기존 고객은 평소와 같이 서비스를 계속 사용할 AWS Cloud9 수 있습니다. [자세히 알아보기](https://aws.amazon.com/blogs/devops/how-to-migrate-from-aws-cloud9-to-aws-ide-toolkits-or-aws-cloudshell/)

이 패턴을 사용하면에서 모노 리포지토리 기반 애플리케이션의 소스 코드에 대한 변경 사항을 자동으로 감지 AWS CodeCommit 한 다음 각 마이크로서비스에 대해 지속적 통합 및 지속적 전달(CI/CD) 자동화를 AWS CodePipeline 실행하는 파이프라인을에서 시작할 수 있습니다. 이 접근 방식은 모노리포지토리 기반 애플리케이션의 각 마이크로서비스가 전용 CI/CD 파이프라인을 가질 수 있다는 것을 의미하며, 이를 통해 가시성을 높이고, 코드를 더 쉽게 공유하고, 협업, 표준화 및 검색 가능성을 개선할 수 있습니다.

이 패턴에 설명된 솔루션은 모노 리포지토리 내의 마이크로서비스 간에 종속성 분석을 수행하지 않습니다. 소스 코드의 변경 사항만 감지하고 일치하는 CI/CD 파이프라인을 시작합니다.

이 패턴은를 통합 개발 환경(IDE) AWS Cloud9 으로 사용하고 AWS Cloud Development Kit (AWS CDK) 를 사용하여 `MonoRepoStack` 및의 두 CloudFormation 스택을 사용하여 인프라를 정의합니다`PipelinesStack`. `MonoRepoStack` 스택은에 모노레포 AWS CodeCommit 와 CI/CD 파이프라인을 시작하는 AWS Lambda 함수를 생성합니다. `PipelinesStack` 스택은 파이프라인 인프라를 정의합니다.

**중요**  
이 패턴의 워크플로는 개념 증명(PoC)입니다. 테스트 환경에서만 사용하는 것이 좋습니다. 프로덕션 환경에서이 패턴의 접근 방식을 사용하려면 AWS Identity and Access Management (IAM) 설명서[의 IAM의 보안 모범 사례를](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html) 참조하고 IAM 역할 및를 필요한 대로 변경합니다 AWS 서비스. 

## 사전 조건 및 제한 사항
<a name="automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit-prereqs"></a>

**사전 조건 **
+ 활성 AWS 계정.
+ AWS Command Line Interface (AWS CLI), 설치 및 구성됨. 자세한 내용은 AWS CLI 설명서[AWS CLI의 설치, 업데이트 및 제거](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html)를 참조하세요. 
+ Python 3 및 `pip` 로컬 머신에 설치. 자세한 내용은 [Python 설명서](https://www.python.org/)를 참조하세요. 
+ AWS CDK설치 및 구성 자세한 내용은 AWS CDK 설명서[의 시작하기 AWS CDK](https://docs.aws.amazon.com/cdk/latest/guide/getting_started.html)를 참조하세요. 
+ 설치 및 구성된 AWS Cloud9 IDE입니다. 자세한 내용은 AWS Cloud9 설명서의 [설정을 AWS Cloud9](https://docs.aws.amazon.com/cloud9/latest/user-guide/setting-up.html) 참조하세요. 
+ GitHub [AWS CodeCommit 모노리포지토리 다중 파이프라인은 로컬 시스템에 복제된 리포지토리를 트리거합니다](https://github.com/aws-samples/monorepo-multi-pipeline-trigger). 
+ CodePipeline으로 빌드하고 배포하려는 애플리케이션 코드가 들어 있는 기존 디렉터리.
+  AWS 클라우드의 DevOps 모범 사례 숙지 및 경험 DevOps에 대한 친숙도를 높이려면 AWS 권장 가이드 웹 사이트에서 [ DevOps 사례를 사용하여 마이크로서비스와 느슨하게 결합된 아키텍처 구축 AWS Cloud9](https://docs.aws.amazon.com/prescriptive-guidance/latest/patterns/build-a-loosely-coupled-architecture-with-microservices-using-devops-practices-and-aws-cloud9.html) 패턴을 사용할 수 있습니다. 

## 아키텍처
<a name="automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit-architecture"></a>

다음 다이어그램은 AWS CDK 를 사용하여 `MonoRepoStack` 및 라는 두 개의 AWS CloudFormation 스택이 있는 인프라를 정의하는 방법을 보여줍니다`PipelinesStack`.

![AWS CDK를 사용하여 두 개의 CloudFormation 스택이 있는 인프라를 정의하는 워크플로입니다.](http://docs.aws.amazon.com/ko_kr/prescriptive-guidance/latest/patterns/images/pattern-img/a3397158-a208-4033-844e-969af13ae8b6/images/b0bb1094-b598-4b3d-ab8b-ad9b0eb45f38.png)


이 다이어그램은 다음 워크플로를 보여줍니다.

1. 부트스트랩 프로세스는 AWS CDK 를 사용하여 AWS CloudFormation 스택 `MonoRepoStack` 및를 생성합니다`PipelinesStack`.

1. `MonoRepoStack` 스택은 애플리케이션과 각 커밋 후에 시작되는 `monorepo-event-handler` Lambda 함수를 위한 CodeCommit 리포지토리를 생성합니다.

1. `PipelinesStack` 스택은 Lambda 함수에 의해 시작되는 파이프라인을 CodePipeline에 생성합니다. 각 마이크로서비스에는 정의된 인프라 파이프라인이 있어야 합니다.

1. `microservice-n`의 파이프라인은 Lambda 함수에 의해 시작되고 CodeCommit의 소스 코드를 기반으로 하는 격리된 CI/CD 단계를 시작합니다.

1. `microservice-1`의 파이프라인은 Lambda 함수에 의해 시작되고 CodeCommit의 소스 코드를 기반으로 하는 격리된 CI/CD 단계를 시작합니다.

다음 다이어그램은 계정에 AWS CloudFormation 스택 `MonoRepoStack` 및를 배포`PipelinesStack`하는 방법을 보여줍니다.

![AWS 계정에 CloudFormation 스택 MonoRepoStack 및 PipelinesStack 배포.](http://docs.aws.amazon.com/ko_kr/prescriptive-guidance/latest/patterns/images/pattern-img/a3397158-a208-4033-844e-969af13ae8b6/images/39e60e49-dea2-486d-8a2c-6cae438f69b4.png)


1. 사용자가 애플리케이션의 마이크로서비스 중 하나에서 코드를 변경합니다.

1. 사용자가 로컬 리포지토리에서 CodeCommit 리포지토리로 변경 내용을 푸시합니다.

1. 푸시 활동은 CodeCommit 리포지토리에 대한 모든 푸시를 수신하는 Lambda 함수를 시작합니다.

1. 이 Lambda 함수는 AWS Systems Manager의 기능인 Parameter Store에서 파라미터를 읽고 가장 최근의 커밋 ID를 검색합니다. 파라미터는 `/MonoRepoTrigger/{repository}/{branch_name}/LastCommit` 명명 형식을 사용합니다. 파라미터를 찾을 수 없는 경우 Lambda 함수는 CodeCommit 리포지토리에서 마지막 커밋 ID를 읽고 반환된 값을 파라미터 스토어에 저장합니다.

1. Lambda 함수는 커밋 ID와 변경된 파일을 식별한 후 각 마이크로서비스 디렉터리의 파이프라인을 식별하고 필요한 CodePipeline 파이프라인을 시작합니다.

## 도구
<a name="automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit-tools"></a>
+ [AWS Cloud Development Kit (AWS CDK)](https://docs.aws.amazon.com/cdk/latest/guide/home.html)는 코드에서 클라우드 인프라를 정의하고 이를 프로비저닝하기 위한 소프트웨어 개발 프레임워크입니다 CloudFormation.
+ [Python](https://www.python.org/)은 빠르게 작업하고 시스템을 보다 효과적으로 통합할 수 있는 프로그래밍 언어입니다.

**코드**

이 패턴의 소스 코드와 템플릿은 GitHub [AWS CodeCommit 모노리포지토리 다중 파이프라인 트리거](https://github.com/aws-samples/monorepo-multi-pipeline-trigger) 리포지토리에서 제공됩니다.

## 모범 사례
<a name="automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit-best-practices"></a>
+ 이 샘플 아키텍처에는 배포된 인프라에 대한 모니터링 솔루션이 포함되어 있지 않습니다. 프로덕션 환경에이 솔루션을 배포하려면 모니터링을 활성화하는 것이 좋습니다. 자세한 내용은 AWS Serverless Application Model (AWS SAM) 설명서의 [ CloudWatch Application Insights를 사용하여 서버리스 애플리케이션 모니터링을 참조하세요](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/monitor-app-insights.html).
+ 이 패턴에서 제공하는 샘플 코드를 편집할 때는 AWS CDK 설명서의 [클라우드 인프라 개발 및 배포 모범 사례를](https://docs.aws.amazon.com/cdk/v2/guide/best-practices.html) 따르세요.
+ 마이크로서비스 파이프라인을 정의할 때 AWS CodePipeline 설명서의 [보안 모범 사례를](https://docs.aws.amazon.com/codepipeline/latest/userguide/security-best-practices.html) 검토하세요.
+ [cdk-nag](https://github.com/cdklabs/cdk-nag) 유틸리티를 사용하여 AWS CDK 코드에서 모범 사례를 확인할 수도 있습니다. 이 도구는 팩별로 그룹화된 규칙 세트를 사용하여 코드를 평가합니다. 사용 가능한 팩은 다음과 같습니다.
  + [AWS 솔루션 라이브러리](https://github.com/cdklabs/cdk-nag/blob/main/RULES.md#awssolutions)
  + [건강보험 이동성 및 책임법(HIPAA) 보안](https://github.com/cdklabs/cdk-nag/blob/main/RULES.md#hipaa-security)
  + [국립 표준 기술 연구소(NIST) 800-53 버전 4](https://github.com/cdklabs/cdk-nag/blob/main/RULES.md#nist-800-53-rev-4)
  + [NIST 800-53 버전 5](https://github.com/cdklabs/cdk-nag/blob/main/RULES.md#nist-800-53-rev-5)
  + [지불 카드 산업 데이터 보안 표준(PCI DSS) 3.2.1](https://github.com/cdklabs/cdk-nag/blob/main/RULES.md#pci-dss-321)

## 에픽
<a name="automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit-epics"></a>

### 환경 설정
<a name="set-up-the-environment"></a>


| 작업 | 설명 | 필요한 기술 | 
| --- | --- | --- | 
| 가상 Python 환경을 생성합니다. |  AWS Cloud9 IDE에서 다음 명령을 실행하여 가상 Python 환경을 생성하고 필요한 종속성을 설치합니다.<br />`make install` | 개발자 | 
| 에 AWS 리전 대해 AWS 계정 및를 부트스트랩합니다 AWS CDK. | 다음 명령을 실행하여 필수 AWS 계정 및 리전을 부트스트랩합니다.<br />`make bootstrap account-id=<your-AWS-account-ID> region=<required-region>` | 개발자 | 

### 마이크로서비스를 위한 새 파이프라인 추가
<a name="add-a-new-pipeline-for-a-microservice"></a>


| 작업 | 설명 | 필요한 기술 | 
| --- | --- | --- | 
|  애플리케이션 디렉터리에 샘플 코드를 추가합니다. | 샘플 애플리케이션 코드가 들어 있는 디렉터리를 복제된 GitHub [AWS CodeCommit 모노리포지토리 다중 파이프라인 트리거](https://github.com/aws-samples/monorepo-multi-pipeline-trigger) 리포지토리의 `monorepo-sample` 디렉터리에 추가합니다. | 개발자 | 
| `monorepo-main.json` 파일을 편집합니다. | 애플리케이션 코드의 디렉터리 이름과 파이프라인 이름을 복제된 리포지토리의 `monorepo-main.json` 파일에 추가합니다. | 개발자 | 
| 파이프라인을 생성하십시오. | 리포지토리의 `Pipelines` 디렉터리에 애플리케이션의 `class` 파이프라인을 추가합니다. 디렉터리에는 `pipeline_hotsite.py` 및 라는 두 개의 샘플 파일이 포함되어 있습니다`pipeline_demo.py`. 각 파일에는 소스, 빌드 및 배포라는 세 단계가 있습니다.<br />애플리케이션 요구 사항에 따라 파일 중 하나를 복사하고 변경할 수 있습니다.  | 개발자 | 
| `monorepo_config.py` 파일을 편집합니다. | `service_map`에서 애플리케이션의 디렉터리 이름과 파이프라인용으로 만든 클래스를 추가합니다.<br />예를 들어, 다음 코드는 `MySamplePipeline` 클래스로 이름이 `pipeline_mysample.py`로 지정된 파일을 사용하는 `Pipelines` 디렉터리의 파이프라인 정의를 보여줍니다.<pre>...<br /># Pipeline definition imports<br />from pipelines.pipeline_demo import DemoPipeline<br />from pipelines.pipeline_hotsite import HotsitePipeline<br />from pipelines.pipeline_mysample import MySamplePipeline<br /><br />### Add your pipeline configuration here<br />service_map: Dict[str, ServicePipeline]  = {<br />    # folder-name -> pipeline-class<br />    'demo': DemoPipeline(),<br />    'hotsite': HotsitePipeline(),<br />    'mysample': MySamplePipeline()<br />}</pre> | 개발자 | 

### MonoRepoStack 스택 배포
<a name="deploy-the-monorepostack-stack"></a>


| 작업 | 설명 | 필요한 기술 | 
| --- | --- | --- | 
|  AWS CloudFormation 스택을 배포합니다. | `make deploy-core` 명령을 실행하여 복제된 AWS CloudFormation `MonoRepoStack`리포지토리의 루트 디렉터리에 기본 파라미터 값을 사용하여 스택을 배포합니다.<br />`make deploy-core monorepo-name=<repo_name>` 명령을 실행하여 리포지토리 이름을 변경할 수 있습니다.`make deploy monorepo-name=<repo_name>` 명령을 사용하여 두 파이프라인을 동시에 배포할 수 있습니다. | 개발자 | 
| CodeCommit 리포지토리를 확인합니다. | `aws codecommit get-repository --repository-name <repo_name>` 명령을 실행하여 리소스가 생성되었는지 확인합니다. CloudFormation 스택은 모노 리포지토리가 저장되는 CodeCommit 리포지토리를 생성하기 때문에 수정 내용을 푸시하기 시작한 경우 `cdk destroy MonoRepoStack `명령을 실행하지 마십시오. | 개발자 | 
|  CloudFormation 스택 결과를 검증합니다. | 다음 명령을 실행하여 스택이 CloudFormation `MonoRepoStack` 올바르게 생성되고 구성되었는지 확인합니다.<pre>aws cloudformation list-stacks --stack-status-filter CREATE_COMPLETE --query 'StackSummaries[?StackName == 'MonoRepoStack']'</pre> | 개발자 | 

### PipelinesStack 스택 배포
<a name="deploy-the-pipelinesstack-stack"></a>


| 작업 | 설명 | 필요한 기술 | 
| --- | --- | --- | 
|  CloudFormation 스택을 배포합니다. | 스택을 AWS CloudFormation `PipelinesStack` 배포한 후에는 `MonoRepoStack` 스택을 배포해야 합니다. 새 마이크로서비스가 모노리포지토리의 코드 베이스에 추가되면 스택의 크기가 증가하고 새 마이크로서비스가 온보딩되면 재배포됩니다.<br />`make deploy-pipelines` 명령을 실행하여 PipelinesStack 스택을 배포합니다.`make deploy monorepo-name=<repo_name>` 명령을 실행하여 두 파이프라인을 동시에 배포할 수도 있습니다.<br />다음 샘플 출력은 구현 종료 시 `PipelinesStacks` 배포가 마이크로서비스의 URL을 인쇄하는 방법을 보여줍니다.<pre>Outputs:<br />PipelinesStack.demourl = .cloudfront.net<br />PipelinesStack.hotsiteurl = .cloudfront.net</pre> | 개발자 | 
|  AWS CloudFormation 스택 결과를 검증합니다. | 다음 명령을 실행하여 스택이 AWS CloudFormation `PipelinesStacks` 올바르게 생성되고 구성되었는지 확인합니다.<pre>aws cloudformation list-stacks --stack-status-filter CREATE_COMPLETE UPDATE_COMPLETE --query 'StackSummaries[?StackName == 'PipelinesStack']'</pre> | 개발자 | 

### 리소스 정리
<a name="clean-up-resources"></a>


| 작업 | 설명 | 필요한 기술 | 
| --- | --- | --- | 
|  AWS CloudFormation 스택을 삭제합니다. | `make destroy` 명령을 실행합니다. | 개발자 | 
| 파이프라인의 S3 버킷을 삭제하십시오. | [See the AWS documentation website for more details](http://docs.aws.amazon.com/ko_kr/prescriptive-guidance/latest/patterns/automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit.html) | 개발자 | 

## 문제 해결
<a name="automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit-troubleshooting"></a>


| 문제 | Solution | 
| --- | --- | 
|  AWS CDK 문제가 발생했습니다. | AWS CDK 설명서의 [일반적인 AWS CDK 문제 해결을](https://docs.aws.amazon.com/cdk/v2/guide/troubleshooting.html) 참조하세요. | 
| 마이크로서비스 코드를 푸시했지만 마이크로서비스 파이프라인이 실행되지 않았습니다. | **설정 검증**<br />*브랜치 구성을 확인합니다.*[See the AWS documentation website for more details](http://docs.aws.amazon.com/ko_kr/prescriptive-guidance/latest/patterns/automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit.html)<br />*구성 파일 검증:*[See the AWS documentation website for more details](http://docs.aws.amazon.com/ko_kr/prescriptive-guidance/latest/patterns/automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit.html)<br />**콘솔 문제 해결**<br />*AWS CodePipeline 검사:*[See the AWS documentation website for more details](http://docs.aws.amazon.com/ko_kr/prescriptive-guidance/latest/patterns/automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit.html)<br />*AWS Lambda 문제 해결:*[See the AWS documentation website for more details](http://docs.aws.amazon.com/ko_kr/prescriptive-guidance/latest/patterns/automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit.html) | 
| 모든 마이크로서비스를 재배포해야 합니다. | 모든 마이크로서비스를 강제로 재배포하려면 두 가지 접근 방식이 있습니다. 요구 사항에 맞는 옵션을 선택합니다.<br />**접근 방식 1: 파라미터 스토어에서 파라미터 삭제**<br />이 방법에는 배포에 사용된 마지막 커밋 ID를 추적하는 Systems Manager Parameter Store 내에서 특정 파라미터를 삭제하는 작업이 포함됩니다. 이 파라미터를 제거하면 시스템은 새 상태로 인식되므로 다음 트리거 시 모든 마이크로서비스를 다시 배포해야 합니다.<br />단계:[See the AWS documentation website for more details](http://docs.aws.amazon.com/ko_kr/prescriptive-guidance/latest/patterns/automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit.html)<br />장점:[See the AWS documentation website for more details](http://docs.aws.amazon.com/ko_kr/prescriptive-guidance/latest/patterns/automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit.html)<br />단점:[See the AWS documentation website for more details](http://docs.aws.amazon.com/ko_kr/prescriptive-guidance/latest/patterns/automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit.html)<br />**접근 방식 2: 각 모노 리포지토리 하위 폴더에서 커밋 푸시**<br />이 방법에는 사소한 변경을 수행하고 모노레포 내의 각 마이크로서비스 하위 폴더에 푸시하여 개별 파이프라인을 시작하는 작업이 포함됩니다.<br />단계:[See the AWS documentation website for more details](http://docs.aws.amazon.com/ko_kr/prescriptive-guidance/latest/patterns/automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit.html)<br />장점:[See the AWS documentation website for more details](http://docs.aws.amazon.com/ko_kr/prescriptive-guidance/latest/patterns/automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit.html)<br />단점:[See the AWS documentation website for more details](http://docs.aws.amazon.com/ko_kr/prescriptive-guidance/latest/patterns/automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit.html) | 

## 관련 리소스
<a name="automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit-resources"></a>
+ [CDK Pipelines을 사용한 지속적 통합 및 전송(CI/CD)](https://docs.aws.amazon.com/cdk/latest/guide/cdk_pipeline.html)(AWS CDK 문서화)
+ [aws-cdk/pipelines 모듈](https://docs.aws.amazon.com/cdk/api/latest/docs/pipelines-readme.html)(AWS CDK API 참조)