継続は力なり

タイトル通り定期的な更新を心掛けるブログです。

AWS CLI で AWS SSO を使うための方法を確認した

タダです.

AWS SSO を経由で AWS CLI コマンドを実行することが増えてきて設定方法を確認したので備忘録として記事にまとめていきます.

事前準備

事前準備として AWS CLI のバージョンを2を使う必要があり,導入しておきます.

この機能は、AWS CLI バージョン 2 でのみ使用できます。

次の機能は、AWS CLI バージョン 2 を使用している場合にのみ有効です。AWS CLI バージョン 1 を使用している場合には無効です。

docs.aws.amazon.com

SSO の設定

導入後,SSO の設定をしていきます.aws configure sso コマンドを実行します.するとウィザードに沿って設定していき,最初 SSO の認証画面に遷移します.

 aws configure sso
SSO start URL [None]: https://xxx.awsapps.com/start#/
SSO Region [None]: ap-northeast-1
Attempting to automatically open the SSO authorization page in your default browser.

f:id:sadayoshi_tada:20210213131926p:plain f:id:sadayoshi_tada:20210213131936p:plain

認証が終わると,他の後続設定を行って aws s3 ls --profile xxxx のところまでいけば終わりです.

There are 5 AWS accounts available to you.
Using the account ID xxxx
There are 2 roles available to you.
Using the role name "xxxx"
CLI default client Region [ap-northeast-1]:
CLI default output format [json]:
CLI profile name [xxxx]:

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

aws s3 ls --profile xxxx

~/.aws/config に次の定義が追加されます.

[profile xxx]
sso_start_url = https://xxx.awsapps.com/start#/ #SSO のユーザーポータルを指す URL
sso_region = ap-northeast-1 #SSO のリージョン 
sso_account_id = xxx #アカウント
sso_role_name = xxx #スイッチロール名
region = ap-northeast-1 #CLI のデフォルトリージョン
output = json #出力形式

SSO のサインインと一時認証情報の取得

初期設定したあとに SSO のサインインして認証情報の取得を行ってみます.下記のコマンドを実行して行います.

aws sso login --profile xxx
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:

xxxx-xxxx
Successully logged into Start URL: https://xxx.awsapps.com/start#/

まとめ

備忘録として AWS CLIAWS SSO を使った設定と一時認証情報の取得をやってみました.AWS SSO を使っている場合特にお世話になりそうな部分なのでまとめてみました!

関連ドキュメント

docs.aws.amazon.com