AWS: Amazon Machine Image (AMI)

Amir Mustafa
AWS in Plain English
6 min readSep 1, 2021

--

→ At the time of installation of an EC2 image, we have chosen any OS of our choice for installation eg. Amazon Linux, Ubuntu, Mac, etc.

→ These are all part of public AMI.

→ We can also use private AMI (which has some customization).

→ At the time of EC2 installation. We used to copy-paste some codes which execute every time i.e. installations and last line which shows on URL running.

eg.

#!/bin/bash# Use this for your user data (script from top to bottom)# install httpd (Linux 2 version)yum update -yyum install -y httpdsystemctl start httpdsystemctl enable httpdecho "<h1>Hello World from $(hostname -f)</h1>" > /var/www/html/index.html.   // THIS DISPLAYED WHEN RUNNING EC2 URL

Now this will be split into two parts:

a.When installing EC2 instance:

All package update running once at the time of EC2 installation.

#!/bin/bash# Use this for your user data (script from top to bottom)# install httpd (Linux 2 version)yum update -yyum install -y httpdsystemctl start httpdsystemctl enable httpd

b. At the time of image creation:

Displaying our content other than the default Apache page of AWS.

#!/bin/bashecho "<h1>Hello World from $(hostname -f)</h1>" > /var/www/html/index.html

Hands-on AMI (Amazon Machine Image):

STEP A Create the EC2 instance2:

→ At the time of EC2 instance creation, we have written some code that displays when we hit the public URL

→ This command executes again and again which should run only once.

→ Let us install a new EC2 instance. This time we will not use the last line at the time of installation EC2 instance.

After creating an instance, below will be the output of the server:

→ Search EC2 instances and follow the below screenshots:

→ Click Launch Instances

→ Click on the Amazon Linux 2 AMI. Click Select button

→ Chose t2 micro type (as it is free-tier 😄) and click the Next: Configure Instance Details button.

→ We will copy only the selected copy now for boot code run (which is for updates only).

→ Scroll down to the next page, paste the code there. Here is what we are specifically telling EC2, hey install the updates only now.

The content to render after hitting the server URL we will give you from the image. Click Next: Add Storage button

→ Hit Next: Add Tags button with default storage.

→ Hit Next: Configure Security Groups button

→ Select the Existing security group radio button and hit the Review and Launch button.

→ Click the Launch button

→ A popup comes, chose existing pair and hit Launch instances

→ Let us go back to the EC2 instances, here we see our brand new EC2 instance running.

→ Copy-paste the public URL. You will see the default Apache Test page screen is showing now.

STEP B Create an Image:

→ The idea of creating an Amazon image is to add more customizations to the base EC2 instances created in STEP A. In place of the default screen, we will add our screen.

→ In the image, we will give the below code, so that instead of the default page Hello world prints

#!/bin/bashecho "<h1>Hello World from $(hostname -f)</h1>" > /var/www/html/index.html

→ Right-click on the newly created EC2 instances → Create Images

→ Write your image name → Click Create Image button

→ On the left-hand side Click AMI. It takes a minute or two to be in the running state.

→ Once the AMI is in an available state. We have to install a new EC2. This time we pick an image from AMI.

→ Click instance from left → Click Launch Instances

→ Click My AMI. Our newly created AMI is here. Click Select

→ From now we will do the same EC2 installation with one difference, we will add just below two lines. This will open from image now instead of the Default page

#!/bin/bashecho "<h1>Hello World from $(hostname -f)</h1>" > /var/www/html/index.html

→ Observe the code pastes on the screen. This will be visible in the EC2 server instead of the default image.

→ Follow the installation steps below in screens (similar we did above)

→ In the tag, we can write from AMI

→ Click Launch Instances. Now the image installation is also done. Go to the EC2 instance and open the public API in the browser tab.

→ This time customized output printed

Video:

Closing thoughts:

We have learnt ways to create a private Amazon Machine Image (AMI) and customize it according to requirements. Clients mainly share customized AMIs, which can be used as a base in the server.

In the next article, we will see EC2 Image builders.

Thank you for being till the end 🙌 . If you enjoyed this article or learned something new, support me by clicking the share button below to reach more people and/or give me a follow on Twitter to see some other tips, articles, and things I learn and share there.

More content at plainenglish.io

--

--

JavaScript Specialist | Consultant | YouTuber 🎬. | AWS ☁️ | Docker 🐳 | Digital Nomad | Human. Connect with me on https://www.linkedin.com/in/amirmustafa1/