AWS IAM Identiy Center利用環境下で、メンバーアカウントのCodeCommitのリポジトリをクローンしたみた

2023.11.20

はじめに

IAM Identiy Center利用環境下で、メンバーアカウントのCodeCommitのリポジトリをクローンしてみました。

IAM Identiy Center利用環境下で、CodeCommitのリポジトリをクライアント端末にクローンする場合、IAMユーザーの永続的な認証情報であるアクセスキーを使うのではなく、IAM Identity Centerから払い出される一時的な認証情報を利用する方がよいです。

今回は、AWS CLIからIAM Identity Centerとの認証後にCodeCommitのリポジトリをクローンする手順をまとめました。

事前設定

  • IAM Identiy Centerの設定
    • IAM Identiy Centerは設定済み
    • 許可セットは、CodeCommitを利用するメンバーアカウントに対して、AWSAdministratorAccess権限をユーザーに割り当てます
    • AWSアクセスポータルのURLとリージョンをコピーしておきます
  • クライアント端末に、AWS CLI V2をインストール済み

メンバーアカウントでリポジトリを作成

メンバーアカウントで、CodeCommitのリポジトリを作成します。

リポジトリ内にtest.txtファイルを追加しておきます。

IAM Identity Centerにログイン

クライアント端末のターミナルで、AWS CLIでIAM Identity Centerにログインします

$ aws configure sso
SSO session name (Recommended): なんでもよいですが、SSOログイン時のユーザー名を推奨(今回は、cm-ssoにしました)
SSO start URL [None]: https://d-xxxxxxxx.awsapps.com/start(IAM Identity CenterポータルページURL)
SSO region [None]: ap-northeast-1
SSO registration scopes [sso:account:access]: 何も入力せずEnter

Attempting to automatically open the SSO authorization page in your default browser.
If the browser does not open or you wish to use a different device to authorize this request, open the following URL:

https://device.sso.ap-northeast-1.amazonaws.com/

Then enter the code:

XLVN-XBWS

最後にXLVN-XBWSという一時的なコードが出力された後、ブラウザに強制遷移されます。

ターミナルのXLVN-XBWSというコードと、ブラウザのコードが同一であることを確認し、Confirm and continueをクリックします。

ユーザー名、パスワード、MFAコードを入力します。

Allowをクリックすると、ターミナルに戻ります。

下記の中から、CodeCommitのアカウントIDとユーザー名を選択します。許可セットが1つであれば、選択せずに次に進みます。

There are 3 AWS accounts available to you.
> ユーザー名, メールアドレス (アカウントID)     
  ユーザー名, メールアドレス (アカウントID)
  ユーザー名, メールアドレス (アカウントID)

選択したアカウントの許可セットを選択します(今回はAWSAdministratorAccessを選択)。許可セットが1つであれば、選択せずに次に進みます。

There are 2 AWS accounts available to you.
Using the account ID アカウントID
There are 2 roles available to you.
> ReadOnly
  AWSAdministratorAccess
CLI default client Region [None]:ap-northeast-1
CLI default output format [None]:そのままエンター
CLI profile name [AWSAdministratorAccess-アカウントID]:そのままでもよい

To use this profile, specify the profile name using --profile, as shown:

aws s3 ls --profile プロファイル名(AWSAdministratorAccess-アカウントID)

最後のメッセージにコマンド例がありましたので、問題なく実行されるか確認してみましょう。

$ aws s3 ls --profile プロファイル名(AWSAdministratorAccess-アカウントID)

ターミナルの/.aws/configにプロファイルが作成されていました。

$ cat ~/.aws/config

[profile AWSAdministratorAccess-アカウントID]
sso_session = cm-sso
sso_account_id = アカウントID
sso_role_name = AWSAdministratorAccess(SSO session nameで付けた名前です)
region = ap-northeast-1

//IAM Identity Centerの設定
[sso-session cm-sso]
sso_start_url = https://d-xxxxxxxx.awsapps.com/start
sso_region = ap-northeast-1
sso_registration_scopes = sso:account:access

クライアント端末にリポジトリをクローン

AWSが提供する、CodeCommitリポジトリにアクセス可能なコマンドラインツールであるgit-remote-codecommitをインストールします。

$ pip install git-remote-codecommit
// もしくは
$ pip3 install git-remote-codecommit

下記のコマンドでリポジトリをクローンできます。

$ git clone codecommit://プロファイル名@リポジトリ名 ローカルの保存パス
// プロファイルにリージョンを指定していない場合
$ git clone codecommit::ap-northeast-1://プロファイル名@リポジトリ名 ローカルに保存するパスと新規ディレクトリ名

//実際に実行した内容
$ git clone codecommit://AWSAdministratorAccess-アカウントID@cm-hirai cm-hirai-repo

Cloning into 'cm-hirai'...
remote: Counting objects: 3, done.
Unpacking objects: 100% (3/3), 187 bytes | 93.00 KiB/s, done.

$ ls cm-hirai-repo
test.txt

リポジトリをクローンできました!

IAM Identity Centerに再ログイン

再ログインする際は、下記のコマンドを実行し、同じように進めるだけです。

$ aws sso login --profile プロファイル名(AWSAdministratorAccess-アカウントID)

Attempting to automatically open the SSO authorization page in your default browser.
If the browser does not open or you wish to use a different device to authorize this request, open the following URL:

https://device.sso.ap-northeast-1.amazonaws.com/

Then enter the code:

NFWL-LJKL

参考