Sub topic 13
Using both in a hub-and-spoke design
This chapter assumes you know how Transit Gateway route tables, association, and propagation work, and how a PrivateLink endpoint service and interface endpoint are created. It also assumes you are comfortable with CIDR blocks, security groups, and Network Load Balancers. The goal is to combine the two services in one design and to know which one carries which traffic.
Why combine them
Transit Gateway and PrivateLink solve different problems.
- Transit Gateway joins networks. Any attachment that has a route can start a connection to any address in another attachment's CIDR range, subject to security groups and network ACLs.
- PrivateLink exposes one service. A consumer reaches a load balancer through network interfaces in its own VPC. The consumer never sees the provider's CIDR range, and the provider cannot start connections to the consumer.
A hub-and-spoke design uses both. Transit Gateway carries the broad, routed traffic: shared tooling, on-premises links, and traffic between trusted environments. PrivateLink carries the narrow traffic: one application or API that you want to expose without opening up its whole network.
| Concern | Transit Gateway | PrivateLink |
|---|---|---|
| What is shared | Routes between networks | One service behind an NLB |
| Direction of connections | Any direction the routes and firewalls allow | Consumer to provider only |
| CIDR overlap | Not allowed between attachments that route to each other | Allowed |
| Access control | Route tables, security groups, network ACLs | Allowed principals, acceptance, endpoint security groups, endpoint policies |
| Scaling unit | One attachment per VPC | One endpoint per consumer VPC per service |
A reference layout
Consider an organization with these VPCs in one Region:
- A shared services VPC (10.0.0.0/16) holding DNS, logging, and an internal package mirror.
- Two workload VPCs,
prod(10.1.0.0/16) anddev(10.2.0.0/16). - A partner-facing provider VPC (10.3.0.0/16) hosting an API behind a Network Load Balancer. Another team's account consumes this API.
The design uses a transit gateway for the first three and PrivateLink for the API.
The provider VPC has no transit gateway attachment. It is reachable only through the endpoint service, so its CIDR range never enters any route table.
Step 1: Build the routed hub
Create the transit gateway with default association and propagation disabled. This forces every attachment to be placed in a route table on purpose, instead of landing in a default table where everything can reach everything.
aws ec2 create-transit-gateway \
--description "hub" \
--options AmazonSideAsn=64512,DefaultRouteTableAssociation=disable,DefaultRouteTablePropagation=disable
aws ec2 create-transit-gateway-vpc-attachment \
--transit-gateway-id tgw-0123456789abcdef0 \
--vpc-id vpc-0aaa1111bbb22222c \
--subnet-ids subnet-0aaa1111 subnet-0bbb2222Repeat the attachment command for each VPC, using one subnet per Availability Zone you use. Attachment subnets should be small, dedicated subnets so that you can apply separate network ACLs to them.
Step 2: Choose the segmentation
For this layout, use two transit gateway route tables:
shared-rtb: associated with the shared services attachment. It has propagations fromprodanddev, so shared services can reply to both.workload-rtb: associated with theprodanddevattachments. It has a propagation from shared services only.
Because workload-rtb does not contain the other workload's CIDR, prod and dev cannot reach each other. They can both reach shared services.
aws ec2 create-transit-gateway-route-table \
--transit-gateway-id tgw-0123456789abcdef0
aws ec2 associate-transit-gateway-route-table \
--transit-gateway-route-table-id tgw-rtb-0123456789abcdef0 \
--transit-gateway-attachment-id tgw-attach-0123456789abcdef0
aws ec2 enable-transit-gateway-route-table-propagation \
--transit-gateway-route-table-id tgw-rtb-0123456789abcdef0 \
--transit-gateway-attachment-id tgw-attach-0123456789abcdef0Run the same three commands for each table and attachment pairing. Remember the two directions: association decides which table an attachment's outbound traffic is looked up in, and propagation decides which tables learn the attachment's CIDR range.
Step 3: Add VPC routes
Each spoke VPC needs a route that sends the other VPCs' ranges to the transit gateway. In prod, send shared services traffic to the hub:
aws ec2 create-route \
--route-table-id rtb-0123456789abcdef0 \
--destination-cidr-block 10.0.0.0/16 \
--transit-gateway-id tgw-0123456789abcdef0Do not add a route for 10.3.0.0/16. The provider VPC is not attached, and PrivateLink traffic does not use routes toward the provider. It goes to the endpoint's local network interfaces.
Step 4: Publish the service
In the provider account, publish the NLB as an endpoint service. Require acceptance and name the consumer account explicitly.
aws ec2 create-vpc-endpoint-service-configuration \
--network-load-balancer-arns arn:aws:elasticloadbalancing:us-east-1:111122223333:loadbalancer/net/my-svc/0123456789abcdef \
--acceptance-required
aws ec2 modify-vpc-endpoint-service-permissions \
--service-id vpce-svc-0123456789abcdef0 \
--add-allowed-principals arn:aws:iam::444455556666:rootStep 5: Consume the service from the hub's spokes
In the consumer account, create the interface endpoint in the prod VPC. Then the provider accepts the connection.
aws ec2 create-vpc-endpoint \
--vpc-endpoint-type Interface \
--vpc-id vpc-0aaa1111bbb22222c \
--service-name com.amazonaws.vpce.us-east-1.vpce-svc-0123456789abcdef0 \
--subnet-ids subnet-0aaa1111 subnet-0bbb2222 \
--security-group-ids sg-0123456789abcdef0
aws ec2 accept-vpc-endpoint-connections \
--service-id vpce-svc-0123456789abcdef0 \
--vpc-endpoint-ids vpce-0123456789abcdef0The endpoint is a set of network interfaces inside prod. Workloads in prod reach it directly. The remaining question is how dev or on-premises users reach it, which is the main design decision in this pattern.
Sharing one endpoint across the hub
Interface endpoint network interfaces have IP addresses inside the VPC where they live. Two approaches follow from that.
- One endpoint per consuming VPC. Simple and isolated. Each VPC has its own endpoint, security group, and DNS. Cost grows with the number of VPCs, because each endpoint is billed per Availability Zone per hour.
- A shared endpoint in the shared services VPC. Create the endpoint once in the hub-side VPC. Other VPCs reach its private IP addresses through the transit gateway, and you resolve its DNS name to those addresses with a Route 53 private hosted zone or a Route 53 Resolver rule shared with the spokes. This reduces endpoint count, but traffic crosses the transit gateway, which adds data processing charges and makes the transit gateway part of the service's failure path.
If you choose the shared approach, the spoke's security groups and network ACLs must allow traffic to the endpoint's addresses, and the endpoint's security group must allow the spoke CIDR ranges as sources.
Failure modes to design for
- Asymmetric routing. If a table lacks a return route, connections start but never complete. Check that both directions of every allowed flow have propagated or static routes.
- Accidental transitivity through the hub. A table that propagates every attachment recreates a flat network. Keep default association and propagation disabled.
- Availability Zone mismatch. The NLB and the endpoint must have subnets in overlapping Availability Zones. An endpoint in an Availability Zone where the service has no presence cannot reach it.
- Endpoint limits. A single endpoint is limited in bandwidth per Availability Zone and scales automatically, but bursts can still hit limits. Watch the endpoint's metrics before choosing a shared endpoint for heavy traffic.
- Unaccepted connections. With acceptance required, the endpoint stays in
pendingAcceptanceand clients time out until the provider accepts.
Decision rule
Use this rule when you place a new dependency in the design:
- If clients need many ports and protocols, or the provider must call back to clients, use Transit Gateway routing.
- If clients need one service and the provider should stay hidden, use PrivateLink.
- If the two sides have overlapping CIDR ranges, use PrivateLink, because it does not require routable, unique addresses.
- If many VPCs consume the same service, compare the cost of one endpoint per VPC against the cost of a shared endpoint plus transit gateway data processing.
Exercise
In the reference layout, dev cannot reach the API in the provider VPC, but prod can. Name two ways to give dev access, and one cost or risk of each.
Show answer
Option 1: create a second interface endpoint in the dev VPC. The risk is added hourly cost per Availability Zone, plus another security group to maintain. Option 2: share the prod (or a shared services) endpoint through the transit gateway, and give dev DNS resolution for the endpoint name. The risk is that dev traffic now crosses the transit gateway, adding data processing charges and tying the service to the hub's availability. It also requires route and security group rules that allow dev to reach the endpoint's addresses.
Exercise
Why does the design leave the provider VPC off the transit gateway?
Show answer
Attaching it would place its CIDR range in transit gateway route tables, which allows routed access to everything in that VPC and requires non-overlapping addresses. Leaving it off means consumers can reach only the NLB-backed service through PrivateLink, and the provider cannot start connections into consumer networks.