0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

CodeCommit で利用できる3種類のアクセス方法ハンズオン

Posted at

はじめに

AWSが提供するコードリポジトリサービスのCodeCommitを利用するための、アクセス方法までをメモ。
つくったはいいけど、どうやって接続したらいいの?と毎回ググる自分のために。

ちなみに最近ググったのは下記ブログを書いた際
CloudFormationでCodeCommit 作成時にリポジトリを反映する構築ハンズオン


CodeCommitのアクセス方法3種類の違い(ざっくり)

アクセス方法 説明
HTTPS IAM ユーザで設定する__HTTPS Git認証情報__を利用 
SSH IAM ユーザに__公開鍵を登録__して利用
HTTPS(GRC) IAM ユーザ の__権限(アクセスキー/シークレットアクセスキー)__で利用。※ GRC は git-remote-codecommit の略語。


ハンズオン

前提条件

CodeCommitリポジトリ名 AWS Region
【MY-REPOSITORY】 ap-northeast-1 

※リポジトリ名は利用者によって都度変更ください

1.HTTPS

1.1.IAMユーザの認証情報からAWS CodeCommit の HTTPS Git 認証情報でID・Password発行

・マネジメントコンソールから、IAM→左ペイン:ユーザー→タブ:認証情報AWS CodeCommit の HTTPS Git 認証情報認証情報を生成押下してID・Passwordを発行する



1.2.ターミナル画面でリポジトリをCloneする

% git clone https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/【MY-REPOSITORY】
Cloning into '【MY-REPOSITORY】'...

The config profile (CodeCommitProfile) could not be found
Username for 'https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/【MY-REPOSITORY】':  #1.1で取得した【ユーザー名】
Password for 'https://【ユーザー名】@git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/【MY-REPOSITORY】':  #1.1で取得した【パスワード】
remote: Counting objects: 6, done.
Unpacking objects: 100% (6/6), 1.36 KiB | 154.00 KiB/s, done.

※上記設定時 clone をした際に 403エラーが自分の環境では確認出来ました。
認証情報ヘルパーと への HTTPS 接続のトラブルシューティングAWS CodeCommitの手順に従い問題を解決しました。

2.SSH

2.1.自身のPCでパブリックキーとプライベートキーをセットアップする

プライベートキーファイル パブリックキーファイル
codecommit_rsa codecommit_rsa.pub 

※設定時にパスワードの設定が求められます(2.5でリポジトリを取得する際に必要となります)
※任意で名前を変えられます


% ssh-keygen

Generating public/private rsa key pair.

Enter file in which to save the key (/home/user-name/.ssh/id_rsa): 【codecommit_rsa】

Enter passphrase (empty for no passphrase): 【パスワード入力】
Enter same passphrase again: 【同:パスワード入力】

Your identification has been saved in /home/user-name/.ssh/【codecommit_rsa】.
Your public key has been saved in /home/user-name/.ssh/【codecommit_rsa.pub】.
The key fingerprint is:
45:63:d5:99:0e:99:73:50:5e:d4:b3:2d:86:4a:2c:14 user-name@client-name
The key's randomart image is:
+--[ RSA 2048]----+
|        E.+.o*.++|
|        .o .=.=o.|
|       . ..  *. +|
|        ..o . +..|
|        So . . . |
|          .      |
|                 |
|                 |
|                 |
+-----------------+

2.2.コマンドを実行して、パブリックキーファイルの値を表示する

% cat ~/.ssh/codecommit_rsa.pub
上記コマンドを実行して、下記のような乱数を取得する(AWSユーザガイドより抜粋)

2.3. IAMユーザの認証情報からAWS CodeCommit の SSH キーでパブリックキーファイルの値を登録

マネジメントコンソールから、IAM→左ペイン:ユーザー→タブ:認証情報AWS CodeCommit の SSH キーSSHパブリックキーのアップロードを押下して、2.2で取得した値を登録(コピペして貼り付ける)する


下記画像のようにSSH KEY ID 情報が登録される


2.4. .ssh/configファイルを編集して、2.3で表示されたSSH キー IDを登録する

自身のターミナル画面で% vi .ssh/configを押下して、configファイルに設定を登録する

Host git-codecommit.*.amazonaws.com
  User 【SSHキーID】
  IdentityFile ~/.ssh/codecommit_rsa

上記設定後 :xvi を保存して閉じる。
ターミナルで下記コマンドを実行して、設定ファイルへのアクセス権限を変更する
% chmod 600 config

2.5.ターミナル画面でリポジトリをCloneする

2.1.で自身で設定したパスワードを入力を求められる部部があります

% git clone ssh://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/【MY-REPOSITORY】
Cloning into '【MY-REPOSITORY】'...
Enter passphrase for key '/Users/inamurateppei/.ssh/codecommit':【パスワード入力】 
remote: Counting objects: 6, done.
Receiving objects: 100% (6/6), 1.38 KiB | 471.00 KiB/s, done.

3.HTTPS(GRC)

3.1.git-remote-codecommitをインストールする

マネジメントコンソールから、Codecommit→左ペイン:リポジトリ→任意:設定したいリポジトリ選択→上部ボタン:URLのクローン接続のステップを押下→タブ:HTTPS(GRC)を選択


pip install git-remote-codecommitコマンドを実行してgit-remote-codecommit をインストールする

3.2.AWSプロファイルの設定

AWS CLIのインストールする
参照:AWS CLI の最新バージョンをインストールまたは更新します。

3.2.1..aws/configの設定
% vi .aws/config

[profile myaccount]
region = ap-northeast-1
output = json

上記設定後、:xで保存して閉じる

3.2.2..aws/credentialsの設定
[myaccount]
aws_access_key_id = 【自身に払い出されているアクセスキー】
aws_secret_access_key = 【自身に払い出されているシークレットアクセスキー】

3.3.ターミナル画面でリポジトリをCloneする

git clone codecommit::ap-northeast-1://myaccount@【MY-REPOSITORY】コマンドの【】前にある部分(myaccount)部分が、3.2.で設定した自身のAWSプロファイル情報にあたります

% git clone codecommit::ap-northeast-1://myaccount@【MY-REPOSITORY】
Cloning into '【MY-REPOSITORY】'...
remote: Counting objects: 6, done.
Unpacking objects: 100% (6/6), 1.36 KiB | 199.00 KiB/s, done.

さいごに

毎回SSH接続していましたが、改めて3種類全てを実施してみると、アクセスキー/シークレットキーが払い出されている環境であれば、HTTPS(GRC)が一番簡単ですね。
このメモを覗き見しながら、それぞれ条件が異なる案件であってもCodeCommitを利用する際の設定はチョットワカル感だしていけるかと思います。


参考URL

参考:AWS CodeCommit の使い方
参考:git-remote-codecommit(GRC)を使ってCodeCommitのリポジトリに接続してみる

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?