AWS — EC2
What is EC2?
Elastic Compute Cloud provides resizable virtual servers in the cloud.
Instance types
| Type | Use Case |
|---|---|
| t2.micro | Free tier, testing |
| t3.medium | General purpose |
| m5.large | Memory intensive |
| c5.xlarge | Compute optimized |
| r5.2xlarge | Memory 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
- Use key pairs for SSH
- Restrict security groups
- Use IAM roles
- Monitor with CloudWatch
Mini Practice
- Launch an instance
- Connect via SSH
- Create a security group
- 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.