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"