如何在 cloudformations "Fn::Sub" 中转义“${}”

How to escape "${}" in cloudformations "Fn::Sub"

我希望此资源与 !Sub(或 Fn::Sub)内部函数一起使用

Resource: !Sub 'arn:aws:iam::${AWS::AccountId}:user/${aws:username}'

aws:username 是一个 pollicy variable,不能被替换。

一个解决方案是改用 Fn::Join 并编写更多样板代码。

更好:您可以转义 ${aws:username} 以便 !Sub 在这里工作吗? 不幸的是,documentation 没有提到任何关于转义的内容。

您实际上可以使用 ${!}.

转义 $ 个字符

因此您的资源将如下所示:

Resource: !Sub 'arn:aws:iam::${AWS::AccountId}:user/${!aws:username}'

它在字符串参数部分的 docs 中提到。

To write a dollar sign and curly braces (${}) literally, add an exclamation point (!) after the open curly brace, such as ${!Literal}. AWS CloudFormation resolves this text as ${Literal}.