Sub topic 10
Reaching AWS services privately with VPC endpoints
This chapter assumes you can read CIDR notation, know what a route table and a security group do, and have a VPC with a private subnet behind a NAT gateway like the one built earlier in this tutorial. You should also be comfortable running AWS CLI commands.
Why endpoints exist
AWS services such as S3, DynamoDB, SQS, and Secrets Manager are reached through public service endpoints, for example s3.us-east-1.amazonaws.com. By default, an instance in a private subnet reaches these addresses through a NAT gateway and then an internet gateway. The traffic stays inside the AWS network for AWS-owned destinations, but the path has three drawbacks:
- You pay NAT gateway hourly and per-GB processing charges for every byte, including large S3 transfers.
- The NAT gateway is a shared egress point that you must size and keep available.
- Your security policy may forbid any route to the internet, even one that only reaches AWS.
A VPC endpoint is a VPC component that lets resources in your VPC reach a supported service using private IP connectivity. No internet gateway, NAT device, or public IP is needed on the instance.
There are two main endpoint types that you will use here.
| Property | Gateway endpoint | Interface endpoint |
|---|---|---|
| Supported services | S3 and DynamoDB only | Most AWS services, plus services published through AWS PrivateLink |
| How traffic is steered | A route table entry that targets the endpoint | Elastic network interfaces (ENIs) with private IPs in your subnets, reached through DNS |
| Security group | Not applicable | Required, attached to the ENIs |
| Cost | No charge | Hourly charge per Availability Zone plus per-GB data processing |
| Reachable from on-premises or peered VPCs | No | Yes, over VPN, Direct Connect, or peering (for the service's private IPs) |
| Resource policy | Endpoint policy | Endpoint policy |
A third type, the Gateway Load Balancer endpoint, steers traffic to security appliances. It is outside this chapter.
Gateway endpoints
A gateway endpoint works at the routing layer. When you create one, AWS adds a route to each route table you choose. The destination of that route is a prefix list, a managed set of the service's public IP ranges, and the target is the endpoint ID (vpce-...). Because the prefix list is more specific than 0.0.0.0/0, traffic to S3 matches it first and bypasses the NAT gateway.
The following command adds an S3 gateway endpoint to the private route table. It uses the variables from the earlier chapters.
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"You can confirm the route was added:
aws ec2 describe-route-tables \
--route-table-ids "$PRIVATE_RT_ID" \
--query "RouteTables[0].Routes"The output now includes an entry with a DestinationPrefixListId (such as pl-63a5400a) and a GatewayId that begins with vpce-, next to the existing 0.0.0.0/0 route to the NAT gateway.
Gateway endpoint behavior to know
- Route tables decide who uses it. Only subnets associated with the selected route tables use the endpoint. A subnet that uses a different table keeps going through the NAT gateway.
- Same Region only. A gateway endpoint reaches S3 or DynamoDB in the Region where you created it. Cross-Region requests do not use it.
- No on-premises access. The prefix list route exists only inside the VPC. Traffic from a VPN or Direct Connect connection, from a peered VPC, or from a Transit Gateway attachment cannot use your gateway endpoint.
- Security groups do not apply. The endpoint has no network interface. You control access with the endpoint policy and with outbound rules on your instance's security group. If you restrict outbound traffic, allow the service's prefix list as the destination.
- Public IPs still appear. The instance resolves the normal public S3 hostname and sends traffic to a public IP. The route table, not DNS, redirects it onto the endpoint.
Interface endpoints
An interface endpoint is built on AWS PrivateLink. AWS creates one ENI in each subnet you select. Each ENI receives a private IP from that subnet's CIDR block. Your instances connect to those private IPs, and AWS carries the traffic to the service.
This example creates an interface endpoint for Secrets Manager in the private subnet. It first creates a security group that allows 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-enabledThe security group on the endpoint ENIs must allow inbound TCP 443 from your clients. If the rule is missing, connections time out and nothing in the console says why. Referencing the app security group, as above, follows the least-privilege pattern from the security groups chapter.
Private DNS
Interface endpoints rely on DNS. With private DNS enabled, AWS creates a hidden Route 53 private hosted zone in your VPC. It makes the service's standard hostname, such as secretsmanager.us-east-1.amazonaws.com, resolve to the endpoint's private IPs. Your SDKs and the AWS CLI then use the endpoint with no code change.
Private DNS has two prerequisites on the VPC, both described in the DNS chapter:
enableDnsSupportmust betrue.enableDnsHostnamesmust betrue. The VPC created earlier in this tutorial already turned this on.
If private DNS is off, the service's standard hostname still resolves to public addresses. You must instead use the endpoint-specific names, which look like vpce-0abc123-xyz.secretsmanager.us-east-1.vpce.amazonaws.com, and configure your client to use them.
You can verify the behavior from an instance in the private subnet:
nslookup secretsmanager.us-east-1.amazonaws.comA correct result lists addresses inside 10.0.2.0/24, not public IPs.
Interface endpoint behavior to know
- One ENI per subnet, per endpoint. For availability, select a subnet in each Availability Zone that hosts clients. This also avoids cross-AZ data charges.
- Cost grows with sprawl. Each endpoint costs an hourly rate per AZ. Ten services across three AZs means thirty billed ENIs. Compare this with your NAT data charges before replacing the NAT gateway entirely.
- Reachable from outside the VPC. On-premises clients and peered VPCs can connect to the endpoint ENI IPs. DNS must be forwarded to resolve the endpoint addresses, which usually means a Route 53 Resolver inbound endpoint.
- S3 also offers an interface endpoint. Use it when you need on-premises access to S3 over private IPs. For traffic that originates inside the VPC, the free gateway endpoint is usually the better choice.
Endpoint policies
Every endpoint can carry an endpoint policy, an IAM-style resource policy that limits what can be done through that endpoint. The default policy allows all actions by all principals. The policy does not grant permissions. Access is allowed only when both the caller's IAM policy and the endpoint policy allow the request.
This policy restricts an S3 gateway endpoint to a single bucket:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowOneBucket",
"Effect": "Allow",
"Principal": "*",
"Action": ["s3:GetObject", "s3:PutObject", "s3:ListBucket"],
"Resource": [
"arn:aws:s3:::my-app-data",
"arn:aws:s3:::my-app-data/*"
]
}
]
}Apply it with aws ec2 modify-vpc-endpoint --vpc-endpoint-id vpce-0abc123 --policy-document file://policy.json. Replace the endpoint ID with your own.
You can also write the reverse control. A bucket policy can deny any request that does not arrive through a specific endpoint by using the aws:sourceVpce condition key. This closes off access from the internet even for principals with valid credentials.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenyOutsideEndpoint",
"Effect": "Deny",
"Principal": "*",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::my-app-data",
"arn:aws:s3:::my-app-data/*"
],
"Condition": {
"StringNotEquals": {
"aws:sourceVpce": "vpce-0abc123"
}
}
}
]
}A policy like this also blocks you, including from the console, so test it on a non-critical bucket first.
How a request chooses its path
The diagram shows how an instance in the private subnet reaches each service after you add both endpoint types.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| S3 calls from a private subnet still use the NAT gateway | The subnet's route table was not selected when the gateway endpoint was created | Run `aws ec2 modify-vpc-endpoint --vpc-endpoint-id <id> --add-route-table-ids <rtb-id>` |
| Interface endpoint calls time out | The endpoint security group does not allow inbound 443 from the client | Add an ingress rule for the client security group |
| Hostname still resolves to public IPs | Private DNS is off, or `enableDnsHostnames` is false | Enable both settings, or use the `vpce-` hostname |
| `AccessDenied` only from inside the VPC | The endpoint policy or a `sourceVpce` condition blocks the request | Review both policies |
| Cross-Region S3 access fails or uses the NAT gateway | A gateway endpoint covers only its own Region | Use the NAT gateway, or create an endpoint in the target Region and peer the VPCs |
| Client restricts outbound traffic and S3 fails | Egress rules do not include the S3 prefix list | Allow the prefix list in the instance's outbound rules |
Exercises
Exercise
A private subnet has no NAT gateway. An instance there can reach S3 after you add a gateway endpoint, but it cannot reach SQS. Why, and what is the simplest fix?
Show answer
Gateway endpoints exist only for S3 and DynamoDB. SQS needs an interface endpoint (service name com.amazonaws.<region>.sqs) with private DNS enabled, and a security group that allows inbound TCP 443 from the instance.
Exercise
You enabled private DNS on an interface endpoint, but nslookup from an instance returns public IPs. Name two VPC settings to check.
Show answer
Check that enableDnsSupport and enableDnsHostnames are both true on the VPC. Also confirm that private DNS is enabled on the endpoint itself.
Exercise
An on-premises server connected through Site-to-Site VPN must read from S3 without using the internet. Does an S3 gateway endpoint solve this? What does?
Show answer
No. Gateway endpoints are reachable only from inside the VPC route tables that include them, not from VPN, Direct Connect, or peered networks. Create an S3 interface endpoint and make its DNS names resolvable from on-premises, for example with a Route 53 Resolver inbound endpoint.