my_org(12345)とother_org(67890)の2組織があり、my_orgのユーザがother_orgのdeployロールを引き受けたいときの例。
other_orgにdeployロールを作り、信頼関係の編集で下記のように設定する。my_org_userはmy_orgのIAMユーザで名前が"my_org_user"のもの。
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::12345:root"
},
"Action": [
"sts:AssumeRole",
"sts:TagSession"
],
"Condition": {
"StringLike": {
"aws:username": "my_org_user"
}
}
}
]
}
上記の設定に一致するようにユーザを作成する。名前は"my_org_user"にする。
このとき下記のような設定のポリシーを作成して割り当てる。Resourceにはother_orgのdeployロールのARNを指定する。
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"sts:AssumeRole",
"sts:TagSession"
],
"Resource": "arn:aws:iam::67890:role/deploy"
}
]
}
aws [--profile username 設定してるprofile使いたいなら] sts assume-role
--role-arn [なりたいロールのarn]
--role-session-name [セッション名。適当でいいっぽい?]
--serial-number [仮想デバイスのserial-number]
--token-code [MFAアプリに表示された番号]
# 例 profileに定義しているmasterでadminロールをassumeする
aws --profile master sts assume-role --role-arn arn:aws:iam::1234567890:role/admin --role-session-name abcd --serial-number arn:aws:iam::1234567890:mfa/my-mfa-device --token-code 123456
実行するとcredentialが返ってくる。これらを環境変数にセットすることでcliが常にそのcredentialを使うようになる。これでadminロールとして作業できる。
# ↑でもらったcredentialを環境変数に設定
export AWS_ACCESS_KEY_ID=ExampleAccessKeyID1
export AWS_SECRET_ACCESS_KEY=ExampleSecretKey1
export AWS_SESSION_TOKEN=ExampleSessionToken1