kasei_sanのブログ

かせいさんのIT系のおぼえがきです。胡乱の方はnoteとtwitterへ

AWS cli で アカウントに紐づいたロールの切り替えをする方法

課題

セキュリティ上の理由等で、普段管理画面上でロールの切り替えを行っているけれど、それをCLI上でやる場合どうするの?

解決方法

ロール切り替え用のprofileを設定して、それを指定してあげれば良い

~/.aws/config

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

[profile adminrole]
source_profile = default
role_arn = arn:aws:iam::***:role/AdminRole
region = ap-northeast-1

profile adminrole が、ロール切り替え後のprofile。 role_arn に切り替え先のロールを設定する

確認

profile default の場合

$ aws sts get-caller-identity  --profile default
{   
    "UserId": "********",
    "Account": "*********",
    "Arn": "arn:aws:iam::********:user/lcl-kobayashi"
}

arn が、IAMユーザであることがわかる

profile adminrole の場合

$ aws sts get-caller-identity  --profile adminrole
{   
    "UserId": "********:botocore-session-********",
    "Account": "********",
    "Arn": "arn:aws:sts::********:assumed-role/AdminRole/botocore-session-********"
}

adminrole のprofile の場合、Arnが、IAMロールに変わっている

上記 profile adminrole--profile なしで使用したい場合

環境変数 AWS_PROFILE を使用する

export AWS_PROFILE=adminrole

参考