Skip to main content
MEDIUMLambdaCost Optimization

Lambda VPC NAT Gateway Cost Warning

lambda-vpc-nat-cost

What this rule checks

Warns about potential NAT Gateway costs for VPC-attached Lambda functions.

How to fix it

  1. 1Use VPC endpoints instead of NAT Gateway where possible
  2. 2Consider if VPC attachment is necessary
  3. 3Monitor NAT Gateway data processing costs
FlaggedA default `ec2.Vpc` provisions a NAT Gateway, and attaching the function with `vpc` gives it a `VpcConfig.SubnetIds`. The check fires only when the template has both a NAT Gateway and a VPC-attached Lambda, warning about accumulating NAT charges (MEDIUM, Cost Optimization).
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as lambda from 'aws-cdk-lib/aws-lambda';

// inside your Stack
const vpc = new ec2.Vpc(this, 'Vpc', { maxAzs: 1 });
new lambda.Function(this, 'Fn', {
  runtime: lambda.Runtime.NODEJS_22_X,
  handler: 'index.handler',
  code: lambda.Code.fromInline('exports.handler = async () => {};'),
  vpc,
});
FixedConfiguring the VPC with `natGateways: 0` and isolated subnets (reaching AWS services via VPC endpoints instead) removes the NAT Gateway from the template. With no NAT Gateway the check short-circuits and never warns.
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as lambda from 'aws-cdk-lib/aws-lambda';

// inside your Stack
const vpc = new ec2.Vpc(this, 'Vpc', {
  natGateways: 0,
  subnetConfiguration: [
    { name: 'isolated', subnetType: ec2.SubnetType.PRIVATE_ISOLATED },
  ],
});
new lambda.Function(this, 'Fn', {
  runtime: lambda.Runtime.NODEJS_22_X,
  handler: 'index.handler',
  code: lambda.Code.fromInline('exports.handler = async () => {};'),
  vpc,
  vpcSubnets: { subnetType: ec2.SubnetType.PRIVATE_ISOLATED },
});

CDK Insights pinpoints the exact file and line in your CDK source for every finding, so you can jump straight to the fix.

Affected resource types

AWS::Lambda::Function

AWS documentation

Read the AWS guidance

Intentional? Suppress this finding

Sometimes a flag is deliberate โ€” a genuinely public endpoint, say. You can dismiss lambda-vpc-nat-cost and the reason is kept in the report, not silently hidden.

In .cdk-insights.json:

{
  "ignoreRules": [
    { "id": "lambda-vpc-nat-cost", "reason": "Why this is intentional" }
  ]
}

Or inline in your CDK code:

Validations.of(scope).acknowledge({
  id: 'cdk-insights::lambda-vpc-nat-cost',
  reason: 'Why this is intentional',
});

Use the rule ID lambda-vpc-nat-cost shown above โ€” not the CDK-* ID from SARIF / GitHub code scanning. To dismiss every finding on one construct instead, use ignorePaths. Suppression docs โ†’

Catch this in your stack

$ npx cdk-insights scan

CDK Insights runs this and 118+ other rules locally against your synthesised CDK app โ€” free, no account, your code never leaves your machine.

More Lambda rules