Skip to main content

Lambda in a VPC: What the NAT Gateway Really Costs and How to Avoid It

What does a Lambda function pay for the privilege of being inside a VPC? Nothing at first. The cost arrives when the function needs to reach something outside the VPC, which usually means a NAT gateway, and a NAT gateway carries two separate meters. This guide is a decision analysis built on AWS documentation and pricing pages, with the questions in order: does the function need VPC access at all, what does it call once it is attached, and does each of those calls need a public path or a private one?

Start by asking whether the function needs the VPC

By default, a Lambda function runs in a Lambda-managed VPC that has internet access. Attaching a function to your own VPC restricts it to that VPC's resources unless you arrange outbound access yourself. The usual reason to attach is a private resource, such as a database or an internal API. If a function only calls public AWS APIs and a few third-party endpoints, attaching it creates a networking problem it did not have before.

The common trap is the public subnet. Connecting a function to a public subnet does not give it internet access. The function has to sit in a private subnet whose route table sends 0.0.0.0/0 to a NAT gateway placed in a public subnet. That is where the cost comes from.

A small, quiet dependency is often what pushes a function toward a NAT gateway. A function that reads credentials from Secrets Manager, for example, needs a route to that service as soon as it is attached to a VPC, even though nothing else about it is "internet-facing".

A NAT gateway bills for existing and for every byte

NAT gateway pricing has two components: an hourly charge while the gateway is provisioned and available, and a data processing charge per GB that passes through it. Published rates have been in the region of $0.045 per hour and $0.045 per GB in the US East region, and they differ by region, so check the VPC pricing page for yours before budgeting. Two things follow from the structure:

  • The hourly meter runs whether or not anything is invoked. A function that runs a few times a day still pays for a gateway that sits there around the clock. That matters most for small, bursty services, and for setups that keep one gateway per Availability Zone.
  • The processing meter does not care where the bytes are going. Traffic to S3 or DynamoDB in the same Region through a NAT gateway has no data transfer charge, but it still pays the processing fee. An S3-heavy function in a private subnet can therefore generate a bill for traffic that never touched the internet.

Gateway endpoints are free, but they cover two services

The cheapest fix is also the best documented. There is no additional charge for gateway endpoints, and they support S3 and DynamoDB. You add the endpoint as a target in the route tables of your private subnets, and traffic to those services stops going through the NAT. A minimal CloudFormation version:

S3GatewayEndpoint:
  Type: AWS::EC2::VPCEndpoint
  Properties:
    VpcId: !Ref Vpc
    VpcEndpointType: Gateway
    ServiceName: !Sub com.amazonaws.${AWS::Region}.s3
    RouteTableIds:
      - !Ref PrivateRouteTableA
      - !Ref PrivateRouteTableB
  • VpcEndpointType: Gateway is what makes it free. An interface endpoint for the same service is a different, billed resource.
  • RouteTableIds is where people slip. The endpoint only helps subnets whose route tables it is attached to. Forget one private route table and that subnet keeps sending S3 traffic through the NAT without any error.
  • The service name is regional. Traffic to the service in a different Region does not use the endpoint, because the routing is Region-specific, so a cross-Region S3 call gets no benefit.

There are limits too. The DynamoDB documentation says gateway endpoints do not serve on-premises networks, peered VPCs in other Regions, or traffic through a transit gateway. Those cases need an interface endpoint, at extra cost.

Interface endpoints trade a shared gateway for a per-AZ bill

Everything else, including Secrets Manager, needs an interface endpoint if you want to avoid the NAT. AWS documents this for Secrets Manager: an interface endpoint powered by PrivateLink lets you reach the API without an internet gateway or a NAT device.

The economics are less clear-cut than "endpoints are cheaper" suggests. Interface endpoints are billed hourly for each AZ where they exist, plus a per-GB charge, and published rates have been around $0.01 for each. The per-GB rate is lower than the NAT's, but the hourly charge is paid in every AZ you use. For a function that fetches one small secret occasionally, the per-GB saving is tiny and the endpoint can cost more than the traffic it replaces. For high-volume calls to one service, the arithmetic can flip. Verify the current rates for your Region before deciding.

One security detail is easy to miss: the default endpoint policy allows full access to Secrets Manager through the endpoint. If you add an endpoint for security reasons, attach a custom policy that scopes it, otherwise the endpoint is just another path.

The regional NAT gateway simplifies operations, not the bill

In November 2025, AWS announced a regional availability mode for NAT gateways. It is a single gateway that expands and contracts across Availability Zones based on where your workloads run, and it does not need a public subnet to host it. That removes real operational work: no per-AZ gateways to create and delete, and no route-table edits each time a workload reaches a new AZ.

It does not make the gateway cheap. According to the VPC pricing page, regional mode is charged hourly per AZ, so one gateway covering three zones is billed like three. Regional mode helps with management and availability, but it does not remove the idle-hours cost for a tiny workload.

Comparing the options

Option Reaches Cost shape Watch for
No VPC attachmentPublic endpointsNoneNo access to private resources
Gateway endpointS3 and DynamoDBFreeEvery private route table must reference it
Interface endpointMany AWS services, such as Secrets ManagerHourly per AZ, plus per GBCan cost more than the traffic it replaces; default policy is open
NAT gatewayAnything on the internetHourly, plus per GB processedIdle hours; processing fee on S3 and DynamoDB traffic
Regional NAT gatewayAnything on the internetHourly per AZ in use, plus per GBSimpler operations, not a cheaper bill

A check to run before attaching a function to a VPC

  1. Write down why the function needs the VPC. If the answer is habit or "the template had it", try removing it.
  2. List every outbound dependency: AWS APIs, third-party APIs, package mirrors and anything fetched at cold start.
  3. Cover S3 and DynamoDB with gateway endpoints and confirm each private route table references them.
  4. Cost each remaining AWS service on its own: an interface endpoint (hourly per AZ plus per GB) against its share of the NAT. Do not add endpoints by default.
  5. Scope any interface endpoint with a policy instead of leaving the full-access default.
  6. Test from inside the subnet. A timeout on the first outbound call is the typical symptom of a missing route, and a public subnet will not fix it.

Where to stay cautious

Prices in this guide are indicative, not quotes: rates differ by Region and change over time, so confirm them on the AWS pricing pages and in the pricing calculator. For destinations that support IPv6, an egress-only internet gateway can avoid a NAT for those calls, but it only helps if the destination is reachable over IPv6, so check the Lambda documentation for your configuration before relying on it.

A qualified recommendation: treat VPC attachment as a cost-bearing design choice, not a default. If you need it, start with the free gateway endpoints, add interface endpoints only where the per-AZ arithmetic works for your traffic, and keep the NAT for what is left. Then look at a cost report after a week instead of trusting the estimate.

Comments