これは何?
AWS-CLIを使って認証する方式をまとめたもの
この記事について
前提
AWS CLIのインストールは完了している前提。
🙅非推奨: IAM Userでアクセスする場合
- AWSのコンソールからIAMユーザを作成する。この際にアクセスキーとシークレットキーをメモっておく
- クレデンシャルを設定する
aws configure
# regionはデフォルトは最も近い場所,outputはjsonにしておく
~/.aws/config
[hoge1] # 任意の値
region = ap-northeast-1
output = json
~/.awx/credentials
[hoge1] # ~/.awx/configにあわせる
aws_access_key_id = AKxxxxxxxxxxxxxxxxxx
aws_secret_access_key = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
- 疎通確認
aws sts get-caller-identity --profile hoge1
補足: スイッチロールしたい場合
- スイッチロール元のクレデンシャルと同じものを使う。
~/.aws/credentials
はそのままで良い。 - AWSコンソールからスイッチロール元のアカウントにログインしてIAMユーザの設定を行う。
- 新しいprofileを
~/.aws/config
に追加する
~/.aws/config
[profile hoge1]
region = ap-northeast-1
output = json
# 追加したプロファイル
[profile fuga] # 好きな名前をつける
region = ap-northeast-1
role_arn = arn:aws:iam::<スイッチロール後のAWSアカウントID>:role/<Role名>
mfa_serial = arn:aws:iam::<スイッチロール元のAWSアカウントID>:mfa/<MFAのdevice name> # 必要な場合のみ
source_profile = hoge1 # hoge1で設定されてる ~/.aws/credentialsを使う
- 疎通確認
aws sts get-caller-identitiy --profile fuga
🙆推奨: アクセスキーを使わずにIAM Identity Centerを使う
- AWSコンソールからブラウザにアクセスし、IAM Identity Centerの設定ページから「AWS access portal URL」
- SSOを使用した設定を行う。設定すると
~/.aws/config
に追加される
aws configure sso
SSO session name (Recommended): test
SSO start URL [None]: https://xxxxxxxxx.awsapps.com/start
SSO region [None]: ap-northeast-1
SSO registration scopes [sso:account:access]:
-
~/.aws/config
からプロファイル名を調べる
~/.aws/config
[profile AdministratorAccess-xxxxxxxxxx]
sso_session = test
sso_account_id = xxxxxxxxxx
sso_role_name = AdministratoAccess
[sso-session test]
sso_start_url = https://xxxxxxxx.awsapps.com/start
sso_region = ap-northeast-1
sso_registration_scopes = sso:account:access
- 疎通確認
aws sts get-caller-identitiy --profile AdministratorAccess-xxxxxxxxxx
補足: 上記を試した環境について
いわゆるおひとりさまOrganizationsというやつ。SSOするならついでにOrganizationsにしてもいいじゃないという発想。無料だし
以下は作業内容(抜粋)
- Organizationsを有効にする
- IAM Identity Centerのセットアップ
- コンソールから有効にする
- ユーザの作成 -->
aws configure sso
した際やブラウザ経由でコンソールにログインする時に使うユーザ - groupの作成
- 許可セットの作成(Administorator Accessをとりあえずつけた。あまり良くないかも)
- 許可セットをAWSのアカウントに紐付ける(許可セットの画面から行える)
- この際に作成したgroupに紐づけする
- MFAを有効化する
- IAM Identity Centerの設定ページから
AWS access portal URL
をメモしておく。
aws configure ssoした
参考: GitHub ActionsでSSOしたい場合
TODO: SSO + スイッチロール
使ったことがないので後で書く
後書き
結構いろんなパターンがありそうなので網羅してかくの大変そうなので一旦こんなもんで