Combine Sub and Ref function

0

I have the following Principal ARN but I am getting error ""Policy contains a statement with one or more invalid principals". Is it possible to combine sub and ref function?

            Principal:
               AWS:
                  - !Sub arn:aws:iam::${AWS::AccountId}:role/aws-reserved/sso.amazonaws.com/${AWS::Region}/!Ref AssumedRole 
              Action: '*'
              Resource: '*'
已提問 1 個月前檢視次數 77 次
2 個答案
2
已接受的答案

Yes, and actually you are already doing it but perhaps did not realise. When using !Sub the !Ref (and !GetAtt) syntax switches to using ${...} instead. So for example where you have used ${AWS::Region} is the equivalent of using !Ref AWS::Region so in your example it would be:

        Principal:
           AWS:
              - !Sub arn:aws:iam::${AWS::AccountId}:role/aws-reserved/sso.amazonaws.com/${AWS::Region}/${AssumedRole}
          Action: '*'
          Resource: '*'

I hope this makes sense.

profile pictureAWS
danjhd
已回答 1 個月前
profile picture
專家
已審閱 1 個月前
profile pictureAWS
專家
已審閱 1 個月前
  • It worked! Thank you.

0

Hello.

I don't think it's necessary to combine them.
As stated in the following document, if you specify the logical ID of !Sub as ${AssumedRole}, you can get the same value as !Ref.
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-sub.html#w8aac34c28c69b7

If you specify template parameter names or resource logical IDs, such as ${InstanceTypeParameter}, CloudFormation returns the same values as if you used the Ref intrinsic function. If you specify resource attributes, such as ${MyInstance.PublicIp}, CloudFormation returns the same values as if you used the Fn::GetAtt intrinsic function.

I think the problem can be solved simply by doing the following:

!Sub arn:aws:iam::${AWS::AccountId}:role/aws-reserved/sso.amazonaws.com/${AWS::Region}/${AssumedRole}
profile picture
專家
已回答 1 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南