Userdata Cloudformation中资源的参考ID

问题描述

我正在以YAML格式编写cloudformation模板。 现在,我不得不将ebs卷的ID附加到ec2用户数据中。

    Type: AWS::EC2::Volume
    Properties:
      Size: 50
      AvailabilityZone: ap-southeast-1b
      Tags:
      - Key: Name
        Value: Logstash Volume
  LogStashMountPoint:
    Type: AWS::EC2::VolumeAttachment
    Properties:
      InstanceId:
        Ref: LogstashInstance
      VolumeId:
        Ref: LogstashVolume
      Device: "/dev/xvdf"        
  LogstashInstance:
    Type: AWS::EC2::Instance
    Properties:
      IamInstanceProfile:
          Ref: LogstashInstanceProfile                 
      InstanceType: t2.micro
      KeyName: chuongtest
      ImageId: ami-0cd31be676780afa7
      UserData:
        Fn::Base64:
          Fn::Sub:
            - |
              #!/bin/bash -xe
              echo ${LogstashVolume} >> /home/ec2-user/ebsid.txt
              {LogstashVolume: Ref: LogstashVolume}
              touch /home/ec2-user/ebscomplete.txt          
              curl "http://169.254.169.254/latest/Meta-data/instance-id" >> /home/ec2-user/ec2id.txt 
              touch /home/ec2-user/ec2complete.txt           
              touch /home/ec2-user/complete.txt
            - LogstashVolume: !Ref LogstashVolume    
      SecurityGroupIds:
        - Ref: LogstashSecurityGroup
      subnetId: subnet-0d0e0989f57b96389
      Tags:
        - Key: Name
          Value: Logstash Instance 

UserData script with Resource Attribute CloudFormation 我正在关注此链接,但仍然无法正常工作 新实例启动时。 / home / ec2-user中没有任何内容。 我到处看,这是我的最后一回,但没有成功 有人可以帮我吗?

解决方法

您的UserData中至少有一个语法错误

echo ${LogstashVolume}) >> /home/ec2-user/ebsid.txt

应该是

echo ${LogstashVolume} >> /home/ec2-user/ebsid.txt

要进一步调试UserData,请登录实例并检查/var/log/could-init-output.log文件。

p.s。

以下内容也不正确:

{LogstashVolume: Ref: LogstashVolume}

这不是有效的bash命令。