Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change default root EBS size in cloudformation? [AWS]

Considering there is less amount of documentation and solutions online for cloudformation I decided to address a common problem regarding changing default size of EBS volumes launched via cloudformation template

By default the instances launched have 8GB size and if your wondering how can you change that to something as per your preference than you've landed to correct solution.

There are two ways to avoid the problem

Solution 1 : Create a New Volume with VolumeAttachment (Incorrect way)

"EBS" : {
   "Type" : "AWS::EC2::Volume",
   "Properties" : {
      "Size" : "100",
      "AvailabilityZone" : { "Fn::GetAtt" : [ "EC2Instance", "AvailabilityZone" ] }
   }
},

"MountPoint" : {
   "Type" : "AWS::EC2::VolumeAttachment",
   "Properties" : {
      "InstanceId" : { "Ref" : "EC2Instance" },
      "VolumeId"  : { "Ref" : "EBS" },
      "Device" : "/dev/sda1"
   }
}

Here I created a new volume and tired to attach it to instance which didn't work.(CF template failed to launch)

Solution 2. Block device mapping (The Correct way)

Use BlockDeviceMappings is the correct way to approach

 "BlockDeviceMappings": [
          {
            "DeviceName": "/dev/xvda",
            "Ebs": {
              "VolumeType": "io1",
              "Iops": "300",
              "DeleteOnTermination": "false",
              "VolumeSize": "30"
            }
          }
        ],

Dont keep device name as /dev/xvda1 otherwise it won't work. Instead, set the "DeviceName" property of the block device mapping to "/dev/xvda" if the selected OS is Amazon Linux, otherwise for Ubuntu or CentOS set it to "/dev/sda1"

like image 578
Bhargav Amin Avatar asked Sep 29 '16 11:09

Bhargav Amin


People also ask

How do I change my EBS size?

To modify an EBS volume using the consoleIn the navigation pane, choose Volumes. Select the volume to modify and choose Actions, Modify volume. The Modify volume screen displays the volume ID and the volume's current configuration, including type, size, IOPS, and throughput.

Can we increase root EBS volume size?

EBS volume can be expanded using Amazon EC2 console or AWS CLI. Note: Windows root volumes are the master boot record (MBR) by default and can be extended up to 2 TB.

Can we modify root volume in AWS?

You can't modify the volume type of Multi-Attach enabled io2 volumes. You can't modify the volume type, size, or Provisioned IOPS of Multi-Attach enabled io1 volumes. A root volume of type io1 , io2 , gp2 , gp3 , or standard can't be modified to an st1 or sc1 volume, even if it is detached from the instance.


1 Answers

So the final solution considering you've multiple OS and you want to increase the default size of EBS volume use Fn::If intrinsic function to set the "DeviceName" property of the block device mapping to "/dev/xvda" if the selected OS is Amazon Linux, otherwise it will set it to "/dev/sda1" for the other OS.

Snippet would look something like this :

 "BlockDeviceMappings": [           {             "DeviceName": {               "Fn::If": [                 "Amazon-AMI",    // condition satisfying that if amazon is OS then use /dev/xvda or else /dev/sda1                 "/dev/xvda",                 "/dev/sda1"               ]             },             "Ebs": {               "VolumeType": "io1",               "Iops": "300",               "DeleteOnTermination": "false",               "VolumeSize": "100"             }           }         ] 

This should get your cloudformation going without any errors. If you have any errors still please check you template and validate it properly

like image 157
Bhargav Amin Avatar answered Sep 21 '22 07:09

Bhargav Amin