VPC Peering
A private wire between exactly two VPCs, over the AWS network, so they behave as if they were on the same network. Inter-Region and cross-account both work. Two things kill it before you start: overlapping CIDRs, and the fact that a peering connection on its own moves no packets at all.
Một sợi dây riêng nối đúng hai VPC, chạy trên hạ tầng mạng của AWS, để hai bên coi nhau như cùng một mạng. Khác Region hay khác account đều được. Nhưng có hai thứ giết nó ngay từ đầu: CIDR bị chồng lấn, và chuyện dựng xong peering connection thì vẫn chưa có gói tin nào chạy qua.
The whole page exists to answer one question in five seconds: will this topology work? Non-overlapping CIDRs, a pcx for every pair, routes on both sides — and nothing that asks one VPC to borrow another VPC's exit.
Cả trang này chỉ để trả lời nhanh một câu trong năm giây: cái topology này có chạy không? CIDR không đè nhau, cặp nào cần nói chuyện thì cặp đó phải có pcx riêng, route phải khai ở cả hai phía — và tuyệt đối không có chuyện VPC này mượn đường ra internet của VPC kia.
Peering is the right answer when… / reach for something else when…Khi nào peering là đáp án… / khi nào phải dùng thứ khác…
Use peeringDùng peering
- A handful of VPCs, and you can name every pair that needs to talk. Two, three, maybe four — past that, count the connections before you commit.Chỉ vài VPC, và bạn kể tên được từng cặp cần nói chuyện. Hai, ba, cùng lắm bốn — quá số đó thì ngồi đếm số kết nối trước khi quyết.
- Two teams, two accounts, one shared database or shared service. Peering is cheap, has no hourly appliance, and adds no hop.Hai team, hai account, dùng chung một database hay một service nội bộ. Peering rẻ, không phải nuôi thiết bị tính tiền theo giờ, và không thêm chặng nào trên đường đi.
- The traffic is VPC to VPC only — private IP to private IP. Nobody is trying to borrow the other side's internet exit.Traffic chỉ đi từ VPC này sang VPC kia — private IP gọi private IP. Không ai định mượn đường ra internet của bên còn lại.
- You need it across Regions and want the simplest possible thing that stays on the AWS backbone.Cần nối giữa hai Region và muốn giải pháp đơn giản nhất mà vẫn chạy trên backbone của AWS.
Reach for something elseChuyển sang thứ khác
- Many VPCs, any-to-any. The mesh and its route tables become the outage. → Transit Gateway.Nhiều VPC, ai cũng cần gọi ai. Chính cái mesh và đống route table của nó sẽ thành nguyên nhân sự cố. → Transit Gateway.
- Centralised egress — one NAT, one firewall, one inspection VPC for everyone. Peering physically cannot do this. → Transit Gateway.Gom đường ra internet về một chỗ — một NAT, một firewall, một VPC kiểm duyệt cho tất cả. Peering không làm được, không phải vì chưa cấu hình đúng mà vì cơ chế không cho. → Transit Gateway.
- The CIDRs overlap and you cannot renumber. Peering is off the table entirely; expose the one service instead, via PrivateLink.CIDR đè nhau mà không đánh số lại được. Peering coi như hết cửa; thay vào đó chỉ mở đúng cái service cần dùng ra ngoài bằng PrivateLink.
- You only want to expose one service, not a whole network — a consumer should not be able to reach anything else in your VPC. Peering gives network-level access; that is too much.Bạn chỉ muốn mở đúng một service chứ không mở cả mạng — bên tiêu dùng không được phép chạm tới bất cứ thứ gì khác trong VPC của bạn. Peering là mở ở mức mạng, cho như vậy là cho quá nhiều.
- On-premises to AWS. That is a VPN or Direct Connect, and peering is not part of the answer.Nối on-premises với AWS. Việc đó là của VPN hoặc Direct Connect, peering không dính dáng gì.
Setting it up — the connection is the easy halfDựng peering — tạo kết nối mới là phần dễ
Requester creates, accepter accepts, and then nothing works yet. A peering connection is a permission, not a path. The path only exists once every subnet's route table on both sides has an entry pointing the peer's CIDR at the pcx- id.
Bên A tạo, bên B accept, và tới đây vẫn chưa có gì chạy. Peering connection chỉ là cái giấy phép, chưa phải con đường. Đường chỉ thực sự có khi route table của từng subnet ở cả hai phía đều có dòng trỏ CIDR của bên kia vào id pcx-.
pcx- actually ispcx- thực ra là gì
Just the resource-ID prefix for a peering connection, the same way vpc-, subnet-, sg-, igw-, tgw- and vpce- tag their own types. Creating the connection hands you back something like pcx-0a1b2c3d4e5f6g7h8, and that string goes in the route table's Target column while the peer's CIDR goes in Destination. So 10.1.0.0/16 → pcx-0a1b2c3d reads "to reach that range, use this peering connection". Read it as peering connexion — AWS never spells it out.
Chỉ là tiền tố id của một peering connection, y như vpc-, subnet-, sg-, igw-, tgw- hay vpce- đánh dấu loại tài nguyên của chúng. Tạo xong kết nối là bạn nhận về một chuỗi kiểu pcx-0a1b2c3d4e5f6g7h8; chuỗi đó điền vào cột Target của route table, còn CIDR của bên kia điền vào cột Destination. Nên dòng 10.1.0.0/16 → pcx-0a1b2c3d đọc là "muốn tới dải đó thì đi qua peering connection này". Cứ nhớ là peering connexion — AWS không giải thích chữ viết tắt này ở đâu cả.
pcx-… id — that id is what route tables target.Khác account, khác Region đều bình thường ở bước này. Kết quả là một id pcx-… — chính id đó là target mà route table sẽ trỏ vào.
Reference the peer's security group, don't hardcode its CIDRTham chiếu security group của bên kia, đừng gõ cứng CIDR
In a security group rule the source can be a security group in the peered VPC, and this works cross-account — you write it as sg-013347765f7a63aae/123456788, the sg id followed by the owning account id. Same idea as referencing an SG inside your own VPC, just with the account spelled out.
Trong rule của security group, source có thể là một security group nằm ở VPC bên kia, và cách này chạy được cả khi khác account — viết dạng sg-013347765f7a63aae/123456788, tức id của sg rồi tới id account sở hữu nó. Bản chất giống hệt việc tham chiếu SG trong cùng VPC, chỉ thêm phần account cho rõ.
Not transitive — and the N(N−1)/2 taxKhông bắc cầu — và cái giá N(N−1)/2
A peering connection joins exactly the two VPCs named in it and refuses to forward for anyone else. A↔B plus A↔C does not produce B↔C, no matter what you write in the route tables. If B and C must talk, build B↔C. Một peering connection chỉ nối đúng hai VPC được ghi tên trong nó, và không chuyển tiếp hộ ai khác. Có A↔B và A↔C thì B↔C vẫn không thông, route table khai kiểu gì cũng vậy. B và C muốn nói chuyện thì phải dựng riêng B↔C.
Two green links exist; the dashed one does not, and no route table entry can conjure it. Read the panel on the right as the cost of insisting: the connection count grows with the square of the VPC count, and each one drags its own route table edits along with it.
Hai đường màu xanh là có thật; đường nét đứt thì không, và khai route kiểu gì cũng không tạo ra được nó. Bảng bên phải là cái giá phải trả nếu cứ cố: số kết nối tăng theo bình phương số VPC, mà kết nối nào cũng kéo theo một mớ route table phải sửa.
Longest prefix match — the most specific route winsLongest prefix match — route hẹp hơn thì thắng
When two routes both match a destination, the VPC picks the one with the longer prefix — the more specific one. That is how you send a single host to one peer and the rest of the same range to a different peer, from one route table. Khi một đích khớp với hai route cùng lúc, VPC chọn route có prefix dài hơn, tức route hẹp hơn, cụ thể hơn. Nhờ vậy mà chỉ với một route table, bạn có thể đẩy đúng một host sang peer này còn phần còn lại của dải thì sang peer kia.
Worked example. VPC A is peered to both B and C, and — legally — B and C are each 10.0.0.0/16: they overlap with each other, but neither overlaps with A's 172.16.0.0/16, so both connections are allowed. A's route table then has to disambiguate, and the prefix length does it.
Ví dụ cụ thể. VPC A peer với cả B lẫn C, và hoàn toàn hợp lệ khi B và C cùng là 10.0.0.0/16: hai đứa nó đè nhau, nhưng không đứa nào đè lên 172.16.0.0/16 của A, nên cả hai kết nối đều được phép tạo. Việc phân biệt gói tin đi đâu dồn hết vào route table của A, và độ dài prefix chính là thứ quyết định.
| Route table of…Route table của… | Destination → TargetDestination → Target | What it meansNghĩa là gì |
|---|---|---|
| VPC A | 172.16.0.0/16 → local |
its own CIDR, never removableCIDR của chính nó, không xoá được |
| VPC A | 10.0.0.77/32 → pcx-aaaabbbb |
one host, sent to VPC B — a /32 is a single addressđúng một host, đẩy sang VPC B — /32 là một địa chỉ duy nhất |
| VPC A | 10.0.0.0/16 → pcx-aaaacccc |
everything else in that range goes to VPC Ctoàn bộ phần còn lại của dải đó đi sang VPC C |
| VPC B | 172.16.0.0/16 → pcx-aaaabbbb |
the return path — without this line nothing comes backđường về — thiếu dòng này thì gói tin không quay lại được |
| VPC C | 172.16.0.0/16 → pcx-aaaacccc |
same, on C's sidetương tự, ở phía C |
Traffic to 10.0.0.77Gói đi tới 10.0.0.77 |
/32 wins overthắng /16 |
it goes to B. 10.0.0.78 goes to C.nó đi sang B. Còn 10.0.0.78 thì đi sang C. |
local always beats 0.0.0.0/0. Here it just happens to be useful: partial access to a peer, one host at a time. See VPC basics for the same rule in its everyday form.
Longest prefix match không phải đặc quyền của peering — mọi route table trong VPC đều xử lý trùng lặp theo cách này, và cũng vì thế mà local luôn thắng 0.0.0.0/0. Ở đây nó tình cờ giải quyết một nhu cầu hay gặp: mở quyền truy cập hạn chế sang peer, từng host một. Xem lại VPC basics để thấy đúng quy tắc đó trong tình huống thường ngày.
Invalid configurations — every way it refuses to workNhững cấu hình không hợp lệ — mọi kiểu peering không chạy
Three families, and they fail differently. Overlapping CIDRs fail at creation. Transitive routing fails silently — the route exists and packets vanish. Edge-to-edge routing also fails silently, and it is the one that wastes days because the design looks so reasonable. Ba nhóm, và mỗi nhóm hỏng một kiểu khác nhau. CIDR chồng lấn thì chặn ngay lúc tạo. Định tuyến bắc cầu thì hỏng âm thầm — route vẫn nằm đó mà gói tin biến mất. Edge-to-edge routing cũng hỏng âm thầm, và đây mới là cái ngốn cả mấy ngày, vì nhìn vào sơ đồ thấy hợp lý quá.
1 · Overlapping IPv4 CIDR — check every block, not just the first1 · CIDR IPv4 chồng lấn — soát mọi khối, đừng chỉ nhìn khối đầu
172.16.0.0/16 ↔ 172.16.0.0/16 · the obvious one. Rejected.trường hợp ai cũng thấy. Bị từ chối.
10.0.0.0/16 + 10.3.0.0/16, B has 10.0.0.0/16 + 10.2.0.0/16. The extra blocks differ, but one pair overlaps — that is enough. Rejected.A có 10.0.0.0/16 + 10.3.0.0/16, B có 10.0.0.0/16 + 10.2.0.0/16. Mấy khối kia khác nhau thật, nhưng có một cặp đè nhau — thế là đủ. Bị từ chối.
172.16.0.0/16 on IPv4 while their IPv6 prefixes are completely different. Still rejected — the IPv4 overlap decides it.Hai bên cùng 172.16.0.0/16 ở IPv4, còn prefix IPv6 thì khác hẳn nhau. Vẫn bị từ chối — IPv4 đè nhau là chốt, IPv6 không cứu được.
2 · No transitive peering2 · Không có peering bắc cầu
B → A → C is not a path. Covered above; the only fix is a direct B↔C connection, or Transit Gateway. B → A → C không phải là một đường đi. Đã nói ở trên; cách duy nhất là dựng thẳng B↔C, hoặc chuyển sang Transit Gateway.
3 · No edge-to-edge routing — the one that matters most3 · Không có edge-to-edge routing — cái quan trọng nhất
A peered VPC cannot use the edge devices of its peer. Not the IGW, not the NAT Gateway, not a VPN, not Direct Connect, and not a Gateway VPC Endpoint to S3 or DynamoDB. Peering carries VPC-to-VPC traffic and stops there. VPC đã peer thì không dùng được các thiết bị biên của phía bên kia. Không dùng được IGW, không dùng được NAT Gateway, không đi ké VPN hay Direct Connect, và cũng không xài được Gateway VPC Endpoint trỏ tới S3 hay DynamoDB. Peering chỉ chở traffic giữa hai VPC, hết.
The peerings themselves are fine — A and B can each reach resources inside the central VPC. What dies at the red X is the second hop: out through the central VPC's NAT and IGW. Peering ends at the VPC edge, so a shared egress VPC built this way has no path to the internet at all.
Bản thân hai peering vẫn hoạt động bình thường — A và B đều với tới được tài nguyên nằm trong Central VPC. Cái chết ở dấu X đỏ là chặng thứ hai: đi tiếp ra NAT rồi IGW của Central VPC. Peering dừng lại ngay ở biên của VPC, nên một VPC egress dùng chung dựng theo kiểu này thì không có đường ra internet, chấm hết.
Things that catch you outNhững chỗ hay sập bẫy
- The connection is active and nothing pings. Nine times out of ten it is a route table you didn't touch. "Active" only means both accounts agreed; it says nothing about whether any subnet knows how to reach the peer. Trạng thái active mà ping không thông. Mười lần thì chín là do một route table bạn chưa đụng tới. Chữ "active" chỉ nói hai account đã đồng ý với nhau, chứ không hề nói subnet nào biết đường sang peer cả.
- It works from one subnet and not the neighbouring one. Different subnets, different route tables. Enumerate the route tables, not the subnets — one table can be shared by several subnets and one subnet can have its own. Từ subnet này thì được, subnet ngay bên cạnh thì không. Vì mỗi subnet gắn một route table khác nhau. Hãy liệt kê theo route table chứ đừng liệt kê theo subnet — một table có thể dùng chung cho nhiều subnet, mà cũng có subnet xài table riêng của nó.
- Requests go out, replies never come back. You added the route on your side only. Peering routing is symmetric by obligation: the return path needs its own entry in the peer's tables. Gói gửi đi được, gói trả về thì mất tăm. Bạn mới khai route ở phía mình. Peering bắt buộc phải khai đối xứng: đường về cũng cần dòng route riêng trong table của bên kia.
- Two CIDR blocks, only one checked. A VPC can carry several CIDR blocks. AWS compares all of them, so a peering can be rejected because of a secondary block you forgot the VPC had. VPC có hai khối CIDR mà bạn chỉ soát một. Một VPC gắn được nhiều khối CIDR, và AWS đối chiếu hết. Nên peering có thể bị từ chối vì một khối phụ mà bạn quên mất là VPC đang có.
- IPv6 does not rescue an IPv4 overlap. Different IPv6 prefixes are irrelevant if the IPv4 ranges collide. The check is per address family, and IPv4 is the one that blocks you. IPv6 không cứu được việc IPv4 đè nhau. Prefix IPv6 có khác nhau cỡ nào cũng vô nghĩa nếu dải IPv4 đụng nhau. AWS xét riêng từng họ địa chỉ, và IPv4 mới là cái chặn bạn lại.
- You cannot grow into it. The pair count is N(N−1)/2, so adding the 11th VPC to a 10-VPC mesh means 10 new connections and route entries in every existing table. Decide between peering and Transit Gateway early, not at VPC number nine. Không thể "cứ làm rồi mở rộng sau". Số cặp là N(N−1)/2, nên thêm VPC thứ 11 vào một mesh 10 VPC là 10 kết nối mới, cộng thêm route phải khai trong mọi table đang có. Hãy chốt giữa peering và Transit Gateway từ sớm, đừng để tới lúc dựng VPC thứ chín mới nghĩ.
- Peering gives network-level access. Once the routes exist, anything the security groups allow is reachable across the whole peer CIDR. If you only meant to share one service, that is a bigger door than you wanted. Peering là mở quyền ở mức mạng. Route đã khai xong thì mọi thứ mà security group cho phép đều với tới được trong cả dải CIDR của peer. Nếu ý bạn chỉ là chia sẻ một service, thì cánh cửa vừa mở ra rộng hơn ý định rất nhiều.
Exam reflexesPhản xạ khi thi
| The question says…Đề nói… | AnswerĐáp án |
|---|---|
| "Two VPCs, different accounts, must reach each other privately""Hai VPC, khác account, cần gọi được nhau qua mạng riêng" | VPC Peering — cross-account and inter-Region are both supportedVPC Peering — khác account, khác Region đều được hỗ trợ |
| "Peering is active but instances still can't connect""Peering đã active mà instance vẫn không kết nối được" | Route tables in every subnet on both sides, then the security groupsRoute table của mọi subnet ở cả hai bên, sau đó mới tới security group |
| "VPC B and VPC C are both peered to A but can't talk to each other""VPC B và VPC C đều peer với A nhưng không nói chuyện được với nhau" | Peering is not transitive — create B↔C, or move to Transit GatewayPeering không bắc cầu — tạo thêm B↔C, hoặc chuyển sang Transit Gateway |
| "Centralised internet egress through one NAT for many VPCs""Gom đường ra internet qua một NAT duy nhất cho nhiều VPC" | Transit Gateway. Peering has no edge-to-edge routing — it can never be thisTransit Gateway. Peering không có edge-to-edge routing — vĩnh viễn không làm được việc này |
| "Can VPC B use VPC A's IGW / VPN / Direct Connect / S3 Gateway Endpoint?""VPC B có xài được IGW / VPN / Direct Connect / S3 Gateway Endpoint của VPC A không?" | No. Every one of those is edge-to-edge routingKhông. Tất cả những cái đó đều là edge-to-edge routing |
| "Allow only one host in the peer to be reached, the rest elsewhere""Chỉ cho phép với tới một host bên peer, phần còn lại đi hướng khác" | Longest prefix match — a /32 route to one pcx, the /16 to anotherLongest prefix match — route /32 trỏ vào một pcx, còn /16 trỏ pcx khác |
| "Restrict access to specific instances in the peer, and it autoscales""Giới hạn quyền truy cập tới đúng vài instance bên peer, mà chúng lại autoscale" | Reference the peer VPC's security group as the source — works cross-accountĐặt source là security group của VPC bên kia — chạy được cả khi khác account |