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