【问题标题】:How does one escape policy variables in AWS CloudFormation YAML templates如何在 AWS CloudFormation YAML 模板中转义策略变量
【发布时间】:2018-09-16 06:52:51
【问题描述】:

我一直在尝试以 YAML 格式编写一些 AWS CloudFormation 模板。

CloudFormation 在尝试解析 IAM 策略变量(例如 ${aws:username})以及替换时遇到了问题。一个例子如下:

CommonUserGroup:
  Type: AWS::IAM::Group
  Properties:
    GroupName: Common
    Path: /project/
    Policies:
      - PolicyName: ClusterPolicy
        PolicyDocument:
          Version: 2012-10-17
          Statement:
            - Effect: Allow
              Action: "iam:GetAccountPasswordPolicy"
              Resource: "*"
            - Effect: Allow
              Action: "iam:ChangePassword"
              Resource: !Sub 'arn:aws:iam::${AWS::AccountId}:user/${aws:username}'

当使用 CloudFormation 模板尝试创建堆栈时,返回以下错误:

Template validation error: Template format error: Unresolved resource dependencies [aws:username] in the Resources block of the template

如果我手动指定用户名,而不是使用策略变量,例如:

'arn:aws:iam::${AWS::AccountId}:user/bob'

然后错误就消失了。

有没有一种方法可以逃避策略变量,使 CloudFormation 不会尝试将它们解析为模板的一部分?

【问题讨论】:

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


    【解决方案1】:

    经过一番搜索,我发现了以下 Reddit 帖子,其中提到了同样的问题:Creating No MFA No Access policy via YAML template - substitution issues

    对该主题的回复引用了以下 AWS CloudFormation 文档:Filter View: Fn::Sub,其中指出:

    要按字面意思书写美元符号和大括号 (${}),请在左大括号后添加感叹号 (!),例如 ${!Literal}。 AWS CloudFormation 将此文本解析为 ${Literal}。

    这可用于转义策略变量。

    所以在我的问题示例中,${aws:username} 可以在 YAML 中转义为 ${!aws:username}

    【讨论】:

      猜你喜欢
      • 2019-01-29
      • 2018-04-05
      • 1970-01-01
      • 2021-02-07
      • 2017-04-05
      • 1970-01-01
      • 2021-11-23
      • 2020-03-24
      • 2020-08-12
      相关资源
      最近更新 更多