EC2
EC2 (Elastic Compute Cloud) gives you virtual servers. You choose the size and operating system; AWS runs the hardware.
On this page · 5 min · Beginner
On this page
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 29, 2026
Related tutorials
Learning objectives#
- Launch an EC2 instance
- Choose an instance type and AMI
- Bootstrap software with user data
- Attach a role and secure access
Explanation#
An AMI (Amazon Machine Image) is the OS template. Instance types combine family and size: t3.micro (burstable), m7i.large (general purpose), c7g (compute, Graviton ARM).
Storage is usually an EBS volume — a network disk that persists when the instance stops.
Security groups act as a firewall. User data runs a script on first boot to install software.
Attach an IAM instance profile (role) so code on the instance can call AWS APIs without keys. Prefer Session Manager over opening SSH.
Code example: Launch a web server#
aws ec2 run-instances \
--image-id resolve:ssm:/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64 \
--instance-type t3.micro \
--security-group-ids sg-0web123 \
--subnet-id subnet-0pub123 \
--iam-instance-profile Name=web-ssm-role \
--user-data file://user-data.sh \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=web-1}]'
# user-data.sh
#!/bin/bash
dnf install -y nginx
systemctl enable --now nginx
aws ssm start-session --target i-0abc123def456 # shell access without SSH
{
"Instances": [{ "InstanceId": "i-0abc123def456", "InstanceType": "t3.micro", "State": { "Name": "pending" } }]
}This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- The AMI ID is resolved from a public SSM parameter so you always get the latest Amazon Linux 2023.
- The security group controls inbound ports.
- The instance profile grants permissions (here, Session Manager access).
- User data installs and starts Nginx on first boot.
- Session Manager opens a shell with no port 22 open.
Real-world use case#
Running a self-managed app server, a build agent or software that needs full OS control.
Common mistakes#
Restrict to your IP, or close port 22 and use Session Manager.
Instance store disks are wiped when the instance stops. Use EBS or S3 for anything important.
Best practices#
- Use instance roles, not keys
- Use Session Manager instead of SSH
- Stop or schedule dev instances
- Use Auto Scaling groups for production instead of single instances
Remember this#
AMI = OS template. Instance type = size.
Security group = firewall. User data = first-boot script.
Role on the instance, no keys on disk.
Practice exercise#
Your task: Stop and then start the instance i-0abc123def456.
Show solution
aws ec2 stop-instances --instance-ids i-0abc123def456
aws ec2 start-instances --instance-ids i-0abc123def456
Mini quiz#
What runs a script automatically when an instance first boots?
Want more? Practise all AWS MCQs · AWS interview questions
My notes#
Saved in this browser.