</>
Skip to content
AWS lessons (11/47)

AWS — EC2

What is EC2?

Elastic Compute Cloud provides resizable virtual servers in the cloud.

Instance types

TypeUse Case
t2.microFree tier, testing
t3.mediumGeneral purpose
m5.largeMemory intensive
c5.xlargeCompute optimized
r5.2xlargeMemory optimized

Launch instance

aws ec2 run-instances \
  --image-id ami-0c55b159cbfafe1f0 \
  --instance-type t2.micro \
  --key-name my-key \
  --security-group-ids sg-12345678 \
  --subnet-id subnet-12345678

Instance states

# Start
aws ec2 start-instances --instance-ids i-1234567890abcdef0

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

# Reboot
aws ec2 reboot-instances --instance-ids i-1234567890abcdef0

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

Connect to instance

# SSH
ssh -i key.pem ec2-user@public-dns

# SSM (no SSH needed)
aws ssm start-session --target i-1234567890abcdef0

Security groups

# Create security group
aws ec2 create-security-group \
  --group-name my-sg \
  --description "My security group"

# Add inbound rule
aws ec2 authorize-security-group-ingress \
  --group-id sg-12345678 \
  --protocol tcp \
  --port 22 \
  --cidr 0.0.0.0/0

Elastic IP

# Allocate
aws ec2 allocate-address

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

Best practices

  1. Use key pairs for SSH
  2. Restrict security groups
  3. Use IAM roles
  4. Monitor with CloudWatch

Mini Practice

  1. Launch an instance
  2. Connect via SSH
  3. Create a security group
  4. Assign an Elastic IP

Up Next

Continue with S3 - Object Storage.

Related Topics

Frequently Asked Questions about EC2

What is EC2 in AWS?

EC2 is a fundamental concept in AWS. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn EC2?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn EC2.

Why is EC2 important in AWS?

EC2 is essential for AWS development. Understanding this concept will help you write better code and solve real-world problems more effectively.