我找不到使用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”。这是为什么?如您所见,这已卸载。我该如何安装呢?
最佳答案
如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