Sub topic 10
Reaching AWS services privately with VPC endpoints
This chapter assumes you know route tables, NAT gateways, and security groups from earlier chapters. It also assumes you have the VPC_ID, PRIVATE_SUBNET_ID, PRIVATE_RT_ID, and APP_SG variables from the earlier examples.
Why endpoints exist
Instances in a private subnet reach services such as S3 or Secrets Manager through public service addresses. With only a NAT gateway, that traffic leaves through the NAT and the internet gateway. You pay NAT data-processing charges, and the path depends on internet-facing components.
A VPC endpoint gives your VPC a private path to a supported AWS service. Traffic stays on the AWS network. There are two types, and they work differently.
| Feature | Gateway endpoint | Interface endpoint |
|---|---|---|
| Services | S3 and DynamoDB | Most other AWS services (powered by AWS PrivateLink) |
| Mechanism | Route table entry | Elastic network interface (ENI) with a private IP in your subnet |
| Security group | Not applicable | Required |
| Extra hourly cost | None | Per endpoint, per Availability Zone, plus data processing |
Gateway endpoints
A gateway endpoint adds a route to the route tables you choose. The destination is a managed prefix list for the service, and the target is the endpoint.
aws ec2 create-vpc-endpoint \
--vpc-id "$VPC_ID" \
--vpc-endpoint-type Gateway \
--service-name com.amazonaws.us-east-1.s3 \
--route-table-ids "$PRIVATE_RT_ID"S3 requests from the private subnet now match the prefix list route, which is more specific than the NAT default route. Nothing changes in your application code.
Interface endpoints
An interface endpoint places an ENI in each subnet you select. Because it has a security group, you control who can connect. Allow HTTPS from the app tier:
ENDPOINT_SG=$(aws ec2 create-security-group \
--group-name endpoint-sg \
--description "Interface endpoints" \
--vpc-id "$VPC_ID" \
--query GroupId --output text)
aws ec2 authorize-security-group-ingress \
--group-id "$ENDPOINT_SG" \
--protocol tcp --port 443 \
--source-group "$APP_SG"
aws ec2 create-vpc-endpoint \
--vpc-id "$VPC_ID" \
--vpc-endpoint-type Interface \
--service-name com.amazonaws.us-east-1.secretsmanager \
--subnet-ids "$PRIVATE_SUBNET_ID" \
--security-group-ids "$ENDPOINT_SG" \
--private-dns-enabledHow DNS ties it together
With --private-dns-enabled, the VPC resolver answers the service's normal hostname, such as secretsmanager.us-east-1.amazonaws.com, with the endpoint's private IP addresses. Your SDKs and CLI need no changes. This requires DNS support and DNS hostnames to be enabled on the VPC, which the first example in this tutorial did.
You can check from an instance in the private subnet:
nslookup secretsmanager.us-east-1.amazonaws.comThe answer should be private addresses from your subnet's CIDR range, not public ones.
Common errors
- Requests still go through NAT. The endpoint route is missing from the route table the subnet actually uses. Check the association, not just the endpoint.
- Interface endpoint calls time out. The endpoint security group does not allow inbound TCP 443 from the caller.
- The hostname resolves to a public IP. Private DNS is off, or the VPC has DNS support or hostnames disabled.
- Cross-Region S3 access fails. A gateway endpoint covers only the S3 service in its own Region.
- Access is broader than intended. Endpoints accept an endpoint policy. The default allows all actions, so restrict it for sensitive data.
Exercise
A private-subnet instance reads from S3 and calls the Secrets Manager API. You add a gateway endpoint for S3 and an interface endpoint for Secrets Manager. Which one needs a security group, and which one changes the route table?
Show answer
The interface endpoint needs a security group, because it is an ENI in your subnet. The gateway endpoint changes the route table, by adding a prefix-list route to the endpoint.