簡體   English   中英

如何在 cloudformation 策略文檔中引用資源 ARN? (山葯)

[英]How to reference a resource ARN in a cloudformation policy document ? (yaml)

我正在嘗試在 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"

剛弄清楚...使用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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM