02. AWS CODE COMMIT 이해와 사용방법

Table of Contents

01. Code Commit

01) 핵심 이해 사항

  • 코드 커밋 이란 ?
    • Private Git repositories 를 관리 호스팅 해주는 서비스
    • 실제로 Git 명령어로 작업들이 가능하며, 관리형 인만큼, 별다른 제약 없이 저장소를 확장 시킬 수 있다.
  • Git 을 사용하지 않는 이유 ?
    • 기업용 Git Hub 등은 많이 출시 되었지만 결국에 내 서버에 관리해야한다는 단점을 지니고 있는 만큼 관리형 이라는 이점이 있다.

02) 코드 커밋의 이점

  • 가용성이 높고, 확장 가능하며, 내결함성을 지니고 있다.
  • 용량 제한이 없으며, 알아서 자동으로 확장 된다.
  • Code Build, Code Pipeline, CodeDeploy, Lambda, SNS 등의 AWS 도구들과 연결성이 높다.
  • 기존 Git 관련 도구들과 잘 동작 한다.

03) 기본적인 Git 의 동작 원리

  1. [개발자 1] V1 을 클론하여, 로컬 저장소로 가져옵니다.
  2. [개발자 1] V1을 수정한 후 이를 커밋하여 변경 사항을 기록 합니다.[V2] (COMMIT)
  3. [개발자 1] V2의 변경 사항을 서버측에 전달 합니다.
  4. [개발자 2] 서버에 PULL 을 받아 변경 사항을 자신의 로컬에도 적용 합니다.

04) 중요 사항

  • 레파지토리를 위해 별도의 서버를 프로비저닝 하지 않아도 된다.
  • AWS의 다른 도구에서 DropDown 형태로 불러와 사용할 수 있다.
  • S3 나 여러 버전 관리 도구를 사용하기 위해서 필요한 사항이다.

02. 코드 커밋 기본

01) GUI 환경

(01) 접근 하기

[AWS] -> CodeCommit

(02) 리포지토리 생성

[01] 리포지토리 생성

[02] 리포지토리 이름 지정 후 생성

(03) 리포지토리 파일 생성 / 업로드

[01] 파일

[02] 작성자 이름 / 이메일 주소/ 커밋 메세지 설정 –

[03] 커밋 완료 파일 확인

02) CLI 환경

(01) 리파지토리 리스트 확인

aws codecommit list-repositories

[결과]

[ec2-user@ip-172-31-46-64 ~]$ aws codecommit list-repositories
{
    "repositories": [
        {
            "repositoryName": "Test-Hub",
            "repositoryId": "0e79cf45-6438-4dfd-ba67-c796ea5a999d"
        }
    ]
}

-> 앞서 생성한 1개 레파지토리가 있는 것을 확인할 수 있다.

(01) 리파지토리 생성

aws codecommit create-repository --repository-name [리파지토리 이름] 

[결과]

[ec2-user@ip-172-31-46-64 ~]$ aws codecommit create-repository --repository-name test-repo2
{
    "repositoryMetadata": {
        "accountId": "382631276292",
        "repositoryId": "570a6355-a695-4025-b5a7-bf70deca57b1",
        "repositoryName": "test-repo2",
        "lastModifiedDate": "2023-08-04T01:34:01.960000+00:00",
        "creationDate": "2023-08-04T01:34:01.960000+00:00",
        "cloneUrlHttp": "https://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/test-repo2",
        "cloneUrlSsh": "ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/test-repo2",
        "Arn": "arn:aws:codecommit:ap-northeast-2:382631276292:test-repo2"
    }
}

[리스트 확인]

[ec2-user@ip-172-31-46-64 ~]$ aws codecommit list-repositories
{
    "repositories": [
        {
            "repositoryName": "Test-Hub",
            "repositoryId": "0e79cf45-6438-4dfd-ba67-c796ea5a999d"
        },
        {
            "repositoryName": "test-repo2",
            "repositoryId": "570a6355-a695-4025-b5a7-bf70deca57b1"
        }
    ]
}

–> 레파지 토리가 2개로 늘어난 것을 확인할 수 있다.

(03) 리파지토리 정보 가져오기

Clone 개념이 아닌, Clone 을 위한 정보를 가져 오는 것이다.

aws codecommit get-repository --repository-name [레파지토리 이름]

[결과]

[ec2-user@ip-172-31-46-64 ~]$ aws codecommit get-repository --repository-name Test-Hub
{
    "repositoryMetadata": {
        "accountId": "382631276292",
        "repositoryId": "0e79cf45-6438-4dfd-ba67-c796ea5a999d",
        "repositoryName": "Test-Hub",
        "defaultBranch": "main",
        "lastModifiedDate": "2023-08-04T01:13:53.752000+00:00",
        "creationDate": "2023-08-04T01:10:28.057000+00:00",
        "cloneUrlHttp": "https://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/Test-Hub",
        "cloneUrlSsh": "ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/Test-Hub",
        "Arn": "arn:aws:codecommit:ap-northeast-2:382631276292:Test-Hub"
    }
}

(03) 리파지토리 삭제 하기

aws codecommit delete-repository --repository-name [삭제 리파지토리 명]

[결과]

[ec2-user@ip-172-31-46-64 ~]$ aws codecommit delete-repository --repository-name test-repo2
{
    "repositoryId": "570a6355-a695-4025-b5a7-bf70deca57b1"
}

[2차 확인]

[ec2-user@ip-172-31-46-64 ~]$ aws codecommit list-repositories
{
    "repositories": [
        {
            "repositoryName": "Test-Hub",
            "repositoryId": "0e79cf45-6438-4dfd-ba67-c796ea5a999d"
        }
    ]
}

03. Code Commit 활용 하기

01) IAM 설정

코드 커밋의 리파지토리의 생성 / 삭제 등의 관리가 아닌 실제로 활용하기 위해서는, IAM 에 코드 커밋 계정 정보가 사전에 등록이 되어 있어야 한다. 이를 위해, IAM 에 설정하는 방법을 소개 하고자 한다.

(01) [IAM] -> [사용자 / 사용자 그룹] -> [해당 유저 클릭] -> [하단 확인] -> [자격 증명 생성]

02) 기초 명령어 (Clone , Commit , Push, Pull )

(01) Clone 하기

git clone [클론 리파지토리 주소]

(실습)

[01] Clone 주소 가져오기

aws codecommit get-repository --repository-name [클론 리파지토리 확인]

[확인]

[ec2-user@ip-172-31-46-64 ~]$ aws codecommit get-repository --repository-name Test-Hub
{
    "repositoryMetadata": {
        "accountId": "382631276292",
        "repositoryId": "0e79cf45-6438-4dfd-ba67-c796ea5a999d",
        "repositoryName": "Test-Hub",
        "defaultBranch": "main",
        "lastModifiedDate": "2023-08-04T01:13:53.752000+00:00",
        "creationDate": "2023-08-04T01:10:28.057000+00:00",
        "cloneUrlHttp": "https://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/Test-Hub",
        "cloneUrlSsh": "ssh://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/Test-Hub",
        "Arn": "arn:aws:codecommit:ap-northeast-2:382631276292:Test-Hub"
    }
}

[02] Clone

git clone [클론 리파지토리 주소]

[확인]

[ec2-user@ip-172-31-46-64 ~]$ git clone https://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/Test-Hub
Cloning into 'Test-Hub'...
Username for 'https://git-codecommit.ap-northeast-2.amazonaws.com': test02-at-382631276292
Password for 'https://test02-at-382631276292@git-codecommit.ap-northeast-2.amazonaws.com':
remote: Counting objects: 3, done.
Unpacking objects: 100% (3/3), 572 bytes | 572.00 KiB/s, done.
[ec2-user@ip-172-31-46-64 ~]$ ls
Test-Hub
  • Username / Password는 앞서 IAM 을 통해 생성한 정보를 입력한다.

(02) Commit

git commit -m [커밋 사유]

[01] 변경 사항 생성

touch commit-test01

[02] git 상태 확인 및 변경 사항 추가

[ec2-user@ip-172-31-46-64 Test-Hub]$ git status
On branch main
Your branch is up to date with 'origin/main'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	commit-test01

nothing added to commit but untracked files present (use "git add" to track)

