Automounting ebs volume in linux-Ec2 instance using cloud formation?

user3545018 picture user3545018 · Aug 4, 2014 · Viewed 8.1k times · Source

Wrote one CFT to create redhat instance with two ebs volume attached. And need automount or formatted the ebs volume from the cft itself.

CFT:

"BlockDeviceMappings": [
    {
        "DeviceName": "/dev/sda1",
        "Ebs": {
            "DeleteOnTermination": "true",
            "VolumeSize": "150",
            "VolumeType": "standard"
        }
    },
    {
        "DeviceName": "/dev/sdm",
        "Ebs": {
            "DeleteOnTermination": "true",
            "VolumeSize": "1000",
            "VolumeType": "standard"
        }
    }
]

Need to mount "DeviceName" : "/dev/sdm", this volume automatically.

Answer

Jason picture Jason · Oct 15, 2014

You will need to add a small script to the UserData property of your instance or launch configuration that this BlockDeviceMappings is associated with. UserData is executed the first time the instance boots. The devices will automatically be remounted when the instance reboots using /etc/fstab.

    "UserData" : { "Fn::Base64" : { "Fn::Join" : [ "", [
      "#!/bin/bash -v\n",
      "mkfs -t ext4 /dev/xvdm\n",
      "mkfs -t ext4 /dev/xvda1\n",
      "mkdir /opt/mount1 /opt/mount2\n",
      "mount /dev/xvdm /opt/mount1\n",
      "mount /dev/xvda1 /opt/mount2\n",
      "echo \"/dev/xvdm /opt/mount1 ext4 defaults,nofail 0 2\" >> /etc/fstab\n"
      "echo \"/dev/xvda1 /opt/mount2 ext4 defaults,nofail 0 2\" >> /etc/fstab\n"
    ]]}}

More information: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-using-volumes.html