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

15,060

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

Share:
15,060

Related videos on Youtube

Bhargav Amin
Author by

Bhargav Amin

AWS Certified Solutions Architect, Blogger and Technology Enthusiast Cloud DevOps and Operations engineer

Updated on June 07, 2022

Comments

  • Bhargav Amin
    Bhargav Amin almost 2 years

    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"

    • Queenie Wong
      Queenie Wong almost 6 years
      Is there also need to run 'growpart' and 'resize2fs' in user data during launching?
    • Efren
      Efren about 5 years
      Device naming aws doc
  • CarlR
    CarlR over 7 years
    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?
  • Bhargav Amin
    Bhargav Amin over 7 years
    Hi @CarlR 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>
  • overexchange
    overexchange over 4 years
  • Prem
    Prem over 2 years
    Any YAML example, please.