There are two ways to migrate an EC2 instance: 1. Using the AWS Systems Manager automation document and 2. Manually copy the EC2 instance and launch a new one. However, we recommend the second option if you want to move your EC2 instance to another VPC (Virtual Private Cloud).
Can You Move an EC2 Instance to Another VPC?
AWS does not provide a direct option to move an existing EC2 instance to another subnet, VPC, or availability zone. Instead, they let you migrate it by creating a new AMI from the source instance and then use it to launch a new one in your desired VPC.
So, it is more of a workaround where you create a new Amazon Machine Image for manual migration. The process also requires you to reassign any Elastic IPs to the new instance from the previous one.
How Do You Move an EC2 Instance to Another VPC?
Moving an EC2 instance to another VPC is a two-step process.
Step 1: Create a New AMI
1. Open your Amazon EC2 console. From the navigation bar on the left, click on Instances.
2. Once the list of EC2 instances appears, choose the instance to move. Then, select Actions > Instance State > Stop.
3. Next, go to Actions, click on Image, and select the Create Image option.
Fill in the Image Name and Image Description text boxes. And if you don’t want to compromise on the file system integrity of the AMI, skip the No reboot option.
Now, click on Create Image.
4. Below the Create Image request received message, you can see an option called View Pending Image with an ID. Click on it and wait for its Status to turn into available. You can do the same from the Images section of the navigation pane.
Step 2: Launch a New Instance in Your Desired VPC
1. Right-click on the new AMI and select Launch.
2. Choose an Instance Type. Make sure you choose the same type as the EC2 instance to move.
3. Click on Configure Instance Details and choose your desired Virtual Private Cloud (VPC) along with the subnet to launch the new instance.
4. Go to the Add Storage section and choose your preferences.
5. In the Add Tags section, enter the Key and Value for your EC2 instance.
6. Choose the same security group corresponding to your instance inside the Configure Security Group section. For migration between VPCs, you must create a new security group on the VPC where you want to launch the instance.
7. From the Review section, click on Launch and select your key pair. Finally, check the agreement box and click Launch Instances.
Remember that you can use each Elastic IP address in only one Region. So, if you use an Elastic IP on the instance to move, you will have to reassociate the IP with the new one.
Can an EC2 instance be in multiple VPCs?
Cross-VPC connection for EC2 instances is possible via a VPC peering network. However, an EC2 instance is bound to the VPC of one primary network and must be in the same VPC.
When you move an EC2 instance to another VPC, stopping the instance is not mandatory. You can choose to skip it if you wish. However, we recommend doing so, as it helps prevent data inconsistency between the new and old EBS volumes.