Search code examples
amazon-web-servicesamazon-ec2amazon-ami

Is it possible to replace the AMI on an EC2 instance with a different AMI?


I'm running an EC2 AMI instance with LAMP stack and Laravel. I need to update my LAMP stack version as well as the Laravel version.

I was thinking of running a new EC2 instance as my sandbox for the update process and install a brand new LAMP stack + Laravel and then make changes on my Laravel code base to make all my code work with the new updates.

Can I then export the AMI from this new instance and replace my already running EC2's AMI with the new one? This way I don't need to ask my client and their clients to change anything such as IP address.


Solution

  • The answer is that you cannot replace the AMI for an existing EC2 instance. However, you can replace the root volume with a new volume which is basically the same thing. That new root volume can come from another EC2 instance. I just repeated the steps to verify that this actually can be done.

    • Create a snapshot of the root volume of the second system.
    • Copy the snapshot to the same AZ as the first system.
    • Create a new volume from the snapshot.
    • Detach the old root volume. Label first in case something fails.
    • Attach the new root volume.
    • Boot the instance and verify everything.

    [Update]

    I just repeated these steps again, but this time I enabled encryption during the snapshot copy. This is now another method of encrypting the root volume of an EC2 instance. The other method is to copy an existing AMI while enabling encryption, but the second method is for creating new instances.