What Are Amazon Machine Images (AMI)?

About AMIs and how to create your own

Cloud Digests
5 min readNov 9, 2020

Amazon Machine Images (AMIs) help speed up the process of setting up EC2 instances by providing a packaged environment containing software configuration and other information required to start an EC2 instance.

Treat AMIs like a template, which contains the operating system, application server and any additional dependencies or application that is a part of it.

When setting up an EC2 instance, you have to choose an AMI. When you launch your first EC2 instance, you will probably use default AWS AMIs or community AMIs, but you can save you own as well.

An AMI contains one or more EBS snapshots, which is a template for the instance’s root device volume, launch permissions that control which AWS accounts can you the AMI to set up instances, as well as block devices that specify the root device volume to attach to the EC2 once it is launched,

You can launch multiple instances from a single AMI when you need multiple instances with the same configuration, this saves you time and helps you enable automation for deployment and auto-scaling.

Types of AMIs

There are 3 types of AMIs, public, paid and private.

Public AMIs are machine images that are safe, secure, customized and available for consumption by any AWS customer.

Paid images are purchased from a third-party developer on AWS Marketplace.

Private images are AMIs you create yourself and only you can access.

Creating Your Own AMI

I will show you how to create your own AMI in 2 different ways, using the AWS Management Console and AWS CLI. In this example, I will be creating an AMI to launch a very simple apache web server.

Prerequisites

In order to follow this guide and tutorial, you will need to know how to launch an EC2 instance. You can learn how here.

If you intend to follow the AWS CLI way, you would also need to install and configure AWS CLI. You can learn that here.

Launching the EC2 Instance

In order to create an AMI, we need to first launch an EC2 instance and configure it the way we want to, which is to be a very simple apache web server. For this, I suggest using the AWS Management Console since it is only one instance.

Launch your instance as per normal however you want, but make sure to input the following user data script before launch:

#!/bin/bash
yum update -y
yum install -y httpd.x86_64
systemctl start httpd.service
systemctl enable httpd.service
echo “Hello World from $(hostname -f)” > /var/www/html/index.html

Note: This script is for Amazon Linux AMIs, Centos or RHEL. If you use any Debian based OSs, you have to alter the script accordingly.

Make sure you tag the instance with Key:Value pair of “Purpose:AMI” for easier identification should you already have EC2 instances running.

For security groups, you need to open port 80 and 22 for HTTP and SSH access:

Once the EC2 instance is launched, go to the public IP address to check if the web server is set up successfully.

You are now ready to start creating an AMI from this EC2 instance to launch a web server.

Creating an AMI from the AWS Management Console

Go to the AWS Management Console → EC2 → Instances. Click on the instance you created in the previous part, and click on Actions → Image and Template → Create Image.

Enter the following details:

  • Instance Name: TestAMI
  • Image Description: For launching a simple apache web server

Then click Create Image.

And there you have it! You have successfully created an AMI from the AWS Management Console. The next part will cover using AWS CLI instead.

Creating an AMI using AWS CLI

Creating an AMI using the AWS CLI is incredibly easy as well. Open up your command prompt or terminal and type the following snippet of code to create an AMI from your existing EC2 instance:

# Change $id, $name and $desc to the instance-id you are using, the name of the AMI and the description of the AMI you want to createaws ec2 create-image --instance-id $id --name $name --description $desc

If you want to do this often and prefer a script which you can run, I have created one which you can access here.

Conclusion

And with that, you have created your first AMI! You can use these methods to create any AMIs you need. To use your AMI, go to launch an instance and select “My AMIs” from the AWS Management Console, or use the AMI-ID in the AWS CLI!

Hope this tutorial has helped you! If you have any other tutorials or guides you want to see, just leave a comment!

--

--

Cloud Digests
Cloud Digests

Written by Cloud Digests

Making Cloud Computing easy to learn and adopt for everybody, tech trained or otherwise. Simple and quick to understand content

No responses yet