如何从CloudFormation模板中的同一AWS角色承担AWS角色?

时间:2020-10-17 01:32:30

标签: amazon-web-services amazon-cloudformation amazon-iam

我正在使用IAM角色进行粘合工作以进行一些数据处理,要完成此任务,我需要承担执行粘合角色的角色。

例如,在以下cloudformation模板中,IAM::Policy有权从Dynamo数据库表中进行查询并从s3存储桶中获取对象。

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'

Resources: 

  GlueAccessPolicy:
    Type: AWS::IAM::Policy
    Properties:
      Roles:
        - !Ref GlueRole
      PolicyName: glue_access_policy
      PolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Action: 's3:getObject'
            Resource:
              - 's3_bucket_arn'
          - Effect: Allow 
            Action: 
              - 'dynamodb:DescribeTable'
              - 'dynamodb:Query'
            Resource:
              - 'dynamo_table_arn'

  GlueRole: 
    Type: 'AWS::IAM::Role'
    Properties: 
      ManagedPolicyArns: 
        - arn:aws:iam::aws:policy/service-role/AWSGlueServiceRole
      AssumeRolePolicyDocument: 
        Version: 2012-10-17
        Statement: 
          - Effect: 'Allow'
            Principal: 
              Service:
                - 'glue.amazonaws.com'
            Action:
              - 'sts:AssumeRole'

现在,此question说明了一个示例,该角色假定角色B由角色A转换角色。

所以,我有一个问题, GlueRole承担GlueRole是否可能或有效?

1 个答案:

答案 0 :(得分:0)

由于角色本身没有限制,docs声明以下内容

授予用户担任角色权限的策略必须包含对以下内容具有“允许”作用的语句:

  • sts:AssumeRole动作
  • 资源元素中角色的Amazon资源名称(ARN)

直接将此策略添加到CloudFormation模板上的AWS::IAM::Policy资源中即可。

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'

Resources: 

  GlueAccessPolicy:
    Type: AWS::IAM::Policy
    Properties:
      Roles:
        - !Ref GlueRole
      PolicyName: glue_access_policy
      PolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Action: 'sts:AssumeRole'
            Resource: !GetAtt GlueRole.Arn

  GlueRole: 
    Type: 'AWS::IAM::Role'
    Properties: 
      ManagedPolicyArns: 
        - arn:aws:iam::aws:policy/service-role/AWSGlueServiceRole
      AssumeRolePolicyDocument: 
        Version: 2012-10-17
        Statement: 
          - Effect: 'Allow'
            Principal: 
              Service:
                - 'glue.amazonaws.com'
            Action:
              - 'sts:AssumeRole'