AWS CLIとaws mfaとswitch role

default roleはあんまりつけたくないので
aws configure listでなにもでてこないのがよい

browserからswitch roleできることは確認しておく

switch roleにVPNが必要で、VPN接続忘れてエラーが出た(1敗)
switch roleにmfaが必要で、mfa設定してなくてエラーが出た(1敗)
mfa設定後に再ログインしていなかったのでエラーが出た(1敗)

access key, secret access keyはiamの画面から取る
https://us-east-1.console.aws.amazon.com/iamv2/home?region=ap-northeast-1#/security_credentials?section=IAM_credentials

create access key

$ aws configure --profile sanemat@example-bastion

この時点の~/.aws/config

[profile sanemat@example-bastion]
region = ap-northeast-1

~/.aws/credentials

[sanemat@example-bastion]
aws_access_key_id     = _AWS_ACCESS_KEY_ID_
aws_secret_access_key = _AWS_SECRET_ACCESS_KEY_

次が awsmfa
https://github.com/future-architect/awsmfa

$ awsmfa --serial-number arn:aws:iam::YOUR_BASTION_AWS_ID:mfa/sanemat --profile sanemat@example-bastion --mfa-profile-name sanemat@mfa-example-bastion 000000

serial-numberにmfaのresource

最後に ~/.aws/config 手書き

[profile other-role@example-dev]
role_arn = arn:aws:iam::YOUR_DEV_ENV_AWS_ID:role/other-role
source_profile = sanemat@mfa-example-bastion

$ aws sts get-caller-identity --profile other-role@example-dev

これでとれるにゃんね

$ aws sts get-caller-identity --profile other-role@example-dev
{
  "UserId": "AWS_ID:example-00000",
  "Account": "_AWS_ID_",
  "Arn": "_ARN_"
}

広告

コメントを残す

以下に詳細を記入するか、アイコンをクリックしてログインしてください。

WordPress.com ロゴ

WordPress.com アカウントを使ってコメントしています。 ログアウト /  変更 )

Facebook の写真

Facebook アカウントを使ってコメントしています。 ログアウト /  変更 )

%s と連携中

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください