Amazon Web Services (AWS) - Update EC2 instance subnet

by
Jeremy Canfield |
Updated: April 15 2024
| Amazon Web Services (AWS) articles
Once you have created and launched an EC2 instance in a particular subnet, you cannot change the EC2 to be in a different subnet. However, there is a workaround.
- Create an Amazon Machine Image (AMI) of the EC2 instance
- Create a new EC2 instance in the desired subnet using the Amazon Machine Image (AMI) of the EC2 instance
The aws ec2 create-image command can be used to create an Amazon Machine Image (AMI) of the EC2 instance. Be aware that by default, this will stop the EC2 instance if the EC2 instance is up and running, then create the AMI, and then start the EC2 instance
~]$ aws ec2 create-image --instance-id i-0a3ea97aa2383de58 --name "my-ami"
{
"ImageId": "ami-0671aa7b44458b59f"
}
And then use the aws ec2 create-image command to launch a new EC2 instance using the Amazon Machine Image (AMI) of the EC2 instance in the desired subnet.
aws ec2 run-instances
--image-id ami-0671aa7b44458b59f \
--count 1 \
--key-name default \
--security-group-ids sg-0778124087b3d14d4 \
--subnet-id subnet-03f11123480f6abcd \
--instance-type t2.micro \
--associate-public-ip-address \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=my-new-instance}]'
Did you find this article helpful?
If so, consider buying me a coffee over at