Wednesday, August 1, 2018


1.       [IMPORTANT] First of all, take a snapshot of the EBS volume for backup[1]
2.       Connect to your Windows instance and shrink the root volume from 1000 GB to 100 GB
a.       Open Disk Management on the Windows instance
b.      Right-click on volume C: -> Choose "Shrink Volume"
c.       Enter the amount of space to shrink in MB(in this case, 921600 MB)
3.       Stop the Windows instance
4.       Detach the root EBS volume from the Windows instance
5.       Launch an Amazon Linux instance in the same availability zone(AZ) with the Windows instance
a.       *I chose an EBS-optimised m4.large instance type to improve the disk speed[2]
6.       Attach the EBS volume to Amazon Linux instance as device /dev/sdf
7.       Create a new 100 GB EBS volume in the same AZ and attach it to the Amazon Linux instance as device /dev/sdg
8.       Connect to the Linux instance with ssh and copy all data from the original volume(/dev/sdf) to the new one(/dev/sdg)
a.       Type  "sudo -s" to switch to the root account
b.      Check that the volumes are attached correctly, type:
                                                               i.      fdisk -s /dev/sdf (the size of the 1000 GB volume should appear in KB, 1048576000 KB)
                                                             ii.      fdisk -s /dev/sdg (the size of the 100 GB volume should appear in KB, 104857600 KB)
c.       Type "yum install pv -y" to install pv. This tool will show you the progress of data transfer through a pipeline.
d.      Type "dd if=/dev/sdf |pv| dd of=/dev/sdg bs=1M"
                                                               i.      "dd" copy the contents of the source to the destination. The "bs=1M" tells "dd" to use a block size of one megabyte.
                                                             ii.      Please expect several hours to finish this step. In my environment, this step took five and half hours.
                                                            iii.      The time required depends on the instance type, network throughput, etc.
9.       Stop the Amazon Linux instance and detach both volumes(/dev/sdf and /dev/sdg)[4]
10.   Attach the 100 GB volume to the original Windows instance as device /dev/sda1 \
11.   Start the Windows instance
a.       If everything goes as expected, the Windows instance should pass the instance status checks.
b.      Logon to the Windows instance and verify from the Disk Management that the root EBS volume is 100 GB.
12.   Terminate Linux instance, delete the EBS volume of 1000 GB


Links:-

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-creating-snapshot.html
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSOptimized.html
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-attaching-volume.html
http://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ebs-detaching-volume.html

Shrink Database Log files (.ldf)

Open SQL Management Studio

Right Click on the database and go to Properties → Options

Select Recovery Model as "Simple". Click OK.
To Shrink Log file, Right click on the database go to Tasks → Shrink → Files
From the Shrink File window select Filegroup as "Log". Make sure to select "Release unused space" option. Click Ok.

Expand the root volume using the Amazon EC2 console

1.    Open the Amazon EC2 console and choose Instances from the navigation pane.

2.    Choose your instance, and choose the volume next to Block devices.

3.    Choose the EBS ID.
4.    Choose the volume, choose Actions, and select Modify Volume.

5.    Enter your desired volume in the Size field, choose Modify, and choose Yes.

6.    Refresh the console page. The expanded volume is immediately available for the OS. You can check this by choosing the Description tab, and choosing the information icon next to the State. This will show the Original Size and the Target Size.
Note: Windows root volumes are the master boot record (MBR) by default, and they can be extended up to 2TB. See Amazon EBS Volume Types for other considerations.

7.    Open the RDP session for the instance, launch the Disk Management view by using the diskmgmt.msc command, choose Action, and choose Refresh.

8.    Open the context (right-click) menu, and choose Extend Volume.

9.    Accept the default options by choosing Next and then Finish.

10.  Check the disk from This PC