Skip to content

Instantly share code, notes, and snippets.

@buzztaiki
Last active December 30, 2020 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buzztaiki/2c9c897cb805bfe83c0f22d25debda84 to your computer and use it in GitHub Desktop.
Save buzztaiki/2c9c897cb805bfe83c0f22d25debda84 to your computer and use it in GitHub Desktop.
AWS の Assume Role

AWS の IAM ユーザーに一時的に Role を割り当てる。assume role で「役割を引き受ける」だから、このアクション自体の主体はユーザーという事。多分。

assume-role するのに必要なのは以下:

  • 割り当て先のロール
  • 割り当てを許可するポリシー

割り当て先のロール

https://docs.aws.amazon.com/ja_jp/IAM/latest/UserGuide/id_roles_create_for-user.html

割り当て先のロールをつくる。信頼ポリシーでどのアカウントからの AssumeRole なら許可するかを書く。つまり、自アカウントから AssumeRole する場合は自アカウントの ID を書く。

{
  "Version": "2012-10-17",
  "Statement": [
      {
          "Effect": "Allow",
          "Principal": { "AWS": "arn:aws:iam::123456789012:root" },
          "Action": "sts:AssumeRole",
          "Condition": { "Bool": { "aws:MultiFactorAuthPresent": "true" } }
      }
  ]
}

割り当てを許可するポリシー

https://docs.aws.amazon.com/ja_jp/IAM/latest/UserGuide/id_roles_use_permissions-to-switch.html

AssumeRole を許可するポリシーを作る。Resource にどこへの AssumeRole を許可するのかを書く。 このポリシーが与えられたユーザーは、Resource で指定されたアカウントのロールで AssumeRole する事ができる。 Resource 指定がない場合は、引受先のロールで許可されていれば、どのロールになる事もできる。

{
  "Version": "2012-10-17",
  "Statement": {
    "Effect": "Allow",
    "Action": "sts:AssumeRole",
    "Resource": "arn:aws:iam::123456789012:role/target-iam-role"
  }
}

このポリシーをユーザーにアタッチする。

AWS CLI

https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/cli-configure-role.html

profile

[profile role-with-mfa]
region = ap-northeast-1
output = json
source_profile = cli-user
mfa_serial = arn:aws:iam::123456789012:mfa/cli-user
role_arn= arn:aws:iam::123456789012:role/target-iam-role
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment