Github에서 AWS CodeCommit으로 이전하기

in #aws6 years ago (edited)

images.png

개요

AWS에도 Github이나 Bitbucket과 같이​ GIT Repository가 존재한다.
AWS에 Code가 존재한다면 CI 구성할때에 속도나 보안문제에 매우 유리할 수가 있다.

Github에서 CodeCommit으로 마이그레이션을 하기 위해서는 다음의 절차로 진행한​다.

  1. CodeCommit에 Repository 생성
  2. Github에 있는 Repository를 CodeCommit으로 복사
  3. IAM을 통해서 권한의 설정
  4. 테스트​

AWS CodeCommit 소개

  • 저렴한 Private 리포지토리 ​가격
  • AWS 클라우드 내에 구축하여 스테이징 또는 프로덕션 환경에 가까이에 리포지토리가 유지
  • IAM을 이용하여 리포지토리에 사용자별 권한을 할당
  • CodePipeline을 이용한 빠르고 편리한 CI구성
  • Git 명령을​ 동일하게 사용​

Github Repository

이 문서에서 CodeCommit으로 옮길 샘플 Repository

https://github.com/harryoh/lambda_weather

CodeCommit에 Repository 생성

Repository 생성​

  1. CodeCommit Service로 이동

  2. [Create Repository] 선택​

  3. 옮길 Repository와 동일하게 ​Repository namelambda_weather 입력
    aws_codecommit_create_repository.png

  4. 생성된 Repository의 URL을 복사
    aws_codecommit_repository_url.png

    https://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/lambda_weather

  5. 생성된 Repository의 [Settings]에서 ARN을 복사
    aws_codecommit_repository_arn.png

    arn:aws:codecommit:ap-northeast-2:000000000000:lambda_weather

HTTPS Git Credential 생성​

  1. IAM Service - Users - 본인계정에서 [Security credentials] 탭으로 이동
  2. HTTPS Git credentials for AWS CodeCommit 항목에서 [Generate] 선택
  3. Credentials를 다운로드 받아서 보관

Repository 이동

Clone CodeCommit Repository

CodeCommit에 생성한 Repository를 생성한다. 다운받은 Credentials의 ID와 Password를 입력한다.

$ git clone https://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/lambda_weather
username:
password:
warning: You appear to have cloned an empty repository.

CodeCommit으로 Push

Repository에 github를 remote branch로 등록하여 fetch하고 소스를 CodeCommit로 push한다.​

$ cd lambda_weather
$ git remote add github https://github.com/harryoh/lambda_weather
$ git remote -v
github  https://github.com/harryoh/lambda_weather (fetch)
github  https://github.com/harryoh/lambda_weather (push)
origin  https://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/lambda_weather (fetch)
origin  https://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/lambda_weather (push)

$ git fetch github
$ git merge github/master --ff-only
$ git push origin master
Counting objects: 10, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (10/10), done.
Writing objects: 100% (10/10), 5.63 KiB | 2.82 MiB/s, done.
Total 10 (delta 0), reused 0 (delta 0)
To https://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/lambda_weather
 * [new branch]      master -> master

CodeCommit에서 확인​

aws_codecommit_show_repository.png

Repository 사용자 추가 및 ​권한 설정​

Policy 추가

  1. IAM - Policies으로 이동후 [Create Policy] 선택

  2. [Create Your Own Policy] 선택
    aws_iam_create_policy.png

  3. Policy Name에 CUSTOM_Developer 입력

  4. Policy Document에 다음을 입력후 [Create Policy] 선택​

    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": [
                    "codecommit:List*"
                ],
                "Resource": "*"
            },
            {
                "Effect": "Allow",
                "Action": [
                    "codecommit:BatchGetRepositories",
                    "codecommit:CreateBranch",
                    "codecommit:DeleteBranch",
                    "codecommit:Get*",
                    "codecommit:GitPull",
                    "codecommit:GitPush",
                    "codecommit:Put*",
                    "codecommit:Test*",
                    "codecommit:Update*"
                ],
                "Resource": [
                    "arn:aws:codecommit:ap-northeast-2:880169174736:lambda_weather"
                ]
            }
        ]
    }
    
  5. CUSTOM_Viewer Policy를 하나 더 추가

  6. 다음을 입력후 [Create Policy] 선택​

    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": [
                    "codecommit:List*"
                ],
                "Resource": "*"
            },
            {
                "Effect": "Allow",
                "Action": [
                    "codecommit:BatchGetRepositories",
                    "codecommit:Get*",
                    "codecommit:GitPull"
                ],
                "Resource": [
                    "arn:aws:codecommit:ap-northeast-2:880169174736:lambda_weather"
                ]
            }
        ]
    }
    
  7. 확인
    aws_iam_custom_policy_list.png

Group 추가

  1. IAM - Groups 에서 [Create New Groups] 선택

  2. Group Name에 MyTeam 입력 후 [Next Step] 선택

  3. Attach Policy에서 다음의 Policy를 추가후 [Next Step] 선택​

    • CUSTOM_Developer
    • IAMReadOnlyAccess
    • IAMSelfManageServiceSpecificCredentials
  4. Review에서 확인 후에 [Create Group] 선택
    aws_iam_git_credentials_generated.png

  5. MyTeam Group과 다음의 Policy를 선택하여 YourTeam Group을 생성

    • CUSTOM_Viewer
    • IAMReadOnlyAccess
    • IAMSelfManageServiceSpecificCredentials

사용자 추가

  1. IAM - Users 에서 [Add user] 선택

  2. User name을 입력하고 AWS Management Console access에 체크하여 `[Next: Permissions]' 선택 (다른 항목은 적절하게 선택)

  3. Set permissions 에서 Add user to group 탭에서 MyTeam 체크후 [Next: Review] 선택​

  4. Review에서 확인 후에 [Create user] 선택
    aws_iam_adduser_review.png

  5. 위와 동일한 방법으로 YourTeam을 Group에 추가하여 새로운 사용자를 추가로 만든다.​

Create HTTPS Git credentials

  1. 새롭게 생성한 계정(harry, sally)로 로그인
  2. IAM - Users 에서 본인 계정 선택
  3. Security credentials 탭에서 ​HTTPS Git credentials for AWS CodeCommit 항목의 [Generate] 선택​
  4. ID / Password 저장

테스트

Git Credential 초기화

  • MacOS 사용자의 경우에는 Keychain Access 에서 Git 관련 정보를 삭제​
    key_chain_access.png

  • Git 인증 캐쉬 삭제

    git config --global --unset credential.helper

YourTeam Group 테스트

YourTeam Group은 Repository를 Clone 할 수는 있어도 Push는 불가

$ git clone https://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/lambda_weather lambda_weather_sally
Cloning into 'lambda_weather_sally'...
Username:
Password:
remote: Counting objects: 10, done.
Unpacking objects: 100% (10/10), done.

$ cd lambda_weather_sally
$ echo "test" | cat > test
$ git add test
$ git commit -m "test commit"

$ git push origin master
fatal: unable to access 'https://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/lambda_weather/': The requested URL returned error: 403

MyTeam Group 테스트

MyTeam Group은 Push가 가능. (테스트시 Git Credential 초기화 필요​)

$ git clone https://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/lambda_weather lambda_weather_harry
Cloning into 'lambda_weather_harry'...
Username:
Password:
remote: Counting objects: 10, done.
Unpacking objects: 100% (10/10), done.

$ cd lambda_weather_harry
$ echo "test" | cat > test
$ git add test
$ git commit -m "test commit"

$ git push origin master
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 258 bytes | 258.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To https://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/lambda_weather
   6c7bfc1..00f986f  master -> master

Pull Request

CodeCommit에서는 Pull Request를 지원하지 않아서 EC2에 Review 시스템을 설치해서 사용하거나 외부 서비스를 사용해야함.

참고

Coin Marketplace

STEEM 0.26
TRX 0.11
JST 0.032
BTC 63585.64
ETH 3035.86
USDT 1.00
SBD 3.84