Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create an AMI from my EBS backed EC2 instance?

I have an EBS backed EC2 running instance. The EBS store holds a database. I would like to create an AMI from this. Can someone please provide guidelines?

  1. Do I have to shut down MySQL?
  2. Will my AMI attempt to connect to the same EBS store?! (disaster)
  3. Will creating the AMI automatically snapshot my EBS volume and create a copy? (hoping it's this one)

Thanks for your patience through a long question. I recognize there's similar information out there, but nothing really addresses what may go wrong.

This SO question:

http://stackoverflow.com/questions/4475532/creating-an-ec2-ami-with-an-ebs-backed-instance-is-it-possible

is specific to ElasticFox and I'm not using any tools (prefer command line). I don't believe the question is answered either.

This blog:

http://instantbadger.blogspot.com/2009/09/how-to-create-and-save-ami-image-from.html

while instructive about creating an AMI, does not mention EBS at all and I'm a little worried about all that data.

Thanks in advance!

like image 825
cone Avatar asked Jun 12 '11 06:06

cone


3 Answers

Before going to your questions I would suggest that you back up your MySQL database to a file which is not stored as a snapshot or in an EBS before you start creating the image. (And of course test that you can restore it to somewhere else than your production system.)

I use:

mysqldump --add-drop-table -u root -p databasename > database.sql

to backup and:

mysql -u root -p databasename < database.sql

to restore.

  1. No, you do not have to shut down MySQL, but you have to prevent writes to the database while create the AMI.
  2. No, The creation of a AMI also creates a new snapshot of your EBS volume (with the content as it was at image creation time).
  3. yes, see 2

A bit more info: I prefer to shut down the database when possible when snapshoting or creating images. That said I am not by any mean an MySQL expert, but here is some guidance from http://aws.amazon.com/articles/1663?_encoding=UTF8&jiveRedirect=1

Start a MySQL session on the instance, using the password you set above.

mysql -u root -p

In the mysql session, flush the tables to disk and acquire a lock. Flush the file system to disk and freeze it. Do not exit the MySQL session or you will lose the lock and snapshot potentially inconsistent database files!

FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;
SYSTEM sudo xfs_freeze -f /vol
like image 189
steenhulthin Avatar answered Oct 21 '22 03:10

steenhulthin


Amazon console gives you an option to do this STUFF.

Just, login on to your aws console and navigate the instance-ID you wish to create an AMI.

  1. Right-Click on the instance and click on "Stop".
  2. Right-Click on the instance and click on "Create AMI".
  3. Type-in name for your AMI & hit OK.

That's it. This would have created an AMI. Please see: http://docs.amazonwebservices.com/AWSEC2/2011-05-15/UserGuide/index.html?Tutorial_CreateImage.html for a detailed information.

like image 26
Rakesh Sankar Avatar answered Oct 21 '22 04:10

Rakesh Sankar


Make sure you always backup you MySQL by dumping as suggested by steenhulthin.

This link will help on creating your new EBS backed AMI: http://www.capsunlock.net/2009/12/create-ebs-boot-ami.html

I also suggest to place your MySQL data to a separate EBS volume or better RAIDed volumes.

like image 1
Rodney Quillo Avatar answered Oct 21 '22 03:10

Rodney Quillo