Migrating an Amazon server to a new region

Once my free trial ran out, I decided to move to the Oregon region which was somewhat cheaper than California.  In doing so, I was rather surprised at how tricky it is to move an AWS server from one region to the next.  After a number of false starts, here’s how to do it:

    1. First, using the Console to make an AMI of the source server doesn’t help you since the resulting AMI doesn’t appear in S3, and thus there’s no way to migrate it to a bucket in the new region.  So don’t bother with that.  Instead, make an AMI snapshot using the Console, then launch a new instance from it.  This will prevent the next steps from affecting performance on the source server.
    2. Using the Console, create S3 buckets on the source and target regions to store the AMI files.
    3. Find the kernel ID and ramdisk ID on the new region by searching for a similar AMI and choosing them from options in the “Launch Instance” wizard poplists.  To be honest, I don’t understand the differences between them so just grabbed the first options shown and they worked.  See here and here on this step.
    4. On the new instance, edit the bash script below to set the appropriate variables at the top, then run it to create an AMI, upload it to the S3 bucket in the current region, then migrate it to the bucket in the target region.

      migrate_ami.sh

      #!/bin/sh
       
      PRIVATEKEY_PATH=/path/to/pk-XXX.pem
      CERT_PATH=/path/to/cert-XXX.pem
      AWS_ACCOUNT_ID=XXXX-XXXX-XXXX
      DESTDIR=/path/to/dest/dir
      SOURCE_BUCKET=mysourcebucket
      DEST_BUCKET=mytargetbucket
       
      # e.g., us-west-2
      DEST_REGION=targetregion    
       
      ACCESS_KEY_ID=XXXXXXXXXXXXXXXXXXXX
      SECRET_ACCESS_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
       
      KERNEL_ID=aki-XXXXXXXX
      RAMDISK_ID=ari-XXXXXXXX
       
      date
       
      # Delete any existing image
      rm -vf $DESTDIR/image
       
      # Bundle the source AMI - see http://tiagomatos.org/blog/?p=611
      ec2-bundle-vol -r x86_64 -d $DESTDIR -k $PRIVATEKEY_PATH --cert $CERT_PATH -u $AWS_ACCOUNT_ID -a 
       
      # Upload the bundle
      ec2-upload-bundle -b $SOURCE_BUCKET -m $DESTDIR/image.manifest.xml -a $ACCESS_KEY_ID -s $SECRET_ACCESS_KEY
       
      # Delete any pre-existing bundle in the destination bucket
      ec2-delete-bundle -b $DEST_BUCKET -a $ACCESS_KEY_ID -s $SECRET_ACCESS_KEY --yes -p image
       
      # Migrate the bundle to the new region
      ec2-migrate-bundle --cert $CERT_PATH --privatekey $PRIVATEKEY_PATH --access-key $ACCESS_KEY_ID \
        --secret-key $SECRET_ACCESS_KEY --bucket $SOURCE_BUCKET --destination-bucket $DEST_BUCKET \
        --manifest image.manifest.xml  --kernel $KERNEL_ID --ramdisk $RAMDISK_ID --region $DEST_REGION
    5. Register the new AMI on the target region using the Console, by entering the path to the manifest.xml (e.g., bucketname/image.manifest.xml)
    6. Launch an instance in the target region from the new AMI.

Leave a comment

Your email address will not be published. Required fields are marked *