AWS 網路架構:從 VPC 到 VPN 一次搞定

你已經會開帳號、管權限了,但機器開出來連不上網、Security Group 設了半天還是打不通——80% 的問題出在網路層。

先講結論

部署一個 Web App 到 AWS,網路層要走完這條路:VPC → Subnet(跨 AZ)→ IGW + NAT GW → Route Table → Security Group。看起來步驟很多,但邏輯很直覺——就是「建房子 → 隔間 → 裝門 → 裝鎖」。另外,如果你需要從外面安全連回 AWS 內部,自架 VPN 是最實在的解法。


VPC:你在 AWS 上的私有網路

VPC 就是你在 AWS 上圈出來的一塊網路空間。把它想像成一棟辦公大樓——大樓是你的,裡面怎麼隔間你決定。

# 建立 VPC
aws ec2 create-vpc --cidr-block 10.0.0.0/16 \
  --tag-specifications 'ResourceType=vpc,Tags=[{Key=Name,Value=prod-vpc}]'

CIDR block 建議用 10.0.0.0/16,這樣有 65,536 個 IP 可以用。用 /24 的話只有 256 個,後面要擴充會很卡。


Subnet 劃分:至少 4 個,跨 2 個 AZ

子網類型AZCIDR放什麼
public-subnet-1Publicap-northeast-1a10.0.1.0/24ALB、NAT GW
public-subnet-2Publicap-northeast-1c10.0.2.0/24ALB
private-subnet-1Privateap-northeast-1a10.0.10.0/24App Server、DB
private-subnet-2Privateap-northeast-1c10.0.20.0/24App Server、DB

為什麼要跨 AZ?因為 AWS 的一個 AZ 就是一個機房。機房掛了(對,會掛),另一個 AZ 還能接住流量。ALB 也強制要求至少兩個 AZ,不跨不行。

# 建立 Public Subnet
aws ec2 create-subnet --vpc-id vpc-xxx --cidr-block 10.0.1.0/24 \
  --availability-zone ap-northeast-1a \
  --tag-specifications 'ResourceType=subnet,Tags=[{Key=Name,Value=public-1a}]'
 
# 建立 Private Subnet
aws ec2 create-subnet --vpc-id vpc-xxx --cidr-block 10.0.10.0/24 \
  --availability-zone ap-northeast-1a \
  --tag-specifications 'ResourceType=subnet,Tags=[{Key=Name,Value=private-1a}]'

Internet Gateway + NAT Gateway

  • IGW 掛到 VPC → 讓 Public Subnet 能上網
  • NAT GW 建在 Public Subnet → 讓 Private Subnet 能「出去」(抓套件、更新),但外面「進不來」

這個設計很像公司大樓:大廳有門可以出去,但你不能從大廳直接走進別人的辦公室。

# 建立並掛載 IGW
aws ec2 create-internet-gateway \
  --tag-specifications 'ResourceType=internet-gateway,Tags=[{Key=Name,Value=prod-igw}]'
aws ec2 attach-internet-gateway --internet-gateway-id igw-xxx --vpc-id vpc-xxx
 
# 建立 NAT Gateway(需要先配置 Elastic IP)
aws ec2 allocate-address --domain vpc
aws ec2 create-nat-gateway --subnet-id subnet-public-1a \
  --allocation-id eipalloc-xxx \
  --tag-specifications 'ResourceType=natgateway,Tags=[{Key=Name,Value=prod-nat}]'

注意:NAT Gateway 是按小時 + 流量計費的,而且比你想像中貴。一個月跑下來可能比 EC2 還貴。省錢方案看 本系列第 11 篇


Route Table:最容易被遺忘的設定

Route Table 決定流量往哪走。設錯了,你的機器就是一座孤島。

# Public Subnet 的 Route Table:流量走 IGW
aws ec2 create-route --route-table-id rtb-public \
  --destination-cidr-block 0.0.0.0/0 --gateway-id igw-xxx
 
# Private Subnet 的 Route Table:流量走 NAT GW
aws ec2 create-route --route-table-id rtb-private \
  --destination-cidr-block 0.0.0.0/0 --nat-gateway-id nat-xxx
 
# 綁定 Route Table 到 Subnet
aws ec2 associate-route-table --route-table-id rtb-public --subnet-id subnet-public-1a
aws ec2 associate-route-table --route-table-id rtb-private --subnet-id subnet-private-1a

踩坑紀錄:我有次建了 NAT GW 但忘了改 Private Subnet 的 Route Table,結果 ECS task 起不來因為拉不到 container image。花了一小時才找到原因。Route Table 是最容易被遺忘的設定,沒有之一。


Security Group:虛擬防火牆

Security Group 控制誰能進、誰能出。記住一個原則:最小權限

你至少需要兩個 SG:

ALB 的 SG:

方向Port來源
Inbound80, 4430.0.0.0/0

App Server 的 SG:

方向Port來源
Inbound8080ALB 的 SG
Inbound22你的 IP/VPN SG

注意 App Server 的來源不是 IP,是 ALB 的 Security Group ID。這樣只有透過 ALB 的流量才進得來。

# 建立 ALB 的 SG
aws ec2 create-security-group --group-name alb-sg \
  --description "ALB Security Group" --vpc-id vpc-xxx
aws ec2 authorize-security-group-ingress --group-id sg-alb \
  --protocol tcp --port 443 --cidr 0.0.0.0/0
 
# 建立 App Server 的 SG(來源指向 ALB SG)
aws ec2 create-security-group --group-name app-sg \
  --description "App Server Security Group" --vpc-id vpc-xxx
aws ec2 authorize-security-group-ingress --group-id sg-app \
  --protocol tcp --port 8080 --source-group sg-alb

別偷懶全開 0.0.0.0/0,等你收到 AWS 的帳單通知說有人在你的機器上挖礦,就知道痛了。


OSI 對應 AWS:除錯的時候你會感謝這張表

OSI 層級對應 AWS 服務你什麼時候會碰到
L3 網路層VPC、Subnet、Route Table、IGW連不上任何東西的時候
L4 傳輸層NLB、Security Groupport 不通的時候
L7 應用層ALB、Route 53、API GatewayHTTP 回 502/504 的時候

80% 的網路問題出在 L3 和 L4。先檢查 Route Table 和 Security Group,不要一上來就懷疑 application code。


實戰:在 AWS 上自架 VPN

公司要你從外面連內部資源,或者你在咖啡廳用公共 Wi-Fi 又不想裸奔?自架 OpenVPN 是最實在的解法。

網路層準備

  1. 在 Public Subnet 開一台 EC2(t3.micro 夠用)
  2. Security Group 入站開 UDP 1194(OpenVPN)和 ICMP(ping 測試)

安裝 OpenVPN

# Amazon Linux 2023
sudo yum update -y
sudo yum install openvpn easy-rsa -y
 
# 產生憑證
cd /etc/openvpn
sudo mkdir -p easy-rsa/keys
sudo cp -r /usr/share/easy-rsa/* easy-rsa/
cd easy-rsa
 
sudo ./easyrsa init-pki
sudo ./easyrsa build-ca nopass
sudo ./easyrsa gen-req server nopass
sudo ./easyrsa sign-req server server
sudo ./easyrsa gen-dh
sudo ./easyrsa build-client-full client1 nopass
 
sudo cp pki/ca.crt pki/private/server.key pki/issued/server.crt pki/dh.pem /etc/openvpn/

Server 設定

# /etc/openvpn/server/server.conf
port 1194
proto udp
dev tun
ca /etc/openvpn/ca.crt
cert /etc/openvpn/server.crt
key /etc/openvpn/server.key
dh /etc/openvpn/dh.pem
server 10.8.0.0 255.255.255.0
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 8.8.8.8"
push "route 10.0.0.0 255.255.0.0"
keepalive 10 120
data-ciphers AES-256-GCM:AES-128-GCM
user nobody
group nobody
persist-key
persist-tun
verb 3

開啟 IP 轉發和 NAT

# 開啟 IP 轉發
sudo sysctl -w net.ipv4.ip_forward=1
echo 'net.ipv4.ip_forward=1' | sudo tee -a /etc/sysctl.conf
 
# NAT 規則(注意網卡名稱,用 ip addr 確認)
sudo iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE

踩過的坑:eth0 不一定是你的網卡名稱。有些 AMI 用的是 ens5enX0,用 ip addr 先確認再設 NAT。

啟動

sudo systemctl start openvpn-server@server
sudo systemctl enable openvpn-server@server

AWS 也有託管 VPN

如果你不想自己維護 OpenVPN,AWS 提供兩種託管方案:

  • Client VPN:點對站(員工連回 VPC),按連線時數計費
  • Site-to-Site VPN:站對站(辦公室連 VPC),需要路由器支援 IPSec

自架 OpenVPN 的好處是免費(只付 EC2 費用),壞處是你要自己維護。


部署前 Checklist

每次建新環境我都跑一遍這個清單:

  • VPC 建好,CIDR 設 /16
  • Public / Private Subnet 各兩個,跨不同 AZ
  • IGW 建好掛到 VPC
  • NAT GW 建好(放在 Public Subnet)
  • Route Table 都設對了(Public → IGW,Private → NAT GW)
  • Security Group 只開必要 port
  • EC2 的 Source/Destination Check 關掉(如果當 NAT/VPN 用)

自架 vs AWS

面向自架(pfSense/OPNsense)AWS VPC
網路隔離VLAN + 防火牆規則VPC + Subnet + SG + NACL
路由自建 RouterRoute Table
VPNOpenVPN/WireGuard 自架Client VPN / Site-to-Site VPN
NATiptables MASQUERADENAT Gateway(託管但貴)
防火牆iptables/nftablesSecurity Group(Stateful) + NACL(Stateless)
監控Netdata/PrometheusVPC Flow Logs + CloudWatch

如果你讀過 Infra 系列的 Network & DNS,VPC 就是那些概念搬到雲端——差別在你不用自己管 switch 和 cable。


K8s 映射

AWS 網路K8s 對應
VPCCluster Network(Pod CIDR + Service CIDR)
SubnetNamespace(邏輯隔離)
Security GroupNetworkPolicy
ALBIngress Controller(nginx-ingress / AWS LB Controller)
NLBService type: LoadBalancer
Route Tablekube-proxy / CNI plugin routing
NAT GatewayPod → Internet(透過 Node 的 NAT)

詳細的 K8s 網路模型,看 K8s 網路模型


系列導覽


網路設定就像水電管線——平常沒人在意,壞掉的時候所有人都在意。