I recently had to do some spelunking into a long-updated AMI image to move it over to automated builds. The AMI had been updated over the period of a few years with security updates, OS patches, configurations, and more, and very little documentation was generated on those changes, as organic growth tends to do. I wanted the opportunity to poke at it over time, but didn’t necessarily want to burn the compute time in EC2 to maintain the instance, so I wanted to get it booting locally.
Of course, Amazon does not let you download AMIs unless you had them built and uploaded in the first place.
In this example, I’m using Linux to create disk images, and I’m running the resulting image in Hyper-V on Windows 10. Some commands can be altered to work with other environments.
- Start a new instance with the AMI you’d like to work with. When creating the storage, uncheck delete on termination.
- Once created, terminate the instance, it isn’t needed anymore.
- Create a new, nano Linux instance with default settings.
- Within Volumes, attach the volume from your first instance to the new Linux instance.
- Create or identify an S3 bucket to store your image to, and make sure the instance profile of the Linux EC2 instance can access that S3 bucket.
- SSH into the Linux machine, and open a
tmuxinstance to shield you from disconnections.
- Identify the device node of the new image. You can
dmesg | tailor look in
/dev/nvme*for new matching, unmounted nodes.
- Now we image that device directly to S3. Replace
/dev/nvme1n1with the device you found, and
your_bucket_namewith the destination you chose in S3. This command pulls a raw image, compresses it, and transmits it directly to S3:
dd if=/dev/nvmen1 - | gzip | aws s3 cp - s3://your_bucket_name/ami-disk-image.img.gz
- With that image saved, you may now terminate the Linux EC2 instance you created, and delete the volume.
- On your local machine, pull the disk image:
aws s3 cp s3://your_bucket_name/ami-disk-image.img.gz .
- Use gunzip, 7zip, or WinRAR to gunzip the disk image
- Use qemu-img to convert the image to Hyper-V:
qemu-img.exe convert ami-disk-image.img -O vhdx -o subformat=dynamic C:\Users\Public\Documents\Hyper-V\ami-disk-image.vhdx
- Create a new Hyper-V instance in version 8, with this disk image mounted to IDE
- Remove the generated image from S3
Congratulations, you now have your environment in a local VM to play with.