Skip to content

InsightsCloud

Reaching cloud services privately with VPC endpoints and PrivateLink

When to use gateway endpoints, interface endpoints and PrivateLink endpoint services, how endpoint and bucket policies close the data path, and how to share endpoints across many VPCs.

Published October 14, 20265 min read

A workload in a private subnet that calls S3, ECR or Secrets Manager usually reaches them through a NAT gateway and the service's public endpoint. It works, but it has three costs. Every gigabyte through the NAT gateway carries a processing charge. The traffic leaves the VPC's private address space, so network controls cannot tell your bucket from anyone else's. And private subnets are not really private if they need a route to the internet to function.

VPC endpoints remove that dependency. On AWS there are three kinds, and choosing the wrong one is the most common mistake.

Gateway endpoints: S3 and DynamoDB

A gateway endpoint adds a route to the subnet's route table that sends traffic for S3 or DynamoDB straight to the service. There is no network interface and no hourly or data charge. For any VPC that talks to S3, it is the first endpoint to create:

endpoints.tf
resource "aws_vpc_endpoint" "s3" {
  vpc_id            = aws_vpc.main.id
  service_name      = "com.amazonaws.eu-central-1.s3"
  vpc_endpoint_type = "Gateway"
  route_table_ids   = aws_route_table.private[*].id
  policy            = data.aws_iam_policy_document.s3_endpoint.json
}

Gateway endpoints have one important limit: they only work for traffic that originates in the VPC. Clients on premises, connected over VPN or Direct Connect, and clients in peered VPCs cannot use another VPC's gateway endpoint. For those, S3 also offers interface endpoints.

Interface endpoints: everything else

An interface endpoint places an elastic network interface with a private IP address in each subnet you choose. It is powered by AWS PrivateLink and is available for most AWS services: ECR, STS, Secrets Manager, KMS, SQS, CloudWatch Logs and many more. It is billed per hour per Availability Zone, plus per gigabyte processed.

With private DNS enabled, the service's normal hostname, such as secretsmanager.eu-central-1.amazonaws.com, resolves to the endpoint's private addresses inside the VPC. SDKs and tools need no configuration change. Private DNS requires the VPC's enableDnsSupport and enableDnsHostnames attributes.

endpoints.tf
resource "aws_vpc_endpoint" "interface" {
  for_each = toset(["ecr.api", "ecr.dkr", "sts", "secretsmanager", "logs"])
 
  vpc_id              = aws_vpc.main.id
  service_name        = "com.amazonaws.eu-central-1.${each.key}"
  vpc_endpoint_type   = "Interface"
  subnet_ids          = aws_subnet.endpoints[*].id
  security_group_ids  = [aws_security_group.endpoints.id]
  private_dns_enabled = true
}

The security group needs to allow HTTPS on port 443 from the clients that use the endpoint, usually the VPC's CIDR range.

Pick the list from what the workloads actually call. Pulling images from ECR in a subnet with no NAT gateway, for example, needs ecr.api, ecr.dkr and the S3 gateway endpoint, because image layers are served from S3. Roles for service accounts on EKS need sts. A missing endpoint shows up as a timeout, not an access error, which makes the first rollout slower to debug than it should be.

Closing the data path with policies

An endpoint gives you a place to enforce policy on both sides of the connection.

Endpoint policies control what can be reached through the endpoint. The default allows everything. A useful restriction for S3 is to allow only buckets owned by your AWS organization, so a compromised workload cannot copy data to a bucket in an attacker's account:

s3-endpoint-policy.json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "OwnOrganizationBuckets",
      "Effect": "Allow",
      "Principal": "*",
      "Action": "s3:*",
      "Resource": "*",
      "Condition": {
        "StringEquals": { "aws:ResourceOrgID": "o-a1b2c3d4e5" }
      }
    },
    {
      "Sid": "EcrImageLayers",
      "Effect": "Allow",
      "Principal": "*",
      "Action": "s3:GetObject",
      "Resource": "arn:aws:s3:::prod-eu-central-1-starport-layer-bucket/*"
    }
  ]
}

The second statement matters. Some AWS features read from AWS-owned buckets, ECR image layers among them, and an organization-only policy silently breaks them. Package repositories for Amazon Linux are another example. Roll the policy out in one VPC first and watch for failed requests.

Resource policies control who can reach the resource from where. A bucket policy can deny access unless the request arrives through a specific endpoint, using the aws:SourceVpce condition key:

bucket-policy.json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "OnlyThroughEndpoint",
      "Effect": "Deny",
      "Principal": "*",
      "Action": "s3:*",
      "Resource": [
        "arn:aws:s3:::payments-exports",
        "arn:aws:s3:::payments-exports/*"
      ],
      "Condition": {
        "StringNotEquals": { "aws:SourceVpce": "vpce-0a1b2c3d4e5f60789" }
      }
    }
  ]
}

A deny like this also blocks the console and any administrator working from outside the VPC, so add an exception for a break-glass role before applying it. Together, the two policies form a data perimeter: identities can only reach approved resources, and sensitive resources can only be reached from approved networks.

workloadVPC endpointendpoint policybucket policyS3
fig. 1 — the request passes the endpoint policy, then the resource policy

Sharing interface endpoints across VPCs

In a landing zone with many VPCs connected through a Transit Gateway, a common pattern is to create interface endpoints once, in a shared services VPC, and let every other VPC use them:

  1. Create the endpoints in the shared VPC with private DNS disabled.
  2. For each service, create a Route 53 private hosted zone for the service hostname, for example sqs.eu-central-1.amazonaws.com, with an alias record pointing to the endpoint's regional DNS name.
  3. Associate those hosted zones with every spoke VPC, across accounts if needed.
  4. Route traffic from the spokes to the shared VPC through the Transit Gateway.

Clients on premises can use the same endpoints by forwarding DNS queries for those names to a Route 53 Resolver inbound endpoint.

The trade-off is that Transit Gateway data processing charges replace the per-VPC endpoint hours, and the shared VPC becomes a dependency for every workload. Centralize the endpoints with broad, steady usage and keep low-volume ones local where that is simpler. Gateway endpoints cannot be shared this way, but they are free, so every VPC should have its own.

The same mechanism works for your own services. An endpoint service puts a Network Load Balancer in front of a service in one VPC, and consumers in other VPCs or accounts create interface endpoints to reach it.

It suits a platform team offering an internal API to many teams, or a vendor offering a service to customers:

  • Overlapping CIDR ranges do not matter. Consumers connect to an address in their own VPC, so there is no routing between the networks.
  • Connections are one-way. The consumer can reach the service; the provider cannot reach into the consumer's VPC.
  • Access is explicit. The provider lists allowed principals and can require each connection request to be accepted.

The service sees connections coming from the load balancer, not from the client. Enable proxy protocol version 2 on the target group if the service needs the client's address.

Other clouds

The concepts carry over with different names. Azure Private Endpoints place a network interface for a PaaS resource in your subnet, with DNS through private DNS zones such as privatelink.blob.core.windows.net. Azure service endpoints are the older, lighter option: traffic stays on the Azure backbone, but the resource keeps its public address. Google Cloud offers Private Google Access for reaching Google APIs from subnets without external IP addresses, and Private Service Connect for endpoints to Google APIs and to published services.

A rollout order that works

  1. Add gateway endpoints for S3 and DynamoDB to every VPC. It is free and immediately cuts NAT traffic.
  2. Check NAT gateway traffic, or VPC flow logs, to see which AWS services carry the most traffic, and add interface endpoints for those.
  3. Tighten endpoint policies to your organization, with explicit exceptions for AWS-owned resources.
  4. Add aws:SourceVpce conditions to the buckets and keys that hold sensitive data.
  5. Once nothing depends on it, remove the NAT gateway from subnets that only need AWS services.