VPC 指南

VPC 与子网创建

# 创建 VPC aws ec2 create-vpc --cidr-block 10.0.0.0/16 # 创建公有子网 aws ec2 create-subnet \ --vpc-id vpc-12345678 \ --cidr-block 10.0.1.0/24 \ --availability-zone us-east-1a # 创建私有子网 aws ec2 create-subnet \ --vpc-id vpc-12345678 \ --cidr-block 10.0.10.0/24 \ --availability-zone us-east-1a # 为公有子网启用自动分配公网 IP aws ec2 modify-subnet-attribute \ --subnet-id subnet-public1a \ --map-public-ip-on-launch

互联网网关与路由表

# 创建并挂载 Internet Gateway aws ec2 create-internet-gateway aws ec2 attach-internet-gateway \ --internet-gateway-id igw-12345678 \ --vpc-id vpc-12345678 # 为公有子网创建路由表 aws ec2 create-route-table --vpc-id vpc-12345678 aws ec2 create-route \ --route-table-id rtb-public \ --destination-cidr-block 0.0.0.0/0 \ --gateway-id igw-12345678 # 关联子网与路由表 aws ec2 associate-route-table \ --route-table-id rtb-public \ --subnet-id subnet-public1a

NAT 网关

# 分配弹性 IP aws ec2 allocate-address --domain vpc # 在公有子网创建 NAT 网关 aws ec2 create-nat-gateway \ --subnet-id subnet-public1a \ --allocation-id eipalloc-12345678 # 私有子网路由指向 NAT 网关 aws ec2 create-route \ --route-table-id rtb-private \ --destination-cidr-block 0.0.0.0/0 \ --nat-gateway-id nat-12345678

安全组 vs NACL

特性安全组网络 ACL
作用级别实例级别子网级别
状态有状态(自动允许返回流量)无状态(需双向配置)
规则类型仅允许规则允许和拒绝规则
规则处理所有规则均评估按编号顺序评估

VPC 对等连接

# 请求对等连接 aws ec2 create-vpc-peering-connection \ --vpc-id vpc-111 --peer-vpc-id vpc-222 # 接受对等请求 aws ec2 accept-vpc-peering-connection \ --vpc-peering-connection-id pcx-12345678 # 在两端添加路由 aws ec2 create-route \ --route-table-id rtb-vpc1 \ --destination-cidr-block 10.1.0.0/16 \ --vpc-peering-connection-id pcx-12345678 # 注意:VPC 对等连接不可传递,枢纽架构请用 Transit Gateway

VPC 端点

# 网关端点(S3、DynamoDB,免费) aws ec2 create-vpc-endpoint \ --vpc-id vpc-12345678 \ --service-name com.amazonaws.us-east-1.s3 \ --route-table-ids rtb-private # 接口端点(大多数服务,收费) aws ec2 create-vpc-endpoint \ --vpc-id vpc-12345678 \ --vpc-endpoint-type Interface \ --service-name com.amazonaws.us-east-1.secretsmanager \ --subnet-ids subnet-private1a