Migrate an EBS backed AWS AMI to a different EC2 Region using a Perl Script
by Alexander Weiß
Recently I wrote a comprehensive overview of the different ways to migrate an EBS backed AMI to another EC2 Region. Today I’m getting more practical and guide you through the migration process via the Perl Module VM-EC2. The client operating system for this guide is Windows 8.
The first step will be the installation of the Perl distribution ActiveState. I chose ActiveState, because it is very easy to install and works well. However, it is a commercial distribution, but there is also a community edition available for free. If you want to use it, you should first check out if your planned usage complies with the licensing model.
As soon as you are sure, that you can use the community edition, follow these steps to get a working Perl environment:
- Download Perl from here: http://www.perl.org/get.html#win32
- Choose the ActiveState Perl Package
- Open a command shell window & install the VM::EC2 module by executing the following command: ppm install VM::EC2
Now you can try and run the script for migrating EBS backed AMIs. Open a shell and execute the following command:
You should get an error message saying that the access_key parameter is missing. If that’s the case you are ready for the next step.
Before you can start the migration you have to install a SSH-client, which behaves exactly like its Linux pedant:
- Download the cwRsync SSH-client from here: http://www.rsync.net/resources/howto/windows_rsync.html
- Install the software
- Add the path to the bin folder from the directory you installed cwrSync into to your PATH environment variable:
Setx PATH “%PATH%; C:\Program Files (x86)\cwrsync\bin”
Migrate AMI to another region
Before you can run the Perl script, be aware that it has some mandatory parameters:
- –from: EC2 Region in which the AMI is currently located (e.g. eu-west-1 )
- –to: EC2 Region to which the AMI should be migrated to (e.g. us-west-1 )
- –access_key: Your AWS Access Key, you can find it in the AWS account settings under the “Security Credentials” section
- –secret_key: Your AWS Secret Key, you can find it in the AWS account settings under the “Security Credentials” section
If you have to migrate many AMIs or if you need the parameters’ values often, it can be tedious to type them every time you want to migrate an AMI. Instead of typing them every time, you can omit the last three parameters and use environment variables instead. The big advantage is, that you have to set them once and you can use them forever. The names of the environment variables are:
- EC2_URL: replaces the –to parameter
- EC2_ACCESS_KEY: replaces the –access_key parameter
- EC2_SECRET_KEY: replaces the –secret_key parameter
To set the variables you can use the following command in the power shell:
Or if you prefer the CLI you can use this one:
Setx EC2_URL ‘us-west-1’
Now you have everything you need, except the AMI id. The Perl script obviously needs to know which AMI it has to move. You can find the AMI id in the “AMIs” section of the EC2 Management Console.
You can execute the Perl script now. If you have set the environment variables the full command looks like that:
migrate-ebs-image.pl –from eu-west-1 ami-aaa123bbb
Congratulations your AMI is now available in the target region.
EC2 Region migration costs
The migration process is not cost neutral, because the script needs some additional resources:
- The script launches two m1.small instances, one in the source and one in the target region.
- Additionally it needs to create EBS volumes on both instances. The size and amount of the volumes depends on the AMI. But you have to calculate that the script needs at least the double amount of the EBS storage your AMI has.
- The data between the two EC2 Regions is copied via the internet: so your bill will show the costs for incoming “data transfer IN” for the target region and the “data transfer OUT” costs for the source region. If your EBS volume contains a filesystem, rsync will be used for the data transfer. If it doesn’t contain a filesystem or if it is part of a RAID or LVM volume dd is used to copy the data on the block level. In general, dd is much slower and traffic intense than rsync
If the script has finished successfully you should check if the additional infrastructure, which was created by the Perl script, is terminated or deleted. I never had any problems, but it’s better to check, otherwise you could pay for something you don’t use.
The Perl script offers a few more options, you can find a full list here. In standard environments you don’t need them, though. All in all the Perl script is a very nice way to migrate your EBS backed AMI to another EC2 Region.