Automatically resizing Windows EC2, using Knife command -ebs-size

We’ve discovered an issue when trying to use theĀ -ebs-size command within Chef. The instances are created, but due to a limitation with the AMIs they won’t use the full amount of disk space without a resize.

I’ve got a “Base” Windows recipe which I use across all Windows instances. I’ve applied the following Powershell code to resolve this problem for us :-

It checks to ensure that there is enough space to meet minimum requirements, then automatically extends the boot volume to the greatest value available.

powershell_script 'Extending primary volume' do
  code <<-EOH
	$drive = (Get-WmiObject Win32_OperatingSystem).SystemDrive.Replace(":", "")
	$minBytesToActivate = 1048576 #1MB minimum in Windows, you may wish to change this.

	$volume = Get-Volume -DriveLetter $drive
	$actualSize = $volume.Size
	$sizeMax = (Get-PartitionSupportedSize -DriveLetter $drive).SizeMax
	$diff = $sizeMax - $actualSize
	if ($diff -gt $minBytesToActivate)
		$diff, $minBytesToActivate
		Resize-Partition -DriveLetter $drive -Size $sizeMax

Now, if we choose to resize our EBS to 60GB, the drive is automatically resized for us.