Amazon EC2 - Create a new EBS backed AMI with custom AMI ID - amazon-ec2

I have an EBS-backed AMI instance running in EC2. I have customized it and now want to create a new AMI from it. I will doing this at regular intervals, thereby replacing the previous AMIs created.
I followed the instructions at
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/creating-an-ami-ebs.html
and everything worked perfectly.
One problem is that whenever, I create a new AMI, it gets a new AMI ID. So, when I make the AMI public whenever, I update the AMI, it's AMI ID changes. Is it possible to create a new AMI with a custom ID we can specify?
One alternative would be modify an existing AMI, so that the AMI ID remains the same. Please confirm if this is possible

Each time you create a new AMI, you will get a new id. There is unfortunately no way around this.
An alternative would be to update some of the AMI at launch, provided that it can be done relatively quickly. That way you wont need to create a new AMI every time something changes.

It is not possible to modify the contents of an AMI once you have created it such that the next time the AMI is used those changes will be present. This includes the root device and any data in EBS snapshots associated with the AMI (since it's not possible to alter the contents of an EBS snapshot, either).

Related

Will An EC2 Snapshot backup An Instance That Does Not Have A Volume?

I create an instance but do not add additional storage / a volume, will a snapshot backup the instance and the changes I have made to the config or does it only backup data from the volumes?
I am making some training material and would like to do the following:
Create an instance
Create users
Make a few more config/program installation changes
At this point, I would have a "clean copy".
I then would like to take a Snapshot of the state of the instance.
fool around, possibly break stuff
Then restore the instance to the "clean copy" after the instance has been created but before I started messing around.
Is this possible with snapshots?
Let's clarify some terminology:
A Snapshot can be made of an Amazon EBS volume. This makes a backup of the specific disk volume. The snapshot can be used later to create a new EBS Volume.
Or, you can create an Amazon Machine Image (AMI) of an Amazon EC2 instance. This creates an EBS Snapshot of all associated volumes and also stores some metadata about the AMI.
You can then Launch a new Amazon EC2 instance from the AMI. This will include a copy of the disks.
If you wish to restore the instance to the "clean copy", then it is best to launch a new instance from the AMI rather than trying to 'reset' the instance to an earlier state. The instance will have a different Instance ID and a different IP address, but will otherwise be the same as the instance from which the AMI was created.

How to save a modified AMI?

I'm using a community AMI, it's great but some of the stuff in it is outdated. Every time I spin up a new machine based on it I have to update all the libraries. I want to instead update them once and save the modified image. It's and EBS backed AMI. I tried creating a snapshot off of the volume of the running instance and then creating an AMI from the snapshot. The resulting AMI does indeed have all the modifications I made but the operating system is different! The original AMI has ubuntu while the thing that comes out is "other linux" - and some things are not working (CUDA). Both "RAM disk ID" and "kernel ID" in the original AMI details are blank so I leave them as "default" when creating the new AMI.
The preferred way to save a modified EC2 instance is to burn an AMI directly from the running instance, rather than taking a snapshot of its root volume.
If for any reason all you had was a snapshot of the root volume of a previously running instance, to create a bootable AMI you have to follow the following process: launch one of the stock EC2 AMIs, one that has the same OS as your EBS snapshot. Create an EBS volume from that snapshot. Stop the newly launched instance. Detach the root volume, and attach the new volume you created form the EBS snapshot as the root volume and start the instance. See Launching a Linux Instance from a Backup. NOTE: Although you can create a Windows AMI from a snapshot, you cannott successfully launch an instance from the AMI.
The easiest way to save an AMI with new modificationa, is to create the AMI image directly from the running instance, and not simply take a snapshot of the running volume.
From the AWS Management Console, click on the instance, then right-click Image -> Create Image.
From that dialog, set the Name, Description etc. Make sure to leave No Reboot unchecked. From the Instance Volumes section adjust the volume settings.
Note that your instance will reboot during the image creation process. Make sure you are prepared for the temporary loss of service of the instance during this time.

After creating an AMI, how do I launch it using the same configuration?

I set up my instance to my liking, created an image (AMI) from it, and stopped the instance. I now want to change the AMI for that instance to the new AMI, and re-launch the instance. How do I do this?
To clarify, clicking "Launch" from the list of AMIs in the management console is not what I need. This creates a new instance, making me go through the wizard again when I just want the same configuration, and it gives the new instance a different IP address, and so on.
I think you have a misunderstanding of how AMIs work. An AMI is just a template for launching an instance with a particular configuration.
You created your original instance from (presumably) a bare bones distribution AMI (say ami-11111), modified it, and created your own AMI (say ami-22222).
You can now launch instances using ami-22222 that will be configured the way you want, but it no longer has any connection to ami-11111. It doesn't make any sense to "change the AMI [...] to the new AMI".
If you stop an instance it will always get a new IP address when it is restarted. You can get a persistent IP address by:
Allocating an Elastic IP.
Launching the instance you want (in this case ami-22222)
Assigning the Elastic IP to that instance.
The instance will then be accessible via the Elastic IP address.
If you modify the instance in the future and create a new image of it (say, ami-33333), you can:
Launch an instance based on ami-33333.
Assign your Elastic IP to ami-33333 (thereby disconnecting ami-22222 from that IP).
Terminate ami-22222 once the new instance is active.
Note that it can take a minute or so for the transition to the new AMI during which the server will be unresponsive.
You can't change an existing instance to a new ami, instead you launch a new instance from that ami. When you launch an instance, it creates a copy of the ami in whatever storage type you are using (instance or ebs).

Are files saved on EBS volume? Are they not supposed to be saved? I'm confused

So I create an instance using one of the Public AMI EBS Ubuntu flavors. I create an EBS volume and attach it to the instance. I format the volume and add an entry to /etc/fstab to mount it on /vol. I add mysql to the AMI and move the data files to the EBS volume I formatted and mounted at /vol. I then create an AMI from the running instance. Then I terminate the running instance.
I start a new instance using the freshly created AMI (with mysql on it). The /vol is mounted has the mysql data files - good, I expect that. Here's where I am confused. When I create any directory or files on the EBS volume /vol they aren't there any more after I terminate the instance and create a new one. The mysql stuff is there but no new stuff I created. Aren't those files and directory supposed to be there? Or am I misunderstanding how this works?
When you create an AMI, "Amazon EC2 powers down the instance, takes images of any volumes that were attached, creates and registers the AMI, and then reboots the instance." -Amazon. When the AMI is used to launch an instance, the images (snapshots) of the attached drives are used to create new volumes. It is these new volumes that are attached to the new instance, not your original EBS. (This generates lots of orphan volumes and snapshots with ongoing use.)
There is no automatic attaching of the EBS volume you created. What is automatically attached is the volume it creates at the time of launching the instance from your AMI! It creates this volume from the snapshot it made of your EBS at the time of the AMI creation!
The way to avoid clone volumes from being created and attached to new instances is simple: detach your volumes before making AMIs. You need to attach your EBS volumes manually with the EC2 Web Control Panel, or programmatically with .net or Java programming, scripting or command line tools.
EBS volumes are not tied to an AMI, only to the literal instance you attach them to. When you created your AMI and a new instance from that, the EBS is not cloned, nor does it follow you to the new instance.
You could move the EBS drive to the new instance manually. Alternately you could snapshot the EBS volume & clone a new drive from that.

Which is better EBS or private AMIs?

I am new to Amazon web services. I need to create a server image with a few software packages pre installed on the EC2 instances.
One option I am considering is to create an EBS volume with these packages and then use them to launch EC2 instances.
The other option I am thinking of is to create a private AMI and then use them to launch the EC2 instances.
I am not sure which option is better.
One other slightly related question I have is can I create a private EBS volume and share it with some other account.
You should create your own AMI, as it's probably more efficient. In order to create an instance, you must specify an AMI. Specifying an EBS snapshot as the root device in addition to specifying the AMI (if this even works, I haven't tested it) would just result in the EC2 cloud launching your instance with the AMI, followed by overwriting it with the EBS snapshot for the root volume.
It's easy enough to keep the AMIs laying around as prototypes.

Resources