0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【AWS】AWS CLIでSSOログインを設定する方法

Last updated at Posted at 2025-02-23

1. はじめに

AWS CLIでSSOログインを設定する手順を簡単にまとめた備忘録です。
アクセスキーを使わずにAWSへログインできるので、セキュリティ的にも安全です。

📌 参考:
本記事は AWS 公式ドキュメントを参考にしています。

2. 本記事の環境

以下の環境で実施しました。

  • PC: M1 MacBook Air
  • OS: macOS Monterey 12.7.6
  • AWS CLI: バージョン 2.11.15
  • エディタ: Visual Studio Code (VSCode)

3. 事前準備

事前に以下を準備してください。

3.1 AWS CLI

以下のコマンドでAWS CLIがインストールされているか確認します。

# コマンド
aws --version
# 出力例
aws-cli/2.11.15 Python/3.9.11 Darwin/x86_64

3.2 SSOが有効なAWS環境があること

AWS SSOを使用するには、AWS IAM Identity Center (旧AWS SSO) が設定済みのAWS環境が必要です。 詳細な設定方法については、AWS公式ドキュメントを参照してください。

4. SSOログインの設定手順

4.1 AWS CLIのSSO設定

以下のコマンドを実行します。

# コマンド
aws configure sso

コマンド実行後、対話形式で設定を進めます。

SSO session name (Recommended):test
SSO start URL [None]: https://d-XXXXXXX.awsapps.com/start/#
SSO region [None]: ap-northeast-1
SSO registration scopes [sso:account:access]: sso:account:access
#入力後に以下が出力されます。
To use this profile, specify the profile name using --profile, as shown:
aws s3 ls --profile {プロファイル名}

4.2 SSOでログイン

以下のコマンドを実行します。

aws sso login --profile {プロファイル名}

#以下が出力されブラウザが自動で開きます。
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-XXXXXX.awsapps.com/start/...

Then enter the code:

#ここに表示されるコードとブラウザに表示されたコードを確認する。
{コード}

#ブラウザ上で許可をすると以下のように出力される。
Successfully logged into Start URL: https://d-XXXXXX.awsapps.com/start/...

5. 接続確認

#以下コマンドを実行し、バケット名が表示されれば接続OK。
aws s3 ls --profile {profile名}
:

なお、--profileを指定しないと以下が出力されます。

Unable to locate credentials. You can configure credentials by running "aws configure".

環境変数にprofile名をセットすることも可能です。

export AWS_PROFILE={profile名}

6. 確認のみ

以降は設定値の確認方法になります。

6.1 プロファイルの確認

以下のコマンドで、SSOの認証情報が正しく適用されているか確認できます。

aws sts get-caller-identity --profile XXXXXX
{
    "UserId": "XXXXXXX",
    "Account": "{AWSアカウントID}",
    "Arn": "arn:aws:sts::{AWSアカウントID}:assumed-role/{フェデレーティッドユーザー名}"
}

6.2 設定ファイルの確認

以下のコマンドで、Configの内容が確認できます。

cat ~/.aws/config

[profile XXXXXX]
sso_session = test
sso_account_id = AWSアカウントID
sso_role_name = ロール名
region = ap-northeast-1
output = json

[sso-session test]
sso_start_url = https://d-XXXXXX.awsapps.com/start/#
sso_region = ap-northeast-1
sso_registration_scopes = sso:account:access

[default]
region = ap-notrheast-1
output = json

7. まとめ

  • AWS CLIでSSOログインを設定すると、アクセスキー不要で安全に利用できる
  • ログインセッションが切れたら aws sso login を再実行
  • AWS_PROFILE 環境変数を設定すると、毎回 --profile を指定しなくてよい
  • プロファイルを活用することで、複数のAWSアカウントを簡単に切り替えられる
    (今回は未実施)

以上、AWS CLIのSSOログイン設定の備忘録でした。

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?