在 AWS 云形成中扮演的角色

Assumable role in an AWS cloud formation

我正在使用 AWS 构建一个 cloud formation 堆栈定义以下内容:

  1. 部分资源(为简单起见,下面不再转录)
  2. 一个名为 MyPolicy 的策略允许使用这些资源(为简单起见,下面未转录)
  3. 名为 MyRole 的角色已提交给该策略

堆栈将由管理员创建;一旦创建,目标是允许(从堆栈外部)一些用户假设 MyRole 以便使用多个资源。

我的问题: 应该如何定义角色以便用户可以承担(特定用户将被允许来自堆栈外部)?

在 AWS 帮助页面中,他们给出了 example,其中 "Service": [ "ec2.amazonaws.com" ],这意味着允许 an ec2 实例承担该角色...但我不明白它如何转化为用户,并且没有给出关于该场景的示例。

下面是我使用 JSON 格式的堆栈定义:

{
    "AWSTemplateFormatVersion" : "2010-09-09",
    "Resources" : {
        "MyRole" : {
            "Type": "AWS::IAM::Role",
            "RoleName": "MyRole",
            "AssumeRolePolicyDocument": {
                "Version": "2012-10-17",
                "Statement": [
                    {
                        "Effect": "Allow",
                        "Principal": { "Service": [ "??" ] },
                        "Action": [ "sts:AssumeRole" ]
                    }
                ]
            },
            "ManagedPolicyArns": [ { "Fn::GetAtt" : [ "MyPolicy", "Arn" ] } ],
        }
    }
}

好问题!只需使用您的根用户 ARN 作为委托人。这将允许您控制哪些用户可以使用 IAM 承担该角色。这是一个示例(为了我自己的理智,在 YAML 中):

  AdministratorRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: administrator
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
        - Effect: Allow
          Principal:
            AWS: !Sub arn:aws:iam::${AWS::AccountId}:root
          Action: sts:AssumeRole
          Condition:
            Bool:
              aws:MultiFactorAuthPresent: 'true'
      Path: "/"
      ManagedPolicyArns:
      - arn:aws:iam::aws:policy/AdministratorAccess

  AssumeAdministratorRolePolicy:
    Type: AWS::IAM::ManagedPolicy
    Properties:
      ManagedPolicyName: "AssumeRolePolicy-Administrator"
      Description: "Assume the administrative role"
      PolicyDocument:
        Version: "2012-10-17"
        Statement:
        - Sid: "AssumeAdministratorRolePolicy"
          Effect: "Allow"
          Action:
          - "sts:AssumeRole"
          Resource: !GetAtt AdministratorRole.Arn

  AssumeAdministratorRoleGroup:
    Type: AWS::IAM::Group
    Properties:
      GroupName: "AssumeRoleGroup-Administrator"
      ManagedPolicyArns:
      - !Ref AssumeAdministratorRolePolicy

唯一剩下的就是将用户添加到 AssumeRoleGroup-Administrator 组。

奖励:我添加了一个条件,只允许使用 MFA 登录的用户担任该角色。

此外,只需将您的 ${AWS::AccountId} 换成您拥有的另一个帐户 ID,您就可以轻松地跨帐户代入角色。

假设您希望帐户 B 可以承担访问帐户 A 的角色以具有自定义列表 S3 访问权限和 AWS 管理的 Rout53 完全访问权限:

然后为下面的 CloudFormation 使用帐户 A

Parameters:
  AccountBId:
    Type: String

  environment:
    Type: String
    Default: development
    AllowedValues:
      - development
      - production

Resources:

# Account A:   

  ListS3Access:
    Type: AWS::IAM::ManagedPolicy
    Properties:
      PolicyDocument:
        Version: 2012-10-17
        Statement:
        - Effect: Allow
          Action:
          - "s3:ListAllMyBuckets"
          - "s3:ListBucket"
          - "s3:ListBucketVersions"
          - "s3:GetObject"
          Resource: '*'



  TestRole:
    Type: AWS::IAM::Role
    Description: Test Role
    DependsOn:
      - ListS3Access
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
        - Effect: Allow
          Principal:
            AWS: !Sub 'arn:aws:iam::${AccountBId}:root'
          Action: sts:AssumeRole
      ManagedPolicyArns:
      - !Ref ListS3Access
      - arn:aws:iam::aws:policy/AmazonRoute53FullAccess
      Tags:
        - Key: environment
          Value: !Ref environment

在你 运行 Account A cloudformation 之后,你应该得到 TestRole arn,保存它供 Account B 以后使用

在以下 CloudFormation 的帐户 B 上:

Parameters:
  AccountAId:
    Type: String

  roleName:
    Type: String
    Default: xxxxxxxxx 
Resources:
  AssumeTestRolePolicy:
    Type: AWS::IAM::ManagedPolicy
    Properties:
      Description: "Assume My Test role"
      PolicyDocument:
        Version: "2012-10-17"
        Statement:
        - Sid: "AssumeTestRolePolicy"
          Effect: "Allow"
          Action:
          - "sts:AssumeRole"
          Resource: !Sub 'arn:aws:iam::${AccountAId}:role/${roleName}'

  AssumeTestRoleGroup:
    Type: AWS::IAM::Group
    DependsOn:
      - AssumeTestRolePolicy
    Properties:
      GroupName: "AssumeRoleGroup"
      ManagedPolicyArns:
      - !Ref AssumeTestRolePolicy

当您 运行 帐户 B cloudFormation 时,将您从帐户 A 获得的 TestRole Arn 提供给 roleName

两个cloudFormation部署完成后,登录账号B,将用户分配到组,应该可以使用用户切换角色。