Skip to main content

Command Palette

Search for a command to run...

Creating an AWS AMI from an EC2 Instance with an Attached EBS Volume and Nginx Installation

Updated
6 min read
Creating an AWS AMI from an EC2 Instance with an Attached EBS Volume and Nginx Installation

Introduction

Amazon Machine Images (AMIs) are one of the most powerful features in AWS. They allow you to create reusable templates of EC2 instances, including the operating system, installed software, configurations, and attached EBS volumes.

In this hands-on guide, we'll:

  • Launch an EC2 Instance

  • Create and Attach an EBS Volume

  • Format and Mount the Volume

  • Install and Configure Nginx

  • Create an AMI

  • Launch a New EC2 Instance from the AMI

  • Verify that Nginx is running on the new instance

By the end of this tutorial, you'll understand how AMIs simplify server deployment and scaling in AWS.


Architecture

Launch EC2 Instance
        ↓
Create EBS Volume
        ↓
Attach Volume to EC2
        ↓
Format and Mount Volume
        ↓
Install Nginx
        ↓
Create AMI
        ↓
Launch New Instance from AMI
        ↓
Verify Nginx Service

Prerequisites

Before starting, ensure you have:

  • AWS Account

  • Basic knowledge of EC2

  • Permission to create EC2 instances, EBS volumes, and AMIs


Step 1: Launch an EC2 Instance

Navigate to:

AWS Console → EC2 → Launch Instance

Configuration

Setting Value
Name AMI-Demo
AMI Ubuntu Server
Instance Type t3.micro
Key Pair Create or Select Existing
Security Group Allow SSH (22)

Click Launch Instance.

Wait until the instance state becomes:

Running

Step 2: Connect to the Instance

Connect using EC2 Instance Connect or SSH.

Example:

ssh -i key.pem ubuntu@<public-ip>

Verify:

hostname

Step 3: Create an EBS Volume

Navigate to:

EC2 → Volumes → Create Volume

Configuration

Setting Value
Size 4 GB
Type gp3
Availability Zone Same as EC2 Instance

Click Create Volume.


Step 4: Attach Volume to EC2

Select the Volume.

Choose:

Actions → Attach Volume

Select your instance.

Device Name:

/dev/sdf

Click Attach Volume.


Step 5: Verify Attached Volume

Connect to the instance and run:

lsblk

Example Output:

nvme0n1   8G
nvme1n1   4G

Where:

nvme0n1 = Root Volume
nvme1n1 = Newly Attached EBS Volume

Step 6: Create Mount Directory

sudo mkdir /mnt/disk-mount

Verify:

ls /mnt

Step 7: Format the Volume

Create an EXT4 filesystem:

sudo mkfs.ext4 /dev/nvme1n1

Output:

Creating filesystem...
Filesystem UUID...

Why do we format?

A newly attached EBS volume is raw storage. Formatting creates a filesystem so Linux can store files and directories.


Step 8: Mount the Volume

Mount the volume:

sudo mount /dev/nvme1n1 /mnt/disk-mount

Verify:

df -h

or

lsblk

Expected Output:

nvme1n1   4G   /mnt/disk-mount

Step 9: Install Nginx

Update package repository:

sudo apt update

Install Nginx:

sudo apt install nginx -y

Verify installation:

sudo systemctl status nginx

Expected Output:

Active: active (running)

Enable Nginx at boot:

sudo systemctl enable nginx

Step 10: Configure Security Group

Navigate to:

EC2 → Security Groups

Add an Inbound Rule:

Type Port Source
HTTP 80 0.0.0.0/0

Save the rule.


Step 11: Verify Nginx

Open:

http://<public-ip>

You should see:

Welcome to nginx!

Step 12: Create an AMI

Navigate to:

EC2 → Instances

Select your instance.

Choose:

Actions
    ↓
Image and Templates
    ↓
Create Image

Configuration

Setting Value
Image Name nginx-ami
Description Ubuntu with Nginx and EBS Volume

Click:

Create Image

AWS will create snapshots and generate an AMI.


Step 13: Wait for AMI Availability

Navigate to:

EC2 → AMIs

Initially:

Pending

Wait until:

Available

Step 14: Launch a New Instance from the AMI

Select the AMI.

Click:

Launch Instance from AMI

Configuration:

Setting Value
Name nginx-server-clone
Instance Type t3.micro
Key Pair Existing Key
Security Group SSH + HTTP

Launch the instance.


Step 15: Verify the New Instance

Connect to the new instance.

Check Nginx:

sudo systemctl status nginx

Expected Output:

Active: active (running)

This confirms that the AMI successfully captured the installed software.


Step 16: Access Nginx on the New Instance

Open:

http://<new-public-ip>

You should see:

Welcome to nginx!

or your custom web page.


How AMI Works Internally

When you create an AMI:

  1. AWS takes snapshots of attached EBS volumes.

  2. Stores instance configuration.

  3. Creates a reusable machine image.

When launching a new instance:

  1. AWS creates new EBS volumes from snapshots.

  2. Attaches them to the new instance.

  3. Boots the operating system.

  4. Preserves installed software and configurations.


Benefits of AMI

  • Rapid server deployment

  • Consistent environments

  • Easy backup and recovery

  • Faster scaling

  • Simplified DevOps workflows


Interview Questions

What is an AMI?

An Amazon Machine Image (AMI) is a template that contains an operating system, application software, configurations, and snapshots used to launch EC2 instances.

Does an AMI include EBS volumes?

Yes. AMIs contain snapshots of attached EBS volumes.

What is the difference between Snapshot and AMI?

Snapshot AMI
Backup of a volume Template for EC2
Contains one volume Can contain multiple snapshots
Used for recovery Used for deployment

Why do we use mkfs.ext4?

To create a filesystem on a newly attached volume so Linux can store files and directories.


Conclusion

In this tutorial, we successfully launched an EC2 instance, attached and mounted an EBS volume, installed Nginx, created an AMI, and launched a new EC2 instance from that AMI. This workflow demonstrates a common real-world AWS administration task and highlights the power of AMIs in creating reusable and scalable server templates.