Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to attach and mount volumes to an EC2 instance using CloudFormation

I can't find a way to attach and mount volumes using cloudformation.

I can attach a volume using VolumeAttachment; however, when I do lsblk after my EC2 instance is in running state, I see this attached instances as being unmounted.

Is there a way to mount this instance from Cloudformation file? I can mount this using linux commands but would be much better to handle everything from cloudformation instead.

Here is what I've so far:

"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"
      }
    }

And when I do lsblk on the instance, this is the result i see:

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

Notice even though I specified device name to be 'sdh' it shows attached as 'xvdh'. Why is that? And as you can see this is unmounted. How do I mount this?

like image 357
user1801879 Avatar asked Dec 10 '16 09:12

user1801879


People also ask

Can you attach volume to AWS EC2 instance?

You can attach a volume to an instance using one of the following methods. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/ . In the navigation pane, choose Volumes. Select the volume to attach and choose Actions, Attach volume.

How do you attach an EBS volume to an EC2 instance?

The Step-By-Step Re-Mounting ProcessSelect 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.


1 Answers

As mentioned by helloV you will need to mount it when the instance launches using UserData. I find the new YAML format for CloudFormation templates is much easier but I've put the example in JSON too.

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
like image 161
NHol Avatar answered Oct 02 '22 17:10

NHol