Simple way to create Amazon EC2 AMI from running instance

Amazon web services

When you have a running EC2 instance, you can build your production environment there, install whatever the packages you need, tweak system-wide configurations to meet your needs… Then you might wanna build your own AMI image to simplify future deployment.

Here we’ll show a quick example on how to build an EBS-backed AMI. This AMI is generated from one of your running instance.

First: Create an EBS volume
This volume will be use as the root partition for new instances, and you can specify whatever size you need. It’s suggested to be at least 3GB. When this volume is created, attach it to the running instance as device /dev/sdc. We assume that the new volume id is vol-8ee49ce6, and the running instance’s kernel id is aki-6603f70f. We’ll use these two ID in the final step.
When the new volume attached, you can use these command to build the file-system on it, then mount it as /mnt/ebs

# mkfs.ext3 /dev/sdc
# mkdir /mnt/ebs
# mount /dev/sdc /mnt/ebs

Step 2: Sync the running system to the new volume
If there’s any service running on the instance, it’s suggested to shut them down. It’ll save you hassles later. Then, use rsync to copy everything over to the EBS volume:

# rsync -ax --exclude=/mnt/* --delete --progress / /mnt/ebs

Before copying the system, make sure your instance is tweaked well, like you might need to update /etc/fstab to remove some unused devices.
You may need to create some device files on the new EBS volume. If console, zero or null don’t exist in /mnt/ebs/dev, create them using some or all of these:

# cp -prf /dev/console /mnt/ebs/dev
# cp -prf /dev/zero /mnt/ebs/dev
# cp -prf /dev/null /mnt/ebs/dev

You can also use MAKEDEV to create device, here we use cp for simple.

Step 3: Make your AMI
Now you can make a snapshot of the EBS volume via Amazon EC2 command line tools. This is the basis of the AMI you’ll be creating. Whatever you copied to the EBS volume in step 2 will be there — user accounts, database data, etc.

# ec2-create-snapshot vol-8ee49ce6 -d "Active AMI for Production Server"

That’ll give you a snapshot-id back. You then need to wait for the snapshot to finish. Keep running ec2-describe-snapshots until it says it’s “completed“.
Finally, you can register the snapshot as an AMI:

# ec2-register --snapshot snap-a8f327c4 --description "Active AMI for Production Server" --name "web-server-image" ––kernel aki-6603f70f --architecture x86_64 --root-device-name /dev/sda1 -b /dev/sdb=ephemeral0

The above command will return an AMI-ID, you can use this AMI-ID to launch an instance and you’ll find it pretty much exactly where you left your original instance. Note that the first time load may take some time like 10 mins and become faster then.

Share this post

2 thoughts on “Simple way to create Amazon EC2 AMI from running instance

  1. i install LAMP in amazon but if i connect mysql database give this error

    Server error
    The website encountered an error while retrieving It may be down for maintenance or configured incorrectly.
    Here are some suggestions:
    Reload this webpage later.
    HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.

    what is this

  2. @Ashok, it seems to be a configuration issue. Can you go through your config steps again and make sure you didn’t miss something? Goodluck!

Post Comment