AWS Examples: Change EC2 Instance Type without Losing Data

AWS Examples: Change EC2 Instance Type without Losing Data

Last updated:
Table of Contents

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:

image-instance-should-be-stopped Make sure the source instance
is stopped

Open detail view for instance and click on the volume

Open the Storage tab and click on the Volume ID

instance-detail-view Open Storage tab and then click the volume ID

Create Snapshot for volume

In the detail view for the Volume, create a snapshot for it

create-snapshot-for-volume Create a snapshot for the volume

copy-snapshot-id-to-clipboard Copy the snapshot ID to the clipboard
(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:

add-snapshot-as-storage Add the Snapshot ID in the "Snapshot" field, after
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 )

    alt text Note that there are entries under
    xvdf and xvdf1, 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

    mounted-to-old-snapshot 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.

Dialogue & Discussion