EC2 参考
实例类型系列
| 系列 | 用途 | 示例 |
|---|---|---|
| t3/t4g | 可突发通用型 | t3.micro, t4g.small |
| m6i/m7g | 均衡通用型 | m6i.large, m7g.xlarge |
| c6i/c7g | 计算优化型 | c6i.2xlarge |
| r6i/r7g | 内存优化型 | r6i.large |
| p3/g4 | GPU(ML/图形) | p3.2xlarge, g4dn.xlarge |
| i3/i4i | 存储优化(NVMe SSD) | i3.large |
安全组
# 创建安全组
aws ec2 create-security-group \
--group-name web-sg \
--description "Web server security group" \
--vpc-id vpc-12345678
# 添加入站规则
aws ec2 authorize-security-group-ingress \
--group-id sg-12345678 \
--protocol tcp --port 80 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress \
--group-id sg-12345678 \
--protocol tcp --port 22 --cidr 203.0.113.5/32
密钥对与用户数据
# 创建密钥对
aws ec2 create-key-pair \
--key-name my-key \
--query 'KeyMaterial' \
--output text > my-key.pem
chmod 400 my-key.pem
# 携带用户数据启动实例
aws ec2 run-instances \
--image-id ami-0abcdef1234567890 \
--instance-type t3.micro \
--key-name my-key \
--user-data file://init.sh
EBS 卷
# 创建并挂载 EBS 卷
aws ec2 create-volume \
--volume-type gp3 --size 100 \
--availability-zone us-east-1a --encrypted
aws ec2 attach-volume \
--volume-id vol-1234567890abcdef0 \
--instance-id i-1234567890abcdef0 \
--device /dev/xvdf
# 创建快照
aws ec2 create-snapshot \
--volume-id vol-1234567890abcdef0 \
--description "每日备份"
AMI
# 从运行中实例创建 AMI
aws ec2 create-image \
--instance-id i-1234567890abcdef0 \
--name "MyApp-v1.0-$(date +%Y%m%d)" \
--no-reboot
# 查找最新 Amazon Linux 2023 AMI
aws ec2 describe-images \
--owners amazon \
--filters "Name=name,Values=al2023-ami-*" \
--query 'sort_by(Images, &CreationDate)[-1].ImageId' \
--output text
Spot 实例
# 请求 Spot 实例
aws ec2 request-spot-instances \
--instance-count 1 \
--type one-time \
--launch-specification file://spot-spec.json
# 生产环境推荐使用带混合实例策略的 Auto Scaling 组
# OnDemandBaseCapacity: 1, SpotInstancePools: 4