How to migrate EC2 instance between Availability Zones

Migrating an EC2 instance between Availability Zones (AZs) within the same region isn't a direct task like clicking a "migrate" button. Instead, you'll create an Amazon Machine Image (AMI) of the current instance and then launch a new instance from that AMI in the desired AZ.

The migration flow diagram:

In this guide, I'll provide an example of how to migrate an EC2 instance running an Apache server from one Availability Zone (AZ) to another within the us-east-1 region.

1. Set Up an Apache Server on EC2 in us-east-1a AZ: For this, I assume you already have an EC2 instance running in the us-east-1a AZ with Apache installed. If not:

  • Launch an EC2 instance (e.g., Amazon Linux 2) in us-east-1a.

  • SSH into the instance.

  • Install Apache:

      sudo yum update -y
      sudo yum install -y httpd
      sudo systemctl start httpd
      sudo systemctl enable httpd
    
  • Add a sample HTML page:

      echo "<h1>Hello from us-east-1a</h1>" | sudo tee /var/www/html/index.html
    
    💡
    To run an EC2 instance use the following guide: How to launch a single EC2 instance via AWS CLI

    Before running the EC2 instance, along with SSH port add rule into the security group for HTTP traffic:

       aws ec2 authorize-security-group-ingress --group-name AWSSSH --protocol tcp --port 80 --cidr <your_public_ip_address>/32
    

    When running the instance use the --placement AvailabilityZone=us-east-1a option as in the following command:

      aws ec2 run-instances --image-id ami-0f34c5ae932e6f0e4 --instance-type t2.micro --key-name DemoKeyPair --security-groups AWSSSH --count 1 --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=MyFirstInstance}]' --placement AvailabilityZone=us-east-1a
    

2. Create an AMI:

  • From the EC2 dashboard, select the instance.

  • Under “Actions”, select “Create Image”. Provide a name and description.

  • The image creation process will start.

3. Launch an Instance in us-east-1b using the AMI:

  • Once the AMI is available, click “Launch Instance”.

  • Select “My AMIs”. You should see the AMI you created.

  • During the launch configuration, ensure you select the us-east-1b AZ.

  • Proceed with the instance launch. You can choose the same security group to allow HTTP traffic.

4. Verify Apache is Running:

  • After the new instance in us-east-1b is running, navigate to its public IP in a web browser. You should see "Hello from us-east-1a".

    This confirms the server was successfully migrated. Even though the message says "us-east-1a", this is because our sample page was created there.

5. Clean Up (Optional):

  • If you're done testing, terminate both instances to avoid additional charges.

  • Also, consider deregistering the AMI and deleting associated snapshots if they're no longer needed.

Notes:

  • If your instance has an Elastic IP or you're using Route 53 for DNS, remember to reconfigure these to point to the new instance in us-east-1b.

  • The data inside the instance (like the sample HTML page) was preserved during the migration thanks to the AMI. If you're running databases or other services with persistent data, ensure you have proper backups and data migration strategies in place.