AWS Examples: Change EC2 Instance Type without Losing Data
Last updated:- Stop source instance
- Open detail view for instance and click on the volume
- Create Snapshot for volume
- Launch new instance in the same AZ, with the old snapshot
- Mount the old snapshot
- Copy data to the new volume
- Other
Alert Be careful when copying data from the old machine to the new one. It's better to keep the old data as a backup instead of overwriting it directly.
There may be other and better ways to do this. This is the one I usually use.
Using Ubuntu 20.04 as the example OS here
Stop source instance
Go to the Instances view on AWS EC2 and stop the current instance:
is stopped
Open detail view for instance and click on the volume
Open the Storage tab and click on the Volume ID
Create Snapshot for volume
In the detail view for the Volume, create a snapshot for it
(You'll need it later)
Launch new instance in the same AZ, with the old snapshot
The new instance must be in the same Availability Zone (AZ) as the source instance.
In the step Step 4: Add Storage, add a New Volume and paste the Snapshot ID you copied into the clipboard in the previous step:
clicking on "Add New Volume"
Note that the Device name must be
/dev/sdf
or higher only or it will fail!
Mount the old snapshot
For this, you will need to SSH into the new instance and run the following commands:
Run
sudo lsblk
to make sure the snapshot was correctly attached (you can see )Note that there are entries under
xvdf
andxvdf1
, without a mountpoint.
(Linux sometimes changes device names
from/dev/sdX
to/dev/xvdX
)Mount it to
/old-snapshot
:$ sudo mkdir /old-snapshot $ sudo mount /dev/xvdf1 /old-snapshot
Check that it's been mounted correctly
Check that the old volume has been
mounted correctly to/old-snapshot
Copy data to the new volume
Be careful when copying data. You probably shouldn't just blindly copy core OS stuff under
/etc
,/var
, etc.
For example, copy the old /home
folder to the new volume:
$ sudo cp -r /old-snapshot/home /old-home
$ sudo mv /home /current-home
$ sudo mv /old-home /home
Other
Error message: snapshotId can only be modified on EBS devices
If you get this error message, choose a device name starting with /dev/sdf
instead of (sdf
, sdg
, sdh
, etc) instead.