Skip to content

AMI

Created: 2016-02-17 11:25:12 -0800 Modified: 2018-09-26 10:52:26 -0700

  • To view any AMIs that you’ve created, go to EC2 Dashboard —> Images —> AMIs
  • Creating AMIs is as easy as creating a snapshot in a hypervisor:
    • Spawn a base image (into an EC2 VM)
    • Make changes over SSH
    • Stop it.
    • Create an AMI from it (see below)
  • First, make sure to reduce start time by deleting temporary files, defragmenting your hard drive, and zeroing out free space. It’s also a good idea to “apt-get update” and “apt-get upgrade”.
  • Also, it’s a good idea to clear your Bash command history so that you don’t have setup commands when you press “up” after logging in (reference)
    • Note: this command will disconnect your SSH session thanks to the “exit”
      • cat /dev/null > ~/.bash_history && history -c && exit
  • To actually make the image, go to your EC2 Dashboard —> Instances —> Actions —> Image —> Create Image
    • You probably do not want “No reboot” checked
    • You probably want “Delete on termination” checked, otherwise you could have stray EBS volumes.
    • Note: this will take ~5 minutes since it has to copy the entire disk, and the only indication that it’s doing something is in the EC2 Dashboard → Images → AMIs UI. Note that even then, you may not see an entry get added to that dialog for a couple of minutes even if everything is fine.
  • The image that you create will be backed by EBS. You wouldn’t want to put it in something like S3 unless you needed to make it public.
  • Note: most images you create are going to have a minimum size of 8 GB. To lower this amount, you can rsync to another mounted EBS volume by following these instructions. WARNING: these instructions will waste 5 hours of your time unless you know what you’re doing with Linux.

It’s basically just:

  • Make a machine with the AMI you want
  • Run the creation steps again
  • Delete the old AMI
  • Update references to the new AMI
  • BEFORE PROCEEDING, REMEMBER THE AMI ID FOR THE STEP BELOW! In the EC2 Dashboard, Images —> AMIs —> Actions —> Deregister. This only removes the AMI, not the snapshot backing it.
  • Delete the EBS snapshot that backed the AMI. You can find this because the AMI ID (which you got in the step above) should be in the name or description (it looks like this: “Created by CreateImage(i-9e34f546) for ami-e7b25287 from vol-ab5b306b”).