可以创建策略并将策略共享到跨多个帐户的角色

Possible to create a policy and share the policy to role across multiple accounts

我正在尝试在一个 AWS 账户中创建一项策略,并且需要将该策略共享给多个账户(Prod、Dev、Sandbox)中的一个角色。

并且我可以手动添加AWS帐号并将AWS Managed Policy分配给角色并且需要创建多个角色。

我们怎样才能做到这一点?

这是我写的代码

 AWSTemplateFormatVersion: '2010-09-09'
Description: 'Create a role that authorizes access to users in another account'
Metadata:
  Version: 0.7
Parameters:
  RoleName:
    Type: String
    Default: R_EC2-Describe-Instance
  MainAccountId:
    Type: String
    Description: >-
     Include the Managed Services Account ID(the account ID where the Main VPC is registered)
    Default: 111111111111
    MaxLength: 12
    MinLength: 12 

Resources:
  AssumeRole:
    Type: AWS::IAM::Policy
    Properties:
      RoleName: !Ref RoleName
      Policies:
          -
            PolicyName: "CoreSVC-Describe-EC2"
            PolicyDocument:
              Version: "2012-10-17"
              Statement:
                -
                  Effect: "Allow"
                  Action: 
                   - 'sts:AssumeRole'
                  Resource: !Join [ "", [ "arn:aws:iam::", !Ref MainAccountId, ":role/R_EC2-Describe-Instance" ] ]
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        ManagedPolicyName: 
          - "arn:aws:iam::aws:policy/AmazonEC2ReadOnlyAcess"  
        Statement:
        - Effect: Allow
          Principal: 
            "AWS": !Join [ "", [ "arn:aws:iam::", !Ref MainAccountId, ":root" ] ]
          Action:
          - sts:AssumeRole
          Condition: {}

您不能与其他帐户共享 IAM 策略,因为它没有允许它的资源策略。

您共享的代码示例正在与多个帐户共享 IAM 角色,这可以通过角色的 resource/trust 政策实现。

如果您想在多个帐户之间共享相同的策略,那么您应该使用 CloudFormation StackSets(如 @ervin-szilagyi 所述),或其他一些基础设施即代码方法。

如果您想与其他帐户共享该角色,那么您已经完成了。所缺少的只是那些可以访问 sts:AssumeRole 操作的帐户中的角色,以便它可以承担您在代码中共享的角色。