AWS CodeCommit の Git リポジトリにアクセスする 5 つの方法を試してみた

多くの Git リポジトリは、パーソナルアクセストークン (PAT) を使用してアクセスする事が可能だと思います。AWS CodeCommit の場合、自分はいつも git-remote-codecommit を使用する方法を使っています。その他に PAT とは違うアクセス方法が用意されているので、実際に試してみました。

1. git-remote-codecommit を使用する方法

python と aws cli がインストールされている前提となりますが、アクセスキーが使用できる場合はお勧めです。

% pip install git-remote-codecommit

% cat ~/.aws/credentials
[default]
aws_access_key_id = 'yourkey'
aws_secret_access_key = 'yourkey'

% cat ~/.gitconfig   
[user]
    name = test
    email = [email protected]

% git clone codecommit::ap-northeast-1://test
Cloning into 'test'...
remote: Counting objects: 3, done.
Unpacking objects: 100% (3/3), 200 bytes | 50.00 KiB/s, done.

% ls test 
README.md

2. AWS CodeCommit 認証情報ヘルパーを使用する方法

aws cli とアクセスキーが必要になります。

% cat ~/.aws/credentials
[default]
aws_access_key_id = 'yourkey'
aws_secret_access_key = 'yourkey'

% cat ~/.gitconfig   
[user]
    name = test
    email = [email protected]
[credential "https://git-codecommit.*.amazonaws.com"]
    helper = !aws codecommit credential-helper $@ 
    UseHttpPath = true

% git clone https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/test
Cloning into 'test'...
remote: Counting objects: 3, done.
Unpacking objects: 100% (3/3), 200 bytes | 33.00 KiB/s, done.

% ls test 
README.md

3. AWS CodeCommit の HTTPS Git 認証情報を使用する方法

aws cli と ssh 通信が使用できない環境から使用する場合など。

# アクセスキーは無効にしています
% cat ~/.aws/credentials
#[default]
#aws_access_key_id = 'yourkey'
#aws_secret_access_key = 'yourkey'

# aws cli は使用できません
% aws s3 ls
Unable to locate credentials. You can configure credentials by running "aws configure".

% cat ~/.gitconfig   
[user]
    name = test
    email = [email protected]
[credential "https://git-codecommit.*.amazonaws.com"]
	helper = store

% git clone https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/test
Cloning into 'test'...
remote: Counting objects: 3, done.
Unpacking objects: 100% (3/3), 200 bytes | 33.00 KiB/s, done.

% ls test
README.md

# .gitcofig で helper = store を設定しているので平文で保存されました
% cat ~/.git-credentials 
https://'youruser':'yourpassword'@git-codecommit.ap-northeast-1.amazonaws.com

git clone すると VS Code の場合は、下記のようにユーザー名の入力ボックスが画面上に現れます。

4. AWS CodeCommit の SSH 公開キーを使用する方法

aws cli が使用できず、https 通信は AWS CodeCommit 以外で使用していて、ssh 通信が使用できる場合など。

% cat ~/.ssh/id_rsa.pub 
ssh-rsa <yourpublickey>

% cat ~/.ssh/config
Host git-codecommit.*.amazonaws.com
User 'youruser'
IdentityFile ~/.ssh/id_rsa

% git clone ssh://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/test
Cloning into 'test'...
remote: Counting objects: 3, done.
Receiving objects: 100% (3/3), 220 bytes | 73.00 KiB/s, done.

% ls test
README.md

‘youruser’ の箇所は、下記の SSH キー ID を記載します。

5. マネジメントコンソールを使用する方法

下記赤枠に必要な情報を入力して、Git リポジトリにあるファイルを操作します。

タグ: ,