【问题标题】:How to assume AWS role from the same AWS role in CloudFormation template?如何从 CloudFormation 模板中的同一 AWS 角色担任 AWS 角色?
【发布时间】:2020-10-17 09:32:30
【问题描述】:

我将 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 说明了一个从角色 A 承担角色 B 并切换角色的示例。

那么,我有一个问题,GlueRole 是否可以或有效地假设为GlueRole

【问题讨论】:

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


    【解决方案1】:

    由于角色本身没有限制,docs 声明如下

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

    • sts:AssumeRole 动作
    • Resource 元素中角色的 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'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-18
      • 2021-03-03
      • 2018-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-29
      • 2023-03-13
      相关资源
      最近更新 更多