【问题标题】:How to attach and mount volumes to an EC2 instance using CloudFormation如何使用 CloudFormation 将卷附加和挂载到 EC2 实例
【发布时间】:2017-04-25 17:28:48
【问题描述】:

我找不到使用 cloudformation 附加和装载卷的方法。

我可以使用 VolumeAttachment 附加卷;但是,当我在我的 EC2 实例处于运行状态后执行 lsblk 时,我看到这个附加的实例正在被卸载。

有没有办法从 Cloudformation 文件挂载这个实例?我可以使用 linux 命令挂载它,但最好处理来自 cloudformation 的所有内容。

这是我到目前为止所做的:

"MyEc2Instance" : {
   "Type" : "AWS::EC2::Instance",
   "Properties" : {
      "KeyName" : { "Ref" : "KeyName" }
   }
},
    "MyVolume" : {
      "Type" : "AWS::EC2::Volume",
      "Properties" : {
        "Size" : "50",
        "AvailabilityZone" : "xyz"
      }
    },
    "attachment" : {
      "Type" : "AWS::EC2::VolumeAttachment",
      "Properties" : {
        "InstanceId" : { "Ref" : "MyEc2Instance" },
        "VolumeId"  : { "Ref" : "MyVolume" },
        "Device" : "/dev/sdh"
      }
    }

当我在实例上执行lsblk 时,我看到的结果是:

xvda    202:0    0   8G  0 disk
└─xvda1 202:1    0   8G  0 part /
xvdh    202:112  0  50G  0 disk

请注意,即使我将设备名称指定为“sdh”,它仍显示为“xvdh”。这是为什么?正如你所看到的,这是卸载的。我如何安装它?

【问题讨论】:

    标签: amazon-web-services amazon-ec2 amazon-cloudformation


    【解决方案1】:

    正如 helloV 所述,您需要在使用 UserData 启动实例时安装它。我发现 CloudFormation 模板的新 YAML 格式要容易得多,但我也将示例放在 JSON 中。

    JSON:

    "UserData"       : { "Fn::Base64" : { "Fn::Join" : ["", [
        "#!/bin/bash -xe\n",
        "# create mount point directory\n",
        "mkdir /mnt/xvdh\n",
        "# create ext4 filesystem on new volume\n",    
        "mkfs -t ext4 /dev/xvdh\n",
        "# add an entry to fstab to mount volume during boot\n",
        "echo \"/dev/xvdh       /mnt/xvdh   ext4    defaults,nofail 0       2\" >> /etc/fstab\n",
        "# mount the volume on current boot\n",
        "mount -a\n"
    ]]}}
    

    YAML:

    UserData:
        'Fn::Base64': !Sub
          - |
            #!/bin/bash -xe
            # create mount point directory
            mkdir /mnt/xvdh
            # create ext4 filesystem on new volume           
            mkfs -t ext4 /dev/xvdh
            # add an entry to fstab to mount volume during boot
            echo "/dev/xvdh       /mnt/xvdh   ext4    defaults,nofail 0       2" >> /etc/fstab
            # mount the volume on current boot
            mount -a
    

    【讨论】:

    • 我需要增加根音量。这是否适用于根卷以及创建新文件系统?
    • @NHoI 好的,这对我帮助很大。那谢谢啦。一个问题:如果我挂载一个现有的文件系统会有什么问题吗?如果我在这个新磁盘上挂载(例如)/usr,而不是创建 /mnt/xvdh。
    • 不,只要现有的文件系统在/usr下有你需要的。请注意,挂载将隐藏您挂载下的所有内容,它仍然存在,因此如果您卸载原始文件夹内容将返回。
    • 我发现更新fstab有问题,所以我用"echo '/dev/xvdh /mnt/data ext4 defaults,nofail 0 2' | sudo tee --append /etc/fstab \n" 使用 VolumeAttachment 资源的另一个问题是,在创建实例之后才实际附加卷,因此最终的挂载执行什么也不做。最好使用 Instance 资源的 Volumes 属性。
    【解决方案2】:

    附加卷可以在管理程序级别完成,因此您可以使用 CF 附加卷。

    但是安装卷是在操作系统级别,CF 无法知道/执行它。同问How can I create a directory in cloudformation after launching an instance?

    你如何解决这个问题? CF 具有名为 UserData 的 EC2Instance 属性。您提供安装附加卷的命令。这是一个example

    {
       "Type" : "AWS::EC2::Instance",
       "Properties" : {
            ....
            "InstanceType"   : { "Ref" : "InstanceType" },
            "KeyName"        : { "Ref" : "KeyName" },
            "UserData"       : { "Fn::Base64" : { "Fn::Join" : ["", [
                 "#!/bin/bash -xe\n",
                 "yum install -y aws-cfn-bootstrap\n",
    
                 "# Install the files and packages from the metadata\n",
                 "/opt/aws/bin/cfn-init -v ",
                 "         --stack ", { "Ref" : "AWS::StackName" },
                 "         --resource WebServerInstance ",
                 "         --configsets Install ",
                 "         --region ", { "Ref" : "AWS::Region" }, "\n"
            ]]}}
          }
        },
    

    【讨论】:

      猜你喜欢
      • 2017-12-05
      • 1970-01-01
      • 2018-07-20
      • 1970-01-01
      • 2015-05-01
      • 2015-04-10
      • 2019-10-19
      • 2012-03-10
      • 2019-06-22
      相关资源
      最近更新 更多