AWS Builder Center

CREATION OF EC2 INSTANCE

This article explains how to create an Amazon EC2 instance and connect to it using a terminal. It provides a beginner-friendly, step-by-step guide covering instance creation, key pair configuration, security groups, launching the instance, and connecting securely using SSH. By completing this hands-on activity, readers will learn the basics of running and accessing a virtual server on AWS

Introduction

Amazon Elastic Compute Cloud (Amazon EC2) is an AWS service that provides resizable virtual servers in the cloud. EC2 allows users to run applications, host websites, perform development and testing, and manage computing resources without purchasing physical servers.
In this article, we will learn how to create an EC2 instance and connect to it using a terminal. This hands-on activity is useful for beginners who are getting started with AWS.

What is Amazon EC2?

Amazon EC2 provides virtual machines called instances. We can select the operating system, instance type, storage, networking, and security settings according to our requirements.
Some important EC2 components are:
  • AMI (Amazon Machine Image): Defines the operating system and initial software.
  • Instance Type: Determines the CPU, memory, and networking capacity.
  • Key Pair: Used to securely connect to the instance.
  • Security Group: Acts as a virtual firewall.
  • EBS Volume: Provides persistent storage for the instance.
  • Public IP Address: Allows the instance to communicate over the internet.

Prerequisites

Before creating an EC2 instance, you need:
  • An AWS account
  • Access to the AWS Management Console
  • Basic knowledge of Linux commands
  • An SSH client or terminal
  • A key pair for authentication

Step 1: Open the EC2 Console

  1. Sign in to the AWS Management Console.
  2. Search for EC2 in the search bar.
  3. Select EC2 from the services list.
  4. Make sure you are working in the desired AWS Region.

Step 2: Launch an EC2 Instance

From the EC2 dashboard:
  1. Click Launch instance.
  2. Enter a name for the instance.
For example:
1
My-First-EC2
Choose an appropriate name that helps you identify the instance.

Step 3: Choose an Amazon Machine Image

Under Application and OS Images, select an operating system.
For this tutorial, you can choose:
Amazon Linux
The AMI provides the operating system and basic software required to start the EC2 instance.

Step 4: Choose an Instance Type

Select an instance type based on your requirements.
For a basic learning project, you can choose a suitable Free Tier-eligible instance type when available for your account and region.
The instance type determines the computing resources such as:
  • vCPUs
  • Memory
  • Network performance
For learning purposes, a small instance is generally sufficient.

Step 5: Create a Key Pair

A key pair is used to securely connect to the EC2 instance.
  1. Under Key pair, select Create new key pair.
  2. Enter a key pair name.
Example:
1
my-ec2-key
  1. Select the appropriate private key format.
  2. Click Create key pair.
The private key file will be downloaded to your computer.
Important: Keep the private key secure. Do not share or upload it publicly.

Step 6: Configure the Security Group

A security group works as a virtual firewall for the EC2 instance.
For SSH access, add an inbound rule:
TypeProtocolPort
SSHTCP22
For a web server, you can also allow:
TypeProtocolPort
HTTPTCP80
HTTPSTCP443
For better security, SSH access should ideally be restricted to your own IP address instead of allowing access from anywhere.

Step 7: Configure Storage

AWS provides default storage for the instance.
For a basic EC2 learning exercise, the default EBS storage is usually sufficient.
You can increase the storage size depending on your application requirements.

Step 8: Launch the Instance

After configuring the instance:
  1. Review the configuration.
  2. Click Launch instance.
  3. Wait for the instance to start.
Go to the Instances page and check the instance state.
You should see:
1
Instance state: Running
Once the instance is running, you can connect to it.

Step 9: Get the Public IP Address

Select your EC2 instance.
In the instance details, find:
Public IPv4 address
For example:
1
3.110.xx.xx
You will use this address to connect to the instance.

Step 10: Connect to the EC2 Instance Using Terminal

Open your terminal on your computer.
Navigate to the directory where your private key is stored.
For example:
1
cd Downloads
If required, change the permissions of your private key:
1
chmod 400 my-ec2-key.pem
Now connect to the EC2 instance using SSH.
For an Amazon Linux instance:
1
ssh -i "my-ec2-key.pem" ec2-user@<Public-IP>
Replace <Public-IP> with the public IPv4 address of your EC2 instance.
For example:
1
ssh -i "my-ec2-key.pem" ec2-user@3.110.xx.xx

Step 11: Confirm the Connection

After executing the SSH command, you may receive a message asking whether you want to continue connecting.
Enter:
1
yes
If the connection is successful, you will see a terminal prompt similar to:
1
[ec2-user@ip-172-31-xx-xx ~]$
This means you have successfully connected to your EC2 instance.

Step 12: Run Basic Linux Commands

After connecting, you can run Linux commands.
Check the current directory:
1
pwd
List files:
1
ls
Check the operating system:
1
cat /etc/os-release
Check the current user:
1
whoami
These commands help you verify that you are working inside the EC2 instance.

Step 13: Install a Web Server

As an additional hands-on activity, you can install a web server.
For Amazon Linux, you can install Apache using:
1
sudo dnf install httpd -y
Start the Apache service:
1
sudo systemctl start httpd
Enable it to start automatically:
1
sudo systemctl enable httpd
Create a simple webpage:
1
echo "<h1>Hello from my AWS EC2 Instance!</h1>" | sudo tee /var/www/html/index.html
Make sure the security group allows HTTP traffic on port 80.
Then open the following in your web browser:
1
http://<Public-IP>
You should see:
Hello from my AWS EC2 Instance!

Step 14: Terminate the Instance

When you have finished the practice, remember to stop or terminate resources that you no longer need to avoid unnecessary charges.
To terminate the instance:
  1. Open the EC2 Console.
  2. Select your instance.
  3. Choose Instance state.
  4. Select Terminate instance.
  5. Confirm the termination.
Note: Termination permanently removes the EC2 instance. Make sure you have backed up anything important before terminating it.

Key Learnings

Through this hands-on activity, we learned how to:
  • Create an EC2 instance.
  • Select an AMI and instance type.
  • Create and use a key pair.
  • Configure an EC2 security group.
  • Launch and monitor an EC2 instance.
  • Find the public IP address.
  • Connect to a Linux EC2 instance using SSH.
  • Execute Linux commands on an EC2 server.
  • Install and run a basic web server.

Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article