
What Will This Terraform PR Cost? Reviewing AWS NAT Gateway Changes Before Apply
Bring FinOps into infrastructure code reviews with a small Node.js script that flags NAT gateway changes in a Terraform plan. Walk through a monthly cost estimate and turn traffic assumptions into a clear pull request review.
A pull request adds two NAT gateways. The networking reason makes sense. Private workloads in three Availability Zones will each have a local egress path. Then someone asks a question the Terraform plan leaves open: How much will this change add to our monthly AWS bill?
The plan can tell us that two gateways will be created. The monthly impact also depends on traffic, routing, and which charges we include. In this walkthrough, we’ll extract the gateway changes from a Terraform plan, price the part we can estimate, and turn the remaining assumptions into a short PR comment.
Start with the change the plan actually shows
Imagine an application running in three Availability Zones. Today, all three private subnets route outbound traffic through one zonal NAT gateway. The proposed design keeps that gateway, adds two more, and updates the route tables so each subnet uses a gateway in its own AZ.
Run these commands from an initialized Terraform working directory using your normal planning environment:
terraform plan -out=tfplanterraform show -json tfplan > tfplan.jsonnode review-nat.cjs tfplan.jsonKeep the binary plan and JSON inside the trusted planning environment. Terraform’s JSON output can contain sensitive values in plaintext. The script below prints only matching resource addresses and action labels. Review those addresses before posting them to a pull request. Terraform
show documentation Save this as
review-nat.cjs . It uses Node.js and has no package dependencies.const { readFileSync } = require("node:fs");const plan = JSON.parse(readFileSync(process.argv[2], "utf8"));
if ( plan.format_version?.split(".")[0] !== "1" || plan.errored || plan.complete === false || !Array.isArray(plan.resource_changes)) { throw new Error("Expected a complete Terraform plan with JSON format 1.x");}
const labels = { create: "ADD", delete: "REMOVE", update: "REVIEW UPDATE", "create,delete": "REVIEW REPLACEMENT (create first)", "delete,create": "REVIEW REPLACEMENT (delete first)", "no-op": null,};
let findings = 0;
for (const resource of plan.resource_changes) { if (resource.mode !== "managed" || resource.type !== "aws_nat_gateway") { continue; }
const action = resource.change.actions.join(","); const label = Object.hasOwn(labels, action) ? labels[action] : "REVIEW ACTION";
if (label !== null) { console.log(`${label} ${resource.address}`); findings++; }}
if (findings === 0) { console.log("No changed aws_nat_gateway resources found.");}For the example plan, the output would look like this:
ADD module.vpc.aws_nat_gateway.zonal["us-east-2b"]ADD module.vpc.aws_nat_gateway.zonal["us-east-2c"]Terraform represents a replacement as either
["create", "delete"] or ["delete", "create"] . Keeping the order visible gives the reviewer something specific to investigate. Creating first can mean a period with two gateways provisioned. Deleting first calls for a closer look at egress continuity during the change. Terraform JSON output format Estimate the monthly cost in two parts
AWS charges for zonal NAT gateway hours and for each GB the gateway processes. Standard data transfer charges can also apply. For this example, we’ll use the rates shown in the AWS pricing example for US East (Ohio), assume 730 hours per month, and hold total NAT traffic steady at 1,000 GB per month across all gateways. Amazon VPC pricing
NAT gateway charge | Before | After | Monthly change |
|---|---|---|---|
Gateway hours at $0.045/hour | $32.85 | $98.55 | +$65.70 |
Processing 1,000 GB at $0.045/GB | $45.00 | $45.00 | $0.00 |
Subtotal | $77.85 | $143.55 | +$65.70 |
The two new gateways add $65.70 per month in gateway hours. If the same 1,000 GB passes through the new design, the data processing charge stays flat. The total networking delta still depends on changes in cross-AZ traffic, other applicable transfer charges, and any additional public IPv4 addresses.
There’s another detail worth checking when you adapt the script. AWS bills Regional NAT Gateway hours per configured AZ. A plan with one gateway resource can therefore represent several billable gateway-hours per hour. Confirm the gateway mode before turning resource counts into a dollar figure. Amazon VPC pricing
Put the decision in the pull request
Here’s the comment I’d leave on this example:
Estimated increase in NAT gateway hours: $65.70/month. Two additional zonal gateways inus-east-2, assuming 730 hours per month. NAT data processing stays at $45/month if total traffic remains at 1,000 GB/month.Before merge, confirm that each private route table points to the gateway in its own AZ. Estimate how much traffic currently crosses AZs to reach the shared gateway, and include any new public IPv4 charges. That gives us the net cost of moving to local egress in each AZ.
This is where the cost review becomes an architecture conversation. If the PR is intended to reduce reliance on a single AZ for egress, the team can weigh that benefit against the added fixed charge. If the case rests on transfer savings, the reviewer now knows which traffic to measure. And if most of the traffic goes to Amazon S3 in the same Region, an S3 gateway endpoint is worth evaluating for that traffic path. It can route eligible S3 traffic away from the NAT gateway without an hourly or data processing charge for the gateway endpoint itself. Amazon VPC pricing
I’d start by running this as an advisory check on networking PRs. An empty result means the script found no changed
aws_nat_gateway resources. A route-table-only change can still redirect traffic through an existing gateway and change the bill. Keep the route review alongside the resource check, and ask the PR author to record the traffic assumption and the reason for the change while the design is still open for discussion.Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article