はじめに
現在、AWS CLIを使用する際は、IAM Identity Centerにて短期認証情報を使用することが推奨となっています。
IAMユーザーのアクセスキーを発行する形でしかAWS CLIを使用したことがなかったため、推奨される形での手順を実施します。
設定手順
今回はIAM Identity Centerにて事前にユーザー及びユーザーグループを作成した状態から進めます。
前提条件
- AWS CLIがインストールされていること
- IAM Identity Center 内で SSO 認証へのアクセス権があること
1. aws configure sso
の実行に必要な値を収集
IAM Identity Centerのダッシュボードから設定の概要を確認し、以下の値を控えておく
- リージョン
- AWS access portal URL

2. aws configure sso
でプロファイルを設定
任意のターミナルで aws configure sso
コマンドを実行し、プロファイルの設定を行う。
SSO start URL と SSO region には手順1で確認した値を入力する
$ aws configure sso
SSO session name (Recommended): my-sso
SSO start URL [None]: https://my-sso-portal.awsapps.com/start
SSO region [None]: ap-northeast-1
SSO registration scopes [None]: sso:account:access
実行例
PS C:\Users\test> aws configure sso
SSO session name (Recommended): my-sso
SSO start URL [None]: https://d-xxxxxxxxxx.awsapps.com/start
SSO region [None]: ap-northeast-1
SSO registration scopes [sso:account:access]: sso:account:access
3. ブラウザからの認証
デフォルトのブラウザが開かれ、サインインプロセスが実施される。
ターミナルで表示されたコードと一致していたら、「確認して続行」を押下する。
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://d-xxxxxxxxxx.awsapps.com/start/#/device
Then enter the code:
XJQL-ZXMP
その後、アクセス許可メッセージが表示されたら、「アクセスを許可」を押下する。

4. CLIの環境設定
ターミナルに戻り、AWSアカウントや使用するIAMロール、プロファイルの設定を行う
There are 2 AWS accounts available to you.
> DeveloperAccount, developer-account-admin@example.com (123456789011)
ProductionAccount, production-account-admin@example.com (123456789022)
Using the account ID 123456789011
There are 2 roles available to you.
> ReadOnly
FullAccess
CLI default client Region [None]: ap-northeast-1
CLI default output format [None]: json
CLI profile name [123456789011_ReadOnly]: my-dev-profile
実行例
The only AWS account available to you is: 123456789123
Using the account ID 123456789123
The only role available to you is: AdministratorAccess
Using the role name "AdministratorAccess"
CLI default client Region [None]: ap-northeast-1
CLI default output format [None]: json
CLI profile name [AdministratorAccess-123456789123]: prod-admin
To use this profile, specify the profile name using --profile, as shown:
aws s3 ls --profile prod-admin
CLI profile name に default
を指定するとデフォルトプロファイルとなる。
5. CLIの使用
セットアップが完了したため、AWS CLIでコマンドの実行確認を行う。
aws s3 ls --profile profile_name
S3バケットの一覧がリスト表示された場合は、正常に設定が行われている。
※使用するユーザーにS3のリスト権限がない場合は権限のある別のコマンドで確認すること。
ログインとログアウトの手順
有効期限を超過したりログアウトをしたりした場合は、AWS CLIのコマンドがエラーとなる。
# aws s3 ls
Error loading SSO Token: Token for my-sso does not exist
その際は下記を実施し、ログインする。
ログイン
aws sso login
を実行し、ブラウザから認証を行う。
# aws sso login --profile profile_name
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://d-xxxxxxxxxx.awsapps.com/start/#/device
Then enter the code:
XZZV-LKMB
ターミナルで表示されたコードと一致していたら、「確認して続行」を押下する。

アクセス許可メッセージが表示されたら、「アクセスを許可」を押下する。
その後ターミナルに成功メッセージが表示されればログインの完了
Successfully logged into Start URL: https://d-xxxxxxxxxx.awsapps.com/start
ログアウト
aws sso logout
を実行し、ログアウトする
# aws sso logout
参考文献