Is it possible to change subnet in EC2 without terminating the instance?

0

Is it possible to change subnet in EC2 without terminating the instance?

asked a year ago830 views
6 Answers
3

No. The primary elastic network interface (ENI) can't be detached from an instance or have its subnet or VPC changed. You can create a new ENI connected to a new subnet and attach it as a secondary interface, though, but the primary one would remain in the old subnet.

EXPERT
answered a year ago
profile picture
EXPERT
reviewed a year ago
profile picture
EXPERT
reviewed a year ago
1

Hello,

You can't directly change the subnet for the instance. but you can do it with another option.

  • Stop the existing Instance.
  • Detach the EBS volume.
  • Create a new instance in required subnet and stop it.
  • Attach the EBS Volume to the new instance and start the new instance.
  • Finally terminate the old instance.
  • So, You can achieve that subnet change and your data is also recovered from EBS.

Thank you

profile picture
EXPERT
answered a year ago
profile picture
EXPERT
reviewed a year ago
profile picture
EXPERT
reviewed a year ago
1

Hello

Changing the subnet of an existing EC2 instance without terminating it is not directly possible in AWS. However, you can achieve the desired outcome by following these steps to effectively move your instance to a new subnet:

Stop the Instance: You must stop the instance before making any changes to its network settings.

**Detach the Root EBS Volume: **Note the device name (e.g., /dev/sda1) of the root volume.

**Launch a New Instance in the Desired Subnet: **Create a new instance in the target subnet with the same AMI, instance type, and any other necessary settings.

Stop the New Instance: Once the new instance is running, stop it to allow volume modifications.

Detach the New Instance's Root Volume: Detach the root EBS volume from this new instance.

Attach the Original Root Volume: Attach the original root EBS volume from your old instance to the new instance, using the same device name.

Start the New Instance: Start the new instance. It should now boot up with the original instance's data and configurations, effectively placing it in the new subnet.

Additional Considerations: *** Elastic IPs:** If your instance uses an Elastic IP, you can reassign it to the new instance.

*** Security Groups:** Ensure the new instance has the appropriate security group settings.

  • Data Consistency: Verify that all data and configurations are intact after moving the root volume.
profile picture
EXPERT
answered a year ago
EXPERT
reviewed a year ago
1

Hi,

A solution to your case may be the multi-VPC ENI attachment for EC2 instance announced at the end of 2023.

See https://aws.amazon.com/about-aws/whats-new/2023/10/multi-vpc-eni-attachments/

You can attach your instance to a new VPC or subnet without destroying existing ENI first.

Best,

Didier

profile pictureAWS
EXPERT
answered a year ago
profile picture
EXPERT
reviewed a year ago
profile picture
EXPERT
reviewed a year ago
profile picture
EXPERT
reviewed a year ago
0

While you cannot move EC2 to another subnet, you can create a AMI from source instance to manually migrate.

Please refer to How do I move my EC2 instance to another subnet, Availability Zone, or VPC?

AWS
EXPERT
answered a year ago
0

There is was update on 25 October 2024 https://aws.amazon.com/about-aws/whats-new/2024/04/removing-adding-auto-assigned-public-ipv4-address/ which made it possible to change IP address dynamically without restarting EC2 instance:

Amazon VPC announces a network interface setting to dynamically remove and add an auto assigned public IPv4 address on EC2 instances. With this capability, customers that no longer require an auto assigned public IPv4 address on their EC2 instance can remove the public IPv4 address, and if needed attach back a new public IPv4 address, by modifying the public IP setting on the network interface. Before today, once a public IPv4 address was auto assigned to EC2 instance it was not possible to remove it. It remained on the network interface for the lifetime of the EC2 instance.

The public IP setting on the network interface makes it easier for customers to be public IPv4 efficient and reduce public IPv4 cost. Customers that no longer need the auto assigned public IPv4 or are migrating to using private IPv4 address for SSH using EC2 instance connect endpoint can simply remove the auto assigned public IPv4 address, instead of recreating their applications on a new EC2 instance with no auto assigned public IPv4 address.

This feature is available in all AWS commercial and the AWS GovCloud (US) Regions at no additional cost. For more information, please visit the VPC user guide.

profile picture
EXPERT
answered 5 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions