Amazon EC2 Basics: Launch Your First Virtual Server, Instance Types, Key Pairs, Security Groups and Connecting
What Is EC2?
Amazon Elastic Compute Cloud (EC2) gives you virtual servers (called instances) in the cloud. You choose the operating system, size and software, and pay per second while the instance runs.
- Think of an instance as a computer you rent, ready in about a minute
- Used for web servers, applications, databases, batch jobs and more
- You are responsible for the guest operating system (updates, software)
Parts of an Instance Launch
| Choice | Meaning | Beginner pick |
| AMI (Amazon Machine Image) | Template with the operating system and software | Amazon Linux 2023 (or Ubuntu, Windows) |
| Instance type | CPU and memory size | t3.micro or t2.micro (Free Tier eligible where offered) |
| Key pair | Public/private key used to log in over SSH | Create one and download the .pem safely |
| Network settings | VPC, subnet (AZ), public IP, security group | Default VPC, public subnet, allow SSH from your IP only |
| Storage | EBS volume for the disk | 8 GiB gp3 root volume |
| IAM instance profile | Role for the server | Optional, for S3 access without keys |
| User data | A script that runs on first boot | Install a web server automatically |
Instance Type Families
| Family | Good for | Example names |
| General purpose | Balanced CPU and memory, web servers | t3, t4g, m6i, m7g |
| Compute optimized | CPU-heavy work, batch, game servers | c6i, c7g |
| Memory optimized | Big databases, in-memory caches | r6i, x2 |
| Storage optimized | High disk speed, data warehouses | i4i, d3 |
| Accelerated computing | Machine learning, graphics (GPU) | p4, g5, inf2 |
Name reading: m6i.large = family m, generation 6, processor option i (Intel), size large. Letter g means AWS Graviton (Arm), often cheaper for the same work.
Launch With the Console
- Open EC2 → Launch instance and give it a name tag
- Choose Amazon Linux 2023 AMI
- Choose t3.micro
- Create a key pair (RSA,
.pem) and download it
- In Network settings, allow SSH (22) from My IP and HTTP (80) if you will run a website
- Keep the default 8 GiB storage
- Open Advanced details → User data and paste the script below
- Click Launch instance, then wait for 2/2 status checks passed
bash
#!/bin/bash
# User data: runs once as root on first boot
dnf update -y
dnf install -y nginx
systemctl enable --now nginx
echo "<h1>Hello from my first EC2 server</h1>" > /usr/share/nginx/html/index.html
Launch With the CLI
bash
# Find the latest Amazon Linux 2023 AMI id from the public SSM parameter
AMI=$(aws ssm get-parameters --names /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64 --query 'Parameters[0].Value' --output text)
aws ec2 create-key-pair --key-name my-key --query KeyMaterial --output text > my-key.pem && chmod 400 my-key.pem
aws ec2 create-security-group --group-name web-sg --description "web and ssh"
aws ec2 authorize-security-group-ingress --group-name web-sg --protocol tcp --port 80 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress --group-name web-sg --protocol tcp --port 22 --cidr 203.0.113.9/32
aws ec2 run-instances --image-id $AMI --instance-type t3.micro --key-name my-key --security-groups web-sg \
--user-data file://userdata.sh --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=first-server}]'
aws ec2 describe-instances --filters Name=tag:Name,Values=first-server --query 'Reservations[].Instances[].[InstanceId,State.Name,PublicIpAddress]' --output table
Connect to the Instance
| Method | How | Notes |
| SSH | ssh -i my-key.pem ec2-user@<public-ip> | Needs port 22 open and the key. ec2-user for Amazon Linux, ubuntu for Ubuntu |
| EC2 Instance Connect | Browser-based connect button in the console | Easy for quick access |
| Session Manager | Console or aws ssm start-session --target <id> | No open port 22 and no key. Needs the SSM role. Best practice |
| RDP | Remote Desktop to port 3389 | For Windows servers |
Instance Lifecycle
| Action | What happens | Charged? |
| Stop | Shuts down. Disk (EBS) is kept | No compute charge, EBS storage still charged. Public IP changes |
| Start | Boots again | Compute billing resumes |
| Reboot | Restart, same machine | Same |
| Hibernate | Saves memory to disk, resumes later | Needs encrypted root volume |
| Terminate | Deletes the instance (root disk deleted by default) | Charges stop. Cannot be undone |
Avoid surprise bills A stopped instance still pays for its EBS disk, and an unattached Elastic IP or public IPv4 address may cost money. When finished practicing, terminate instances and delete extra volumes and IPs.
Key Extras
- Elastic IP: a fixed public IPv4 address that you can move between instances
- Instance metadata: the server can read info about itself at
169.254.169.254 (use IMDSv2 tokens)
- Tags: labels like
Name=web, env=dev for organization and billing
- Status checks: system (AWS side) and instance (your OS) checks every minute
- Termination protection: stops accidental terminate
Practice
- Launch the nginx server from the console and open its public IP in the browser
- Connect with SSH and run
uptime and df -h
- Stop and start it and notice the public IP change
- Attach an IAM role and use Session Manager instead of SSH
- Terminate everything and confirm no instances remain
Conclusion
EC2 gives virtual servers. Choose an AMI, instance type, key pair, security group and storage, use user data for first-boot setup, connect with SSH or better Session Manager, understand stop vs terminate, and clean up after practice.
Quick Summary: what; launch choices; families; console; CLI; connecting; lifecycle; extras.