为什么我需要 AWS CloudFormation 中的额外元素来插值变量

Why do I need an extra element in AWS CloudFormation for interpolating variables

我正在尝试使用以下方法将用户数据传递到在 Cloudformation 中创建的 EC2 实例:

      UserData: !Base64
        'Fn::Join':
          - ''
          - - |
              #!/bin/bash -xe
              echo '
            - !Ref 'someVar'
            - |
              ' > /tmp/some-content

有效。这是扩展的用户数据(顺便说一句,我也不知道为什么 echo ' 之后有一个新行但这不是主要问题):

#!/bin/bash -xe
echo '
some string passed as parameter' > /tmp/some-content

但是如果我从 !Ref 部分删除 -,它会将 !Ref 视为字符串:

      UserData: !Base64
        'Fn::Join':
          - ''
          - - |
              #!/bin/bash -xe
              echo '
              !Ref 'License'
              ' > /tmp/some-content

这里是扩展后的用户数据。

#!/bin/bash -xe
echo '
!Ref 'someVar'
' > /tmp/some-content

为什么会这样?这是 cloudformation 内部的东西吗,只有当它们作为列表中的元素单独存在时才扩展 !Ref 变量?

这样做岂不是更简单:

  UserData: 
    Fn::Base64: !Sub |
          #!/bin/bash -xe 
          echo "${someVar}" > /tmp/some-content

基本上不用用Join吃亏。使用 !Sub | ,这样使用起来要容易得多。代码可读性更强,更易于管理。