[AWS] AWS CLI でスイッチロールする
AWSアカウントAにユーザを作成し、AWSアカウントBでスイッチロールすることで、AWSアカウントBにはユーザを作成する必要がありません。AWSのコンソール、CLI どちらでもスイッチロールすることができます。
AWSアカウントAにユーザを追加
AWSアカウントAにユーザを作成し、AWS CLIを利用できるように設定します。
AWSコンソールでスイッチロール
| スイッチ元のAWSアカウントID(A) | 111122223333 | 
| スイッチ先のAWSアカウントID(B) | 999988887777 | 
| スイッチ先に設定する Role 名 | SwitchRoleAccountB | 
| スイッチ先に設定する Profile 名 | production | 
AWS CLI のスイッチロール
~/.aws/config に設定を追加します。default は設定済みのため、profile production を追加します。
role_arn にはスイッチ先のARNを指定します。
source_profile はスイッチ元の Profile 名を指定します。
[default]
region = ap-northeast-1
output = json
[profile production]
role_arn = arn:aws:iam::999988887777:role/SwitchRoleAccountB
source_profile = defaultaws configure list –profile production で assume-role となっていればOKです。
$ aws configure list --profile production
      Name                    Value             Type    Location
      ----                    -----             ----    --------
   profile               production           manual    --profile
access_key     ****************xxxx      assume-role
secret_key     ****************xxxx      assume-role
    region                <not set>             None    NoneAWS CLI を利用する場合は、–profile production と Profile を指定すれば、スイッチロールされた権限で利用することが可能です。
