<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Leonard O Sullivan</title>
    <description>The latest articles on DEV Community by Leonard O Sullivan (@leonard_o_sullivan).</description>
    <link>https://dev.to/leonard_o_sullivan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3815697%2F57ba7313-1e54-42f2-838e-f70d0f29f562.jpeg</url>
      <title>DEV Community: Leonard O Sullivan</title>
      <link>https://dev.to/leonard_o_sullivan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/leonard_o_sullivan"/>
    <language>en</language>
    <item>
      <title>nat-zero: scale-to-zero NAT instances for AWS</title>
      <dc:creator>Leonard O Sullivan</dc:creator>
      <pubDate>Tue, 28 Apr 2026 07:26:07 +0000</pubDate>
      <link>https://dev.to/leonard_o_sullivan/nat-zero-scale-to-zero-nat-instances-for-aws-55co</link>
      <guid>https://dev.to/leonard_o_sullivan/nat-zero-scale-to-zero-nat-instances-for-aws-55co</guid>
      <description>&lt;p&gt;nat-zero is a Terraform module that replaces always-on NAT infrastructure with on-demand NAT instances that start when your workloads need internet access and shut down when they don't. We built it because we're cheap and our workloads are weird.&lt;/p&gt;

&lt;p&gt;Today we're open sourcing it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;At machine.dev we run GPU workloads across every availability zone in six AWS regions. The workloads live in private subnets — no public IPs, no direct internet access. They need NAT to reach the outside world for things like pulling packages and container images.&lt;/p&gt;

&lt;p&gt;Our workloads are sporadic. We have an Intelligent Tiering system that hunts for the cheapest GPU globally, or within whatever regions the user has defined. Some AZs might not see a single job for days. Then suddenly they see fifty.&lt;/p&gt;

&lt;p&gt;AWS gives you two standard options for NAT and neither of them made sense for us:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NAT Gateway&lt;/strong&gt; costs about $36/month per AZ. We operate in every AZ across six regions. That's a lot of AZs, and most of them are sitting empty most of the time. The per-GB data processing charge on top of that would have eaten us alive. NAT Gateway is built for steady-state traffic. Our traffic is the opposite of steady-state.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Always-on NAT instances&lt;/strong&gt; (like &lt;a href="https://github.com/AndrewGuenther/fck-nat" rel="noopener noreferrer"&gt;fck-nat&lt;/a&gt;, which is genuinely good) run about $7-8/month per AZ. Better, but we were too tight-fisted to pay for instances running 24/7 in AZs that had zero workloads. Paying for a NAT instance to sit idle in ap-southeast-2b for four days straight because nobody needed a GPU in Sydney this week felt like the kind of waste that keeps you up at night. It did keep me up at night.&lt;/p&gt;

&lt;p&gt;We needed a third option: NAT that scales to zero when nothing is running, starts up when something is, and costs almost nothing in between.&lt;/p&gt;

&lt;p&gt;So we built one.&lt;/p&gt;

&lt;h2&gt;
  
  
  How nat-zero works
&lt;/h2&gt;

&lt;p&gt;The core idea is simple: a single Lambda function watches for EC2 instance state changes via EventBridge. When a workload launches in a private subnet, the Lambda starts a NAT instance in that AZ. When the last workload terminates, it stops the NAT and releases the Elastic IP.&lt;/p&gt;

&lt;p&gt;The interesting part is how it makes decisions. The Lambda runs a reconciliation loop — it doesn't care what event triggered it. It just looks at the current state of the AZ and takes one corrective action:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Workloads?&lt;/th&gt;
&lt;th&gt;NAT State&lt;/th&gt;
&lt;th&gt;EIP?&lt;/th&gt;
&lt;th&gt;Action&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;Create NAT&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Stopped&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;Start NAT&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Running&lt;/td&gt;
&lt;td&gt;No EIP&lt;/td&gt;
&lt;td&gt;Allocate and attach EIP&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Running&lt;/td&gt;
&lt;td&gt;Has EIP&lt;/td&gt;
&lt;td&gt;Converged. Do nothing.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Running&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;Stop NAT&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Stopped&lt;/td&gt;
&lt;td&gt;Has EIP&lt;/td&gt;
&lt;td&gt;Release EIP&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Stopped&lt;/td&gt;
&lt;td&gt;No EIP&lt;/td&gt;
&lt;td&gt;Converged. Do nothing.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;One action per invocation, then it returns. The next event triggers the next step. This keeps the logic dead simple and avoids the kind of race conditions that make infrastructure code age you prematurely.&lt;/p&gt;

&lt;p&gt;The Lambda runs at concurrency of one. This is deliberate. One writer means no duplicate NAT creation, no double EIP allocation, no start/stop races, and no need for distributed locking. Events that arrive while it's running just queue up. Simple beats clever every time.&lt;/p&gt;

&lt;h2&gt;
  
  
  The dual ENI trick
&lt;/h2&gt;

&lt;p&gt;Each NAT instance uses two network interfaces — one private, one public — both pre-created by Terraform. When the NAT instance stops and starts, the ENIs stick around. This means your route tables stay pointed at the right place and you don't have to reconfigure anything on restart. The EIP attaches to the public ENI when the NAT is running and gets released when it stops, so you're not paying the $3.60/month public IPv4 charge on idle AZs.&lt;/p&gt;

&lt;p&gt;This is the part that makes the scale-to-zero part actually work without breaking your routing. It's a nice bit of engineering that we're quietly proud of, despite being the kind of people who normally downplay everything.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it costs
&lt;/h2&gt;

&lt;p&gt;Here's the part we actually care about:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;State&lt;/th&gt;
&lt;th&gt;nat-zero&lt;/th&gt;
&lt;th&gt;fck-nat&lt;/th&gt;
&lt;th&gt;NAT Gateway&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Idle (no workloads)&lt;/td&gt;
&lt;td&gt;~$0.80/month&lt;/td&gt;
&lt;td&gt;~$7-8/month&lt;/td&gt;
&lt;td&gt;~$36+/month&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Active (workloads running)&lt;/td&gt;
&lt;td&gt;~$7-8/month&lt;/td&gt;
&lt;td&gt;~$7-8/month&lt;/td&gt;
&lt;td&gt;~$36+/month&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;That $0.80 idle cost is just the EBS volume sitting there waiting. No instance running, no EIP allocated, no meter ticking. When a workload shows up, you're paying the same as a regular &lt;a href="https://github.com/AndrewGuenther/fck-nat" rel="noopener noreferrer"&gt;fck-nat&lt;/a&gt; instance. When it leaves, you're back to pocket change.&lt;/p&gt;

&lt;p&gt;We run across 22 AZs. NAT Gateway would have cost us $792/month. With nat-zero, idle months cost us $17.60. That's the kind of difference that turns your AWS bill from "we need to have a meeting about this" into "that seems fine."&lt;/p&gt;

&lt;h2&gt;
  
  
  How fast is it
&lt;/h2&gt;

&lt;p&gt;The honest answer: about 10 seconds for a cold start. A NAT instance that's completely new takes roughly 10.7 seconds from workload launch to internet connectivity. Restarting a stopped NAT is about 8.5 seconds. If the NAT is already running, it's instant.&lt;/p&gt;

&lt;p&gt;For us this was fine. Our workloads are resilient to a brief wait for network access. The GPU instances themselves need time to initialize anyway — the NAT instances actually started faster than the workloads did, so in practice nobody was ever waiting for NAT. The 10-second cold start is a number we measured carefully and never actually experienced as a real delay.&lt;/p&gt;

&lt;p&gt;The Lambda itself is a compiled Go binary running on ARM64. Cold start is 55ms. Typical invocation is 400-600ms. Peak memory is 29MB out of 128MB allocated. It's fast because it does very little per invocation, which is the whole point.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why open source
&lt;/h2&gt;

&lt;p&gt;Same reason as everything else we open source: it's useful, and keeping it to ourselves doesn't make it more useful. We built nat-zero to solve a real problem we had. Other people running sporadic workloads in private subnets have the same problem. The module is self-contained, well-tested (integration tests run against real AWS infrastructure on every PR), and MIT licensed.&lt;/p&gt;

&lt;p&gt;If your workloads are bursty, spread across multiple AZs, or just not running often enough to justify always-on NAT, this might save you some money. If your workloads are steady-state and high-throughput, NAT Gateway is probably still the right call. We're not pretending this is the right solution for everyone. We're saying it was the right solution for us, and it might be for you too.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;

&lt;p&gt;The repo is at &lt;a href="https://github.com/MachineDotDev/nat-zero" rel="noopener noreferrer"&gt;github.com/MachineDotDev/nat-zero&lt;/a&gt; and the docs are at &lt;a href="https://nat-zero.machine.dev/" rel="noopener noreferrer"&gt;nat-zero.machine.dev&lt;/a&gt;. It's a Terraform module — point it at your VPC, tell it which AZs and subnets to manage, and it handles the rest.&lt;/p&gt;

&lt;p&gt;If you find a bug, open an issue. If you find a way to make it cheaper, we definitely want to hear about it.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>terraform</category>
      <category>devops</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