[ec2-user@ip-172-31-46-64 Test-Hub]$ git add commit-test01

[ec2-user@ip-172-31-46-64 Test-Hub]$ git status
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	new file:   commit-test01

[03] Commit 하기

[ec2-user@ip-172-31-46-64 Test-Hub]$ git commit -m "커밋 테스트용 파일 생성"
[main ad9c471] 커밋 테스트용 파일 생성
 Committer: EC2 Default User <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 commit-test01

(03) push 하기

git push [리모트] [브랜치]

[01] 현재, 리모트 확인 하기

git remote
[ec2-user@ip-172-31-46-64 Test-Hub]$ git remote
origin

[02] 현재, 브랜치 확인 하기

git branch
[ec2-user@ip-172-31-46-64 Test-Hub]$ git branch
* main

[03] 코드 변경 사항 확인 하기

git diff [리모트]/[브랜치]
[ec2-user@ip-172-31-46-64 Test-Hub]$ git diff origin/main
diff --git a/commit-test01 b/commit-test01
new file mode 100644
index 0000000..e69de29

[04] 푸시 하기 (변경 사항 서버 반영)

git push [리모트] [브랜치]
[ec2-user@ip-172-31-46-64 Test-Hub]$ git push origin main
Username for 'https://git-codecommit.ap-northeast-2.amazonaws.com': test02-at-382631276292
Password for 'https://test02-at-382631276292@git-codecommit.ap-northeast-2.amazonaws.com':
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 354 bytes | 354.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Validating objects: 100%
To https://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/Test-Hub
   92c7059..ad9c471  main -> main

03) 코드 병합(Merge) / 분기(Branch)

(01) 개념 이해하기

[상황]

– 2명의 개발자(A , B) 가 동일한 파일을 수정한 후, A 가 먼저 커밋 푸시 하고 B 는 그 이후 커밋 푸시를 하려고 하였다.
– B는 A가 푸시한 데이터 이전의 코드를 수정 하였음으로, B 가 커밋하는 경우 A 의 변경사항이 적용되지 않기에 충돌을 일으킨다.

[해결 방법]

  • 브랜치(분기) : A 와 B 의 저장 공간을 분할 하는 방식으로, B는 변경 사항을 별도로 커밋할 수 있다.
    • Branch 를 하는 이유 ?
      • 기존의 운영 시스템에 반영하지 않고 별도의 새로운 기능 개발용으로 사용할 수 있다.
      • 버그를 수정한 이후, 다른 사람들에게 공유하고, 충분한 테스트 이후 운영 시스템에 반영할 수 있다.
    • 다만, Branch는 완전한 해결 방법은 아니다. 결국에 코드를 합치는 경우 충돌은 해결 해야 한다.
  • Merge(병합) : 커밋과 푸시를 하는 담당자는 반드시 이전에 반영된 내역이 있는지 확인하고 커밋을 해야 한다. 이를 위해, 푸시 전 PULL 을 통해 현재 로컬 리포지토리를 최신화 하고, 이 과정에서 충돌되는 사안을 검토하고 Push 하는 것을 Merge 라고 한다.
    (또 다른 의미로는

(02) Branch 생성하기

git checkout -b [브런치 이름] 

[결과]

[ec2-user@ip-172-31-46-64 Test-Hub]$ git checkout -b testbrunch
Switched to a new branch 'testbrunch'
[ec2-user@ip-172-31-46-64 Test-Hub]$ git branch
  main
* testbrunch

(03) Branch 변경 하기

git checkout [브랜치 이름] 

[결과]

[ec2-user@ip-172-31-46-64 Test-Hub]$ git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
[ec2-user@ip-172-31-46-64 Test-Hub]$ git branch
* main
  testbrunch

(04) Branch 변경 테스트

[01] main Branch 에서 기존 파일 변경 후 Commit

[ec2-user@ip-172-31-46-64 Test-Hub]$ ls
Ceph.html  commit-test01  test-branch
[ec2-user@ip-172-31-46-64 Test-Hub]$ echo "aa" >> commit-test01
[ec2-user@ip-172-31-46-64 Test-Hub]$ git add commit-test01
[ec2-user@ip-172-31-46-64 Test-Hub]$ git commit -m "main change"
[main 6dcc006] main change
[ec2-user@ip-172-31-46-64 Test-Hub]$ git push
Username for 'https://git-codecommit.ap-northeast-2.amazonaws.com': test02-at-382631276292
Password for 'https://test02-at-382631276292@git-codecommit.ap-northeast-2.amazonaws.com':
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 316 bytes | 316.00 KiB/s, done.

[02] Test Branch 에서 기존 main 파일에서 변경한 파일 변경 후 커밋 (잘됨)

[ec2-user@ip-172-31-46-64 Test-Hub]$ git checkout testbranch
[ec2-user@ip-172-31-46-64 Test-Hub]$ echo "bb" >> commit-test01
[ec2-user@ip-172-31-46-64 Test-Hub]$ git add commit-test01
[ec2-user@ip-172-31-46-64 Test-Hub]$ git commit -m "test change"
[main 6dcc006] main change
[ec2-user@ip-172-31-46-64 Test-Hub]$ git push origin testbrunch
Username for 'https://git-codecommit.ap-northeast-2.amazonaws.com': test02-at-382631276292
Password for 'https://test02-at-382631276292@git-codecommit.ap-northeast-2.amazonaws.com':
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 520 bytes | 520.00 KiB/s, done.
Total 5 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Validating objects: 100%
To https://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/Test-Hub

-> 충돌이 나지 않은 이유는 둘간의 브런치가 다르기 때문

[03] Merge 해보기

최종적으로 1개로 병합되는 브랜치로 이동 후, merge 한다.

[머지 명령어]

git merge [브런치명]
[ec2-user@ip-172-31-46-64 Test-Hub]$ git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
[ec2-user@ip-172-31-46-64 Test-Hub]$ git merge testbrunch
Auto-merging commit-test01
CONFLICT (content): Merge conflict in commit-test01
Automatic merge failed; fix conflicts and then commit the result.

충돌이 발생 한다.

[04] 충돌 해결하기

충돌하는 파일을 확인한 후, 이상 있는 사항을 수정 후 -> commit -> push 한다.

[ec2-user@ip-172-31-46-64 Test-Hub]$ cat commit-test01
<<<<<<< HEAD
aa
=======
bb
bb
>>>>>>> testbrunch
[ec2-user@ip-172-31-46-64 Test-Hub]$ cat commit-test01
aa
bb
bb
[ec2-user@ip-172-31-46-64 Test-Hub]$ git add commit-test01
[ec2-user@ip-172-31-46-64 Test-Hub]$ git commit -m "머지 브랜치 및 수정"
[main 0400978] 머지 브랜치 및 수정
 Committer: EC2 Default User <[email protected]>
[ec2-user@ip-172-31-46-64 Test-Hub]$ git push
Username for 'https://git-codecommit.ap-northeast-2.amazonaws.com': test02-at-382631276292
Password for 'https://test02-at-382631276292@git-codecommit.ap-northeast-2.amazonaws.com':
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.

03. CodeCommit 보안 정책

01) CodeCommit 의 기본 보안

(01) HTTPS / SSL 관련 보안 통신 제공 및 파일 암호화 제공

기본적으로, CodeCommit 은 리파지토리의 대한 보안으로 HTTPS 와 SSL을 제공하고 있다. 여기에 해당하는 키는, AWS KMS 에 생성되며, 리전별로 생성된다. 또한, 이 키를 활용하여, AWS CODE COMMIT 의 소스도 암호화 되어 관리 된다.

02) 사용자 / 팀단위 접근 제한 가능

  • [IAM] -> [사용자 그룹] -> [권한 정책 연결] 을 통해, 사용자별 권한을 전체를 주거나 제한할 수 있다.
  • 일반적으로, 개인 단위가 아닌 그룹단위의 보안 설정을 수행하며, 정책을 연결하여 관리한다.
    • [개발 팀] : 레파지토리 삭제 방지
    • [Devops] : 전체 접근 가능 등

205 thoughts on “02. AWS CODE COMMIT 이해와 사용방법”

  1. Pingback: grandpashabet
  2. Pingback: grandpashabet

Leave a Comment