AWS IAM Identity CenterのアカウントでCodeCommitを利用する方法

1498
AWS IAM Identity CenterのアカウントでCodeCommitを利用する方法

AWS IAM Identity Centerで作成されたアカウントを利用していたところ、Code Commitのクローンが普通のHTTPSのアクセスでは行えずに困ったので、やり方を調べた事のメモ書き。

方法

git-remote-codecommitを利用して、awsコマンドでSSOの認証情報を取得する。

手順

git-remote-codecommitのインストール

Python3とpipを先にインストールしておくこと。

pip3 install git-remote-codecommit

aws ssoの設定

以下のコマンドを実行すると、SSOのURLとリージョンを聞かれるので入力する。

aws configure sso

ブラウザに飛ばされるので、そこで認証のステップを続行。終わったらまたシェルに戻ってくる。下記のようなメッセージが表示されて、アカウントやロールを選ぶよう促されるので選んでいく。

The only AWS account available to you is: XXXXXXXXXXX
Using the account ID XXXXXXXXXXX
There are 2 roles available to you.

リージョンやプロファイル名などは適宜入力。

Using the role name "ZZZZZZZZZZZZZ"

CLI default client Region [ap-northeast-1]:
CLI default output format [None]:
CLI profile name [ZZZZZZZZZZZZZ-XXXXXXXXXXX]: sso-profile

以下のようなプロファイルが作成される。

[profile sso-profile]
sso_start_url = https://xxxxx.awsapps.com/start#/
sso_region = ap-northeast-1
sso_account_id = XXXXXXXXXXX
sso_role_name = ZZZZZZZZZZZZZ
region = ap-northeast-1

以降、認証を再度行う場合はaws sso login --profile=プロファイル名を実行する。

CodeCommitからのクローン

CodeCommitの画面で、URLのクローンからHTTPS(GRC)を選ぶと、以下のようなURLがクリップボードにコピーされる。

codecommit::ap-northeast-1://REPO_NAME

これをそのままgit cloneせず、リポジトリ名の前にプロファイル名@を付ける。

codecommit::ap-northeast-1://sso-profile@REPO_NAME