Skip to content

Launch Your First EC2 Instance

Beginner

Spin up a virtual server on AWS, connect to it via SSH, and serve a simple web page using Amazon EC2.

Overview

Amazon EC2 (Elastic Compute Cloud) lets you rent virtual servers in the cloud. In this project you will launch a Linux instance, connect to it over SSH, install a web server, and serve a simple HTML page to the internet.

What You Will Learn

Prerequisites

Architecture

Your Browser → Internet → Security Group (port 80) → EC2 Instance (Nginx)
Your Terminal → Internet → Security Group (port 22) → EC2 Instance (SSH)

Steps

1. Create a Key Pair

Before launching an instance you need a key pair to authenticate over SSH.

  1. In the AWS Console, navigate to EC2Key Pairs (under Network & Security).
  2. Click Create key pair.
  3. Name it my-ec2-key, choose RSA and .pem format.
  4. Click Create key pair — the .pem file downloads automatically. Keep it safe.

On macOS/Linux, restrict the file permissions so SSH accepts it:

chmod 400 ~/Downloads/my-ec2-key.pem

2. Launch an EC2 Instance

  1. Navigate to EC2InstancesLaunch instances.
  2. Fill in the details:
    • Name: my-first-server
    • AMI: Amazon Linux 2023 (free tier eligible)
    • Instance type: t2.micro or t3.micro (free tier eligible)
    • Key pair: select my-ec2-key
  3. Under Network settings, click Edit and add two inbound rules to the security group:
    TypeProtocolPortSource
    SSHTCP22My IP
    HTTPTCP800.0.0.0/0
  4. Leave storage at the default 8 GiB.
  5. Click Launch instance.

3. Connect via SSH

Wait about 30 seconds for the instance to reach the Running state, then:

  1. Select your instance and click Connect → copy the SSH command, or build it manually:
ssh -i ~/Downloads/my-ec2-key.pem ec2-user@<YOUR_PUBLIC_IP>

Replace <YOUR_PUBLIC_IP> with the Public IPv4 address shown in the instance details.

You should land at the Amazon Linux shell prompt.

4. Install Nginx and Serve a Web Page

Run the following commands inside the SSH session:

# Update packages
sudo dnf update -y

# Install Nginx
sudo dnf install nginx -y

# Start Nginx and enable it on boot
sudo systemctl start nginx
sudo systemctl enable nginx

# Replace the default welcome page
echo "<h1>Hello from my EC2 instance!</h1>" | sudo tee /usr/share/nginx/html/index.html

5. View Your Web Page

Open a browser and navigate to:

http://<YOUR_PUBLIC_IP>

You should see Hello from my EC2 instance!

Clean Up

EC2 instances accrue charges while running. When you are done:

  1. Select the instance → Instance stateTerminate instance.
  2. Optionally delete the key pair and security group.

Stopping an instance pauses the hourly compute charge but the root EBS volume still incurs storage costs. Terminating removes everything.

Going Further