How to Create and Use Custom Amazon Machine Images (AMIs)

AWS Logo

AMIs are similar to operating system ISOs but are more than a simple OS. They are base templates that you can build on top of; they contain the OS, preinstalled software, and configuration files that the server needs to function.

AMIs Are More Than an Operating System

There are many “fresh” AMIs that are simply just an operating system; for example, Amazon Linux 2 or Ubuntu Server LTS 18.04 (though Amazon Linux does come with the AWS CLI preinstalled). If you just want a blank server install with out-of-the-box Linux, you can choose these.

However, there are many AMIs that contain extra software suited for different tasks. AWS has a whole community browser for these, where a quick scroll will find you many AMIs like this:

AMI.

This AMI is based on Ubuntu and comes with drivers and software intended to be used with Nvidia GPUs for machine learning. There are premade images for all sorts of things on the community browser.

All of the AMIs on the community browser were created by someone, and you can do the same. Say you’re hosting a web app, and you want to spin up a second server, either for redundancy or just to meet increasing demands. You could choose a blank AMI, spend an afternoon installing everything you need to run your app, copy your code over, and troubleshoot the eventual errors.

Or, you could make an AMI based off of your current server, and spin up a perfect clone in a few minutes. This saves you the headache of reinstalling and reconfiguring nginx every time you make a new instance.

Create Your Own Images to Package Your Platform

One small note: If you’re planning on using multiple servers for a single service, you’ll likely want to have an Elastic Load Balancer sitting in front of them. This will allow you to point your DNS to the load balancer and have traffic balanced between all of your instances. Without it, there’s not much point to having multiple servers.

Creating an AMI is simple—you can easily copy your current server, or you can create a new server to set up the AMI with exactly what you want. Whichever route you choose, when you’re done configuring the server, head over to the EC2 Management Console and find your instance in the Instances list.

Right-click your instance and select Image > Create Image. You’ll be brought to this dialog, where there isn’t much to do except give your instance a name and optional description.

Create image.

By default, AWS will create a snapshot of your root EBS volume to base your AMI off of. This snapshot includes your OS, all of your installed software, and your files.

You’ll likely want to have some way of keeping your code up to date past the AMI creation, whether that’s regularly creating new AMIs for major changes, employing version management with git, running containerized applications with Docker, or simply using EFS to provide all of your instances with a shared file system.

Your AMI will take a few minutes to create, depending on the size of your root EBS volume. Once it’s done, it will be viewable in the “AMIs” tab of the EC2 Management Console.

When you create a new server, you can choose your AMI under the “My AMIs” tab:

Choose your AMI under "My AMIs" tab.

You can also right-click the AMI in the AMIs tab to launch an instance using that AMI.

Using Custom AMIs as a Base for Autoscaling Groups

Autoscaling groups are a fantastic feature of AWS—they can allow your network to scale up to meet demand, and scale down when demand is low. If an instance in the autoscaling group becomes unhealthy (i.e., overloaded with traffic), a new instance can be created automatically to help meet the traffic needs. Often, you’re able to use EC2 Spot instances, which can be much cheaper than renting On-Demand.

When you create an autoscaling group, you’ll make a launch template that defines the properties of the automatically launched servers. Part of this launch template is the AMI; You could use a blank “base” AMI like Amazon Linux 2, or you can use your own custom AMI with your software preinstalled.

Custom AMI.

Once you’ve set your AMI, you can create the rest of the autoscaling group as usual.

This helps a lot with load times of new servers; if you’re constantly spinning up new servers to efficiently meet fluctuations in demand, your biggest bottleneck will be how long it takes to get your server booted, running, and handling client requests.

Rather than waiting minutes for software to install, having everything preinstalled on an AMI reduces boot times to little more than the 45 seconds or so it takes to launch a regular EC2 instance.

Source

Author: admin

Leave a Reply

Your email address will not be published. Required fields are marked *