【问题标题】:How to reference a resource ARN in a cloudformation policy document ? (yaml)如何在 cloudformation 策略文档中引用资源 ARN? (yaml)
【发布时间】:2020-03-24 14:09:15
【问题描述】:

我正在尝试在 cloudformation (yaml) 中定义角色和用户之间的信任关系策略文档。

为了在角色的AssumeRolePolicyDocument 中指定用户的 ARN,我想从实际的 cloudformation 资源中引用 ARN,而不必构造 ARN 字符串。

但是,它不起作用。当我使用 !Ref rUser 时,在创建 cloudformation 堆栈时出现“策略中的主体无效”的错误。

当我只是将 ARN 字符串粘贴为值时,它可以工作。是因为!Ref rUser 返回一个用户对象类型并且不计算为字符串吗?如果是这样,我如何从资源中引用 ARN?

代码:

  rUser:
    Type: "AWS::IAM::User"
    Properties:
      UserName: "my_user"

  rRole:
    DependsOn: rRole
    Type: "AWS::IAM::Role"
    Properties:
      RoleName: "my_role"
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          -
            Effect: "Allow"
            Principal:
              AWS:
                # this does not work, gives error "Invalid Principal in policy"
                - !Ref rUser
                # this does work (just hard coding the ARN string):
                # - "arn:aws:iam::111111111111:user/my_user"
            Action:
              - "sts:AssumeRole"

【问题讨论】:

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


    【解决方案1】:

    刚刚想通了...使用GetAtt 函数非常简单:

      rRole:
        DependsOn: rRole
        Type: "AWS::IAM::Role"
        Properties:
          RoleName: "my_role"
          AssumeRolePolicyDocument:
            Version: "2012-10-17"
            Statement:
              -
                Effect: "Allow"
                Principal:
                  AWS:
                    - !GetAtt rExternalUser.Arn  # use the GetAtt function
                Action:
                  - "sts:AssumeRole"
    

    【讨论】:

    • 您知道如何在打字稿领域使用!GetAtt 吗?
    猜你喜欢
    • 2021-02-10
    • 2021-06-01
    • 2022-01-08
    • 2020-04-24
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    • 2021-03-09
    • 2019-08-31
    相关资源
    最近更新 更多