Launch an AWS EC2 Instance
Source: Dev.to
Introduction
This guide walks you through launching an AWS EC2 instance, installing Docker, and running NGINX inside a Docker container. By the end you will have a publicly accessible web server showing the default NGINX welcome page.
Prerequisites
- AWS account
- Basic terminal knowledge
- SSH client installed on your local machine
- Internet connection
Launch an EC2 Instance
- Open the AWS Console → EC2 → Launch Instance.
- Choose an AMI: Amazon Linux 2023.
- Select instance type:
t2.micro(Free tier). - Create a key pair:
- Name:
my-ec2-key - Type: RSA
- Format:
.pem
- Name:
- Network settings:
- Auto‑assign Public IP: Enabled
- Create a Security Group with the following inbound rules:
| Type | Port | Source |
|---|---|---|
| SSH | 22 | My IP |
| HTTP | 80 | 0.0.0.0/0 |
- Click Launch Instance and wait until the instance state becomes Running.
Connect via SSH
chmod 400 my-ec2-key.pem
ssh -i ~/.ssh/my-ec2-key.pem ec2-user@
Replace the empty space after ec2-user@ with the public IP address of your instance.
Install Docker
# Update packages
sudo yum update -y
# Install Docker
sudo yum install docker -y
# Start and enable Docker service
sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl status docker
# Add ec2-user to the docker group
sudo usermod -aG docker ec2-user
Log out and log back in for the group change to take effect:
ssh -i ~/.ssh/my-ec2-key.pem ec2-user@
Verify Docker installation:
docker --version
Run NGINX in Docker
docker run -d --name nginx-test -p 80:80 nginx
- Runs NGINX in detached mode
- Exposes port 80 to the public
Check that the container is running:
docker ps
You should see a line similar to:
0.0.0.0:80->80/tcp
Verify Public Access
Open a web browser and navigate to:
http://<public-ip>
You should see the default NGINX welcome page:
Welcome to nginx!
This confirms that your EC2 instance is publicly accessible and serving content via Docker.