参考未在云形成中的用户数据中得到解析

Ref not getting resolved in user data in cloud formation

我正在云形成中创建 EFS 卷并尝试在启动模板的用户数据中引用它。

我尝试了多种在 CF 中使用 Ref 的语法,但每次都出现相同的错误。 我实际上想用 EFS 做不同的事情,但发布的示例代码也不起作用

  ClusterFileSystem:
    Type: AWS::EFS::FileSystem
    Properties:
      Encrypted: true

ClusterLaunchTemplate:
  Type: AWS::EC2::LaunchTemplate
  DependsOn: ClusterFileSystem
  Properties:
    LaunchTemplateName: !Sub ${AWS::StackName}
    LaunchTemplateData:
      ImageId: !Ref 'AMIId'
      SecurityGroupIds: [!GetAtt 'ClusterSecurityGroup.GroupId']
      InstanceType: !Ref 'InstanceType'
      BlockDeviceMappings:
      - DeviceName: "/dev/xvda"
        Ebs:
          VolumeSize: "40"
          VolumeType: "gp2"
          Encrypted: true
      - DeviceName: "/dev/xvdcz"
        Ebs:
          VolumeSize: "22"
          VolumeType: "gp2"
          Encrypted: true
      IamInstanceProfile:
        Name: 'ECSHostInstanceProfile'
      Monitoring:
        Enabled: true
      KeyName: !Ref 'Key'
      UserData:
        Fn::Base64: !Sub |
          #!/bin/bash -xe

          function setup-efs () {

            {
              mkdir -p /ecs-resources/${AWS::StackName}/environment
              EFS_FILE_SYSTEM_ID= !Ref ClusterFileSystem
              echo ${EFS_FILE_SYSTEM_ID} >> /tmp/xyz.txt
            }

这是我得到的错误 -

模板格式错误:未解析的资源依赖性[EFS_FILE_SYSTEM_ID]在模板的Resources块调用UpdateStack操作时发生错误(ValidationError):模板格式错误:未解析的资源依赖性[EFS_FILE_SYSTEM_ID] 在模板的资源块中 - "

您不需要在 !Sub 中使用 !Ref,要获得相同的行为,您只需在 ${}.

中引用逻辑 ID

此外,您需要对 ${EFS_FILE_SYSTEM_ID} 进行转义,因为您想按字面打印它而不是让 !Sub 解析它。

UserData:
  Fn::Base64: !Sub |
    #!/bin/bash -xe

    function setup-efs () {

      {
        mkdir -p /ecs-resources/${AWS::StackName}/environment
        EFS_FILE_SYSTEM_ID= ${ClusterFileSystem}
        echo ${!EFS_FILE_SYSTEM_ID} >> /tmp/xyz.txt
      }

请注意 ${ClusterFileSystem} 的引用和 EFS_FILE_SYSTEM_ID 的大括号内的 !

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

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}.

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-sub.html