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

1

30

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"

Onepiece answered 29/9, 2016 at 11:22 Comment(2)
Is there also need to run 'growpart' and 'resize2fs' in user data during launching?Bridgid
Device naming aws docCuria
O
25

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

Onepiece answered 29/9, 2016 at 11:22 Comment(4)
Forgot my earlier comment, I see you need to add a conditions section. Can you show your conditions section to see how you decide whether the value of "Amazon-AMI" is set to true?Frangible
Hi @Frangible I havent used condition section I'm just using if statement to decide the OS type. My template takes OS name from allowed values in paramenters section which further are used in condition as mentioned below. <code> "Fn::If": [ //If "--Amazon OS name here--", //then "/dev/xvda", //use xvda "/dev/sda1" // or use sda1 ] <code>Onepiece
Related question - stackoverflow.com/q/59586518/3317808Albina
Any YAML example, please.Fulllength

© 2022 - 2024 — McMap. All rights reserved.