Skip to content
Back to Cloud Services

AWS EC2 Virtual Servers

Provision resizable compute instances with full OS control, networking, and storage options.

AWSIntermediate

Why

EC2 gives full OS access for apps that can't be serverless — custom runtimes, long-lived processes, GPU workloads. 750 hours/mo free tier (t2.micro) covers a small app 24/7.

When

Use EC2 for stateful services, custom software, GPU/ML training, and apps needing persistent disks. Avoid for event-driven or spiky workloads (use Lambda/Fargate).

How

1. Launch — aws ec2 run-instances --image-id ami-xxx --instance-type t3.micro --key-name mykey 2. Security group — allow ports 22 (SSH), 80/443 (HTTP), restrict source to your IP 3. Connect — ssh -i key.pem ec2-user@<public-ip> 4. User data — bootstrap script runs on first boot (install packages, start app) 5. EBS — attach volumes, snapshot for backup, gp3 for best price/perf 6. Elastic IP — allocate fixed public IP for stable DNS 7. Auto Scaling — launch template + ASG, scale on CPU/queue depth 8. Spot — 90% off for batch, use Spot Fleet with auto-replace

Example

# Launch EC2 with user-data bootstrap aws ec2 run-instances \ --image-id ami-0c7211f8e8e1d2c3 \ --instance-type t3.micro \ --key-name my-key \ --security-group-ids sg-abc123 \ --user-data file://bootstrap.sh \ --tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=web-server}]" # bootstrap.sh #!/bin/bash yum install -y nginx systemctl start nginx systemctl enable nginx # Auto Scaling Group aws autoscaling create-auto-scaling-group \ --auto-scaling-group-name web-asg \ --launch-template LaunchTemplateName=web-template \ --min-size 2 --max-size 10 \ --target-group-arns arn:aws:elasticloadbalancing:...

Common Problems

  • ⚠Default security group open to 0.0.0.0/0 — lock down to specific IPs/SGs immediately
  • ⚠EBS volumes orphaned after termination — enable DeleteOnTermination or snapshot manually
  • ⚠t2.micro CPU credits exhaust — burstable instances throttle after credits spent, use t3 unlimited

Best Practices

  • ✓Use IMDSv2 only — IMDSv1 vulnerable to SSRF, enforce via metadata options
  • ✓Use launch templates not run-instances — versioned, reusable, ASG-compatible
  • ✓Right-size with Compute Optimizer — recommends instance type based on 14d metrics

FAQ

What is AWS EC2 Virtual Servers and what problem does it solve?

Provision resizable compute instances with full OS control, networking, and storage options. It lets developers deploy, store, and scale without self-managing infrastructure — a core reference for cloud service selection.

How do I get started with AWS EC2 Virtual Servers in 5 minutes?

Two steps to run it: (1) Launch — aws ec2 run-instances --image-id ami-xxx --instance-type t3.micro --key-name mykey; (2) Security group — allow ports 22 (SSH), 80/443 (HTTP), restrict source to your IP. Validate on the free tier first, then wire into production.

What is the #1 pitfall with AWS EC2 Virtual Servers?

The most frequent issue is: Default security group open to 0.0.0.0/0 — lock down to specific IPs/SGs immediately. Run a pre-launch checklist item by item, and add policy/config scanning in CI.

What are the best practices for AWS EC2 Virtual Servers?

Best practices for AWS EC2 Virtual Servers include: Use IMDSv2 only — IMDSv1 vulnerable to SSRF, enforce via metadata options; Use launch templates not run-instances — versioned, reusable, ASG-compatible; Right-size with Compute Optimizer — recommends instance type based on 14d metrics. The first one — "Use IMDSv2 only — IMDSv1 vulnerable to SSRF, enforce via metadata options" — should be a team default baked into your IaC templates.

How do I control cost and avoid bill surprises with AWS EC2 Virtual Servers?

Three ways to prevent overspend: (1) tag every resource with env/team and enable billing alarms; (2) set budget thresholds to trigger Slack/email alerts; (3) review idle resources in Cost Explorer regularly and clean them up. Most AWS EC2 Virtual Servers options offer a free tier — validate within free quota before scaling.