AWS EC2 Complete Guide for Developers

Sanjeev SharmaSanjeev Sharma
2 min read

Advertisement

AWS EC2 Complete Guide for Developers

Amazon EC2 provides scalable compute resources. Learn to launch, manage, and scale instances.

Introduction

EC2 offers virtual machines with various instance types, networking options, and storage configurations.

Launching Instances

# Launch instance
aws ec2 run-instances \
  --image-id ami-0c55b159cbfafe1f0 \
  --instance-type t3.micro \
  --key-name my-key \
  --security-groups default

# Connect
ssh -i my-key.pem ec2-user@54.123.45.67

# List instances
aws ec2 describe-instances

# Stop instance
aws ec2 stop-instances --instance-ids i-1234567890abcdef0

# Terminate instance
aws ec2 terminate-instances --instance-ids i-1234567890abcdef0

Security Groups

# Create security group
aws ec2 create-security-group \
  --group-name web-sg \
  --description "Web server security group"

# Allow SSH
aws ec2 authorize-security-group-ingress \
  --group-id sg-123456 \
  --protocol tcp \
  --port 22 \
  --cidr 0.0.0.0/0

# Allow HTTP/HTTPS
aws ec2 authorize-security-group-ingress \
  --group-id sg-123456 \
  --protocol tcp \
  --port 80 \
  --cidr 0.0.0.0/0

Elastic IPs and Networking

# Allocate elastic IP
aws ec2 allocate-address --domain vpc

# Associate with instance
aws ec2 associate-address \
  --instance-id i-1234567890abcdef0 \
  --allocation-id eipalloc-123456

# VPC and subnets management
aws ec2 create-vpc --cidr-block 10.0.0.0/16
aws ec2 create-subnet --vpc-id vpc-123 --cidr-block 10.0.1.0/24

Auto Scaling

# Create launch template
aws ec2 create-launch-template \
  --launch-template-name my-template \
  --version-description "v1" \
  --launch-template-data file://launch-template.json

# Create auto scaling group
aws autoscaling create-auto-scaling-group \
  --auto-scaling-group-name my-asg \
  --launch-template LaunchTemplateName=my-template
  --min-size 1 \
  --max-size 10 \
  --desired-capacity 3

FAQ

Q: What's an AMI? A: Amazon Machine Image—template for EC2 instance with pre-configured OS and software.

Q: What instance type should I use? A: Start with t3.micro for testing, t3.small/medium for production web apps.

Advertisement

Sanjeev Sharma

Written by

Sanjeev Sharma

Full Stack Engineer · E-mopro