1
2

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-CLIでスイッチロールしたい

Last updated at Posted at 2024-08-16

背景

AWSのアカウント構成

  • ユーザ管理用のアカウント
  • 開発環境
  • 本番環境

のような構成になっており,それまでは各AWSアカウントごとにaws-cli用のアカウントを作ってaws-cli実行時に --profileで指定していましたが,AWS-CLIでもスイッチロールができることを知ったのでやり方を記載します。


やり方

  1. GUIでユーザ管理用のアカウント(スイッチロール元)にログイン

  2. IAMユーザの認証情報を取得して~/.aws/credentialsに記載します。ここではdefaultにしていますが,別の値でも問題ないです。

    [!NOTE]
    defaultにするとその認証情報が--profileで明示的に指定しない際に使われます。

    # ~/.aws/credentials
    [default]
    aws_access_key_id=xxxxxxxxxxxxxxxxxxxx
    aws_secret_access_key=xxxxxxxxxxxxxxxxxxxxxx
    
  3. 必要ならばMFAの設定をします。(組織のポリシー等で強制されていることもある)
    MFA作成時に決めたMFA device nameを後に~/.aws/configに記載します。
    image.png

  4. スイッチロール用のconfigを記載します。

    # ~/.aws/config
    [profile super-admin-config] # 好きな名前をつける
    region = ap-northeast-1
    role_arn = arn:aws:iam::<スイッチロール後のAWSアカウントID>:role/<Role名>
    mfa_serial = arn:aws:iam::<MFAを登録したAWSアカウントID>:mfa/<MFAのdevice name>
    source_profile = default # ~/.aws/credentialsのプロファイル名を指定
    
  5. 切り替わっているか確認します。

    aws sts get-caller-identity --profile super-admin-config
    {
    "UserId": "xxxxxxxxxxxxxxxxxx",
    "Account": "<スイッチロール後のAWSアカウントID",
    "Arn": "arn:aws:iam::<スイッチロール後のAWSアカウントID>:user/<ユーザ名>"
    }
    

感想

aws-cliでスイッチロールすることで

  • 複数の認証情報の管理
  • 複数のaws-cli用のアカウント管理
    をする必要がなくなりつつ,cliで快適に作業できるようになりました。
1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?