Lab · Task 4.4 · about 40 minutes
Put Private Traffic on a Gateway Endpoint, Not NAT
Services in this lab
- Amazon VPCin scope
- NAT gatewayscope not listed
- AWS CLIin scope
Lesson for this lab: Network Cost
Goal
Build a VPC with private subnets in two Availability Zones, route S3 traffic through a gateway endpoint and internet traffic through one NAT gateway per Availability Zone, and see that a gateway endpoint can't be created for a service that only supports interface endpoints.
Before you start
- An AWS sandbox account, not a production account, with permission to manage VPCs, subnets, route tables, Elastic IP addresses, NAT gateways and VPC endpoints. Set up an AWS Budgets alert on the sandbox account before you start, and finish the clean-up step the same day.
- AWS CLI v2 configured for the sandbox account, with a Region that has at least two Availability Zones. This lab uses
us-east-1; replace it if you use another Region. - Run the commands in one terminal session so the shell variables carry over. Replace the
<...>placeholders with your own values, and never use real account IDs in notes. - This lab doesn't launch instances. It shows the network decisions in the lesson through route tables and endpoint configuration.
Steps
Steps 1 to 4 build the VPC, subnets, route tables and NAT gateways. How that layout is secured is taught in task 1.2. Here, keep your attention on which route each kind of traffic takes.
0 of 7 steps done
Create a VPC with a /16 block, then one private subnet in each of two Availability Zones.
VPC_ID=$(aws ec2 create-vpc --cidr-block 10.20.0.0/16 \ --query 'Vpc.VpcId' --output text) PRIV_A=$(aws ec2 create-subnet --vpc-id $VPC_ID --cidr-block 10.20.1.0/24 \ --availability-zone us-east-1a --query 'Subnet.SubnetId' --output text) PRIV_B=$(aws ec2 create-subnet --vpc-id $VPC_ID --cidr-block 10.20.2.0/24 \ --availability-zone us-east-1b --query 'Subnet.SubnetId' --output text) echo $VPC_ID $PRIV_A $PRIV_BEach Availability Zone gets its own route table, so each can point at the NAT gateway in its own zone.
RTB_A=$(aws ec2 create-route-table --vpc-id $VPC_ID \ --query 'RouteTable.RouteTableId' --output text) RTB_B=$(aws ec2 create-route-table --vpc-id $VPC_ID \ --query 'RouteTable.RouteTableId' --output text) aws ec2 associate-route-table --route-table-id $RTB_A --subnet-id $PRIV_A aws ec2 associate-route-table --route-table-id $RTB_B --subnet-id $PRIV_BNAT gateways must sit in a public subnet that routes to an internet gateway. Create one public subnet per Availability Zone.
IGW_ID=$(aws ec2 create-internet-gateway --query 'InternetGateway.InternetGatewayId' --output text) aws ec2 attach-internet-gateway --internet-gateway-id $IGW_ID --vpc-id $VPC_ID PUB_A=$(aws ec2 create-subnet --vpc-id $VPC_ID --cidr-block 10.20.101.0/24 \ --availability-zone us-east-1a --query 'Subnet.SubnetId' --output text) PUB_B=$(aws ec2 create-subnet --vpc-id $VPC_ID --cidr-block 10.20.102.0/24 \ --availability-zone us-east-1b --query 'Subnet.SubnetId' --output text) RTB_PUB=$(aws ec2 create-route-table --vpc-id $VPC_ID --query 'RouteTable.RouteTableId' --output text) aws ec2 create-route --route-table-id $RTB_PUB --destination-cidr-block 0.0.0.0/0 --gateway-id $IGW_ID aws ec2 associate-route-table --route-table-id $RTB_PUB --subnet-id $PUB_A aws ec2 associate-route-table --route-table-id $RTB_PUB --subnet-id $PUB_BAllocate an Elastic IP address for each NAT gateway and place each gateway in its own zone's public subnet. Wait until both are
available.EIP_A=$(aws ec2 allocate-address --domain vpc --query 'AllocationId' --output text) EIP_B=$(aws ec2 allocate-address --domain vpc --query 'AllocationId' --output text) NAT_A=$(aws ec2 create-nat-gateway --subnet-id $PUB_A --allocation-id $EIP_A \ --query 'NatGateway.NatGatewayId' --output text) NAT_B=$(aws ec2 create-nat-gateway --subnet-id $PUB_B --allocation-id $EIP_B \ --query 'NatGateway.NatGatewayId' --output text) aws ec2 wait nat-gateway-available --nat-gateway-ids $NAT_A $NAT_BThe default route for each private Availability Zone goes to the NAT gateway in that same zone.
aws ec2 create-route --route-table-id $RTB_A --destination-cidr-block 0.0.0.0/0 --nat-gateway-id $NAT_A aws ec2 create-route --route-table-id $RTB_B --destination-cidr-block 0.0.0.0/0 --nat-gateway-id $NAT_BThe gateway endpoint adds a route for the S3 prefix list to each route table you list. S3 traffic from the private subnets then skips the NAT gateways.
S3_EP=$(aws ec2 create-vpc-endpoint --vpc-id $VPC_ID \ --vpc-endpoint-type Gateway \ --service-name com.amazonaws.us-east-1.s3 \ --route-table-ids $RTB_A $RTB_B \ --query 'VpcEndpoint.VpcEndpointId' --output text) echo $S3_EPGateway endpoints exist only for Amazon S3 and Amazon DynamoDB. Ask for a gateway endpoint for Amazon EC2, a service that offers interface endpoints only.
aws ec2 create-vpc-endpoint --vpc-id $VPC_ID \ --vpc-endpoint-type Gateway \ --service-name com.amazonaws.us-east-1.ec2 \ --route-table-ids $RTB_AYou should see the call fail with an error, because gateway endpoints are available only for Amazon S3 and DynamoDB. The trap from the lesson: when the answer needs a private path to a service other than S3 or DynamoDB, the choice is an interface endpoint, not a gateway endpoint. Don't create one in this lab.
Verify
Confirm that the S3 prefix list route is present, that each private route table points its default route at its own zone's NAT gateway, and that both NAT gateways are available.
aws ec2 describe-route-tables --route-table-ids $RTB_A $RTB_B \
--query 'RouteTables[].{Table:RouteTableId,Routes:Routes[].{Dest:DestinationCidrBlock,Prefix:DestinationPrefixListId,Gateway:GatewayId,Nat:NatGatewayId,State:State}}' \
--output json
aws ec2 describe-nat-gateways --nat-gateway-ids $NAT_A $NAT_B \
--query 'NatGateways[].{Id:NatGatewayId,Az:SubnetId,State:State}' --output table
Expected result:
- Each private route table has a
0.0.0.0/0route whoseNatGatewayIdis the NAT gateway in its own zone ($NAT_Afor$RTB_A,$NAT_Bfor$RTB_B). - Each private route table has a route whose
Prefixis an S3 prefix list and whoseGatewayIdis$S3_EPwithStateactive. This is the route that keeps S3 traffic off the NAT gateways. - Both NAT gateways show
Stateavailable.
Clean up
This lab creates these resources: one VPC, four subnets (two private, two public), one internet gateway, three route tables, two NAT gateways, two Elastic IP addresses and one gateway endpoint for S3. Delete the NAT gateways and Elastic IP addresses first, then remove the endpoint, route tables, subnets, internet gateway and VPC in dependency order.
aws ec2 delete-vpc-endpoints --vpc-endpoint-ids $S3_EP
aws ec2 delete-nat-gateway --nat-gateway-id $NAT_A
aws ec2 delete-nat-gateway --nat-gateway-id $NAT_B
aws ec2 wait nat-gateway-deleted --nat-gateway-ids $NAT_A $NAT_B
aws ec2 release-address --allocation-id $EIP_A
aws ec2 release-address --allocation-id $EIP_B
aws ec2 disassociate-route-table --association-id $(aws ec2 describe-route-tables --route-table-ids $RTB_A --query "RouteTables[0].Associations[?SubnetId=='$PRIV_A'].RouteTableAssociationId" --output text)
aws ec2 disassociate-route-table --association-id $(aws ec2 describe-route-tables --route-table-ids $RTB_B --query "RouteTables[0].Associations[?SubnetId=='$PRIV_B'].RouteTableAssociationId" --output text)
aws ec2 delete-route-table --route-table-id $RTB_A
aws ec2 delete-route-table --route-table-id $RTB_B
aws ec2 delete-route --route-table-id $RTB_PUB --destination-cidr-block 0.0.0.0/0
for ASSOC in $(aws ec2 describe-route-tables --route-table-ids $RTB_PUB --query 'RouteTables[0].Associations[?!Main].RouteTableAssociationId' --output text); do
aws ec2 disassociate-route-table --association-id $ASSOC
done
aws ec2 delete-route-table --route-table-id $RTB_PUB
aws ec2 delete-subnet --subnet-id $PRIV_A
aws ec2 delete-subnet --subnet-id $PRIV_B
aws ec2 delete-subnet --subnet-id $PUB_A
aws ec2 delete-subnet --subnet-id $PUB_B
aws ec2 detach-internet-gateway --internet-gateway-id $IGW_ID --vpc-id $VPC_ID
aws ec2 delete-internet-gateway --internet-gateway-id $IGW_ID
aws ec2 delete-vpc --vpc-id $VPC_ID
Final check: confirm nothing billable remains in the VPC. This should return no NAT gateways in the available or pending state, and the VPC lookup should fail.
aws ec2 describe-nat-gateways --filter Name=vpc-id,Values=$VPC_ID \
--query 'NatGateways[?State!=`deleted`].NatGatewayId' --output text
aws ec2 describe-vpcs --vpc-ids $VPC_ID 2>&1 | head -1
The first command prints nothing. The second prints an error saying the VPC ID doesn't exist.
Sources
- https://docs.aws.amazon.com/vpc/latest/privatelink/vpc-endpoints-s3.html
- https://docs.aws.amazon.com/vpc/latest/privatelink/concepts.html
- https://docs.aws.amazon.com/vpc/latest/userguide/vpc-nat-gateway.html
- https://docs.aws.amazon.com/vpc/latest/userguide/nat-gateway-pricing.html
- https://docs.aws.amazon.com/vpc/latest/userguide/create-subnets.html
Last reviewed