LoginSignup
16
12

More than 1 year has passed since last update.

AWS CLI で AWS SSO にログインする

Posted at

初めに

この記事では以下を参考にして AWS CLI を用いて AWS SSO にログインする手順を書きます。

手順

EC2 インスタンスを起動し、aws configure sso を実行します。インスタンスに AWS SSO のロールをアタッチする必要はありません。URL については、以下の画像にある URL を入力します。

50.png

[ec2-user@ip-xxx-xxx-xxx-xxx ~]$ aws configure sso
SSO start URL [None]: https://Identity_store_ID.awsapps.com/start
SSO Region [None]: ap-northeast-1

続いて以下のように表示されるので https://device.sso.ap-northeast-1.amazonaws.com/ をブラウザで開きます。XXXX-YYYY は承認するためのコードなのでコピーします。

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-YYYY

ブラウザで上記の URL を開くと、SSO ユーザーでのサインインを求められます。

0.png

サインイン成功後、以下のようにコピーしたコードを入力します。

1.png

「Allow」をクリックします。

2.png

承認後、以下のように表示されるのでブラウザを閉じます。

3.png

承認成功後、アカウントを選択します。

There are 2 AWS accounts available to you.
> Account_1, Account_1@example.mail.com (111111111111)
  Account_2, Account_2@example.mail.com (222222222222)

アカウントにアクセス許可セットが複数ある場合はどのロールを使用するかを選択できます。

There are 2 roles available to you.
> ViewOnlyAccess
  PowerUserAccess

デフォルトリージョン、フォーマット、プロファイル名を入力します。

CLI default client Region [None]: ap-northeast-1
CLI default output format [None]: json
CLI profile name [PowerUserAccess-111111111111]: sso-profile

プロファイルの作成後、以下のように表示されます。

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

aws s3 ls --profile sso-profile

aws sts get-caller-identity --profile sso-profile を実行してユーザー情報を確認してみます。

[ec2-user@ip-xxx-xxx-xxx-xxx ~]$ aws sts get-caller-identity --profile sso-profile
{
    "UserId": "XXXXXXXXXXXXXXX:test-user-1",
    "Account": "111111111111",
    "Arn": "arn:aws:sts::111111111111:assumed-role/AWSReservedSSO_PowerUserAccess_xxxxxxxxxxx/test-user-1"
}

以下は ~/.aws/config ファイルです。

~/.aws/config
[profile sso-profile]
sso_start_url = https://Identity_store_ID.awsapps.com/start
sso_region = ap-northeast-1
sso_account_id = 111111111111
sso_role_name = PowerUserAccess
region = ap-northeast-1
output = json

ログアウトするには以下のコマンドを実行します。

aws sso logout

ログアウト後、aws sts get-caller-identity --profile sso-profile を実行してユーザー情報を確認してみます。

[ec2-user@ip-xxx-xxx-xxx-xxx ~]$ aws sts get-caller-identity --profile sso-profile

Error loading SSO Token: The SSO access token has either expired or is otherwise invalid.

再ログインするには以下のコマンドを実行します。

aws sso login --profile sso-profile

参考記事

16
12
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
16
12