Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 212
user3545018 Avatar asked Aug 04 '14 19:08

user3545018


People also ask

How do I add EBS volume to EC2 instance Linux?

To attach an EBS volume to an instance using the consoleOpen the Amazon EC2 console at https://console.aws.amazon.com/ec2/ . In the navigation pane, choose Elastic Block Store, Volumes. Select an available volume and choose Actions, Attach Volume. For Instance, start typing the name or ID of the instance.

Can you attach volume to AWS EC2 instance?

You can use a block device mapping to specify additional EBS volumes when you launch your instance, or you can attach additional EBS volumes after your instance is running. For more information, see Amazon EBS volumes. You can specify the instance store volumes for your instance only when you launch it.

Which action makes an EBS volume available to an EC2 instance?

The Step-By-Step Re-Mounting Process Select the EBS Volume that you want to attach to an EC2 instance. The EBS volume should be in available status. Enter the Instance ID or the Instance name. Make sure that the Amazon EBS volume and the Amazon EC2 instance are in the same availability zone.


2 Answers

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

like image 108
Jason Avatar answered Sep 19 '22 13:09

Jason


Rather than mounting explicitly in the user-data script, I prefer the following approach:

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

"mount -a" will try to mount all the entries in /etc/fstab and in turn verify the previous append operation.

like image 38
Saurabh Hirani Avatar answered Sep 21 '22 13:09

Saurabh Hirani