Skip to content

feat: add optional NAT gateway with static Elastic IP - #47

Merged
ianaya89 merged 13 commits into
masterfrom
feat/nat-gateway
Nov 20, 2026
Merged

ianaya89 merged 13 commits into
masterfrom
feat/nat-gateway

Conversation

@ianaya89

@ianaya89 ianaya89 commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Affected Components

  • Module (*.tf)
  • Examples
  • Docs (README.md, CONTRIBUTING.md)
  • CI / Tooling (.github/, hooks, lint config)
  • Other

Description

Opt-in NAT gateway mode: cloud-connect runs without public IPs and all outbound traffic leaves through NAT gateway(s), so its egress IPs are known and can be allowlisted. No plan changes when the flag is off.

  • enable_nat_gateway (default false): NAT subnets, NAT gateways and Elastic IPs in the module VPC; no public IPs on instances or subnets
  • nat_gateway_count (default 1): one NAT gateway per AZ; instances launch only in AZs that have one (no cross-AZ traffic)
  • nat_eip_allocation_ids: pre-allocated Elastic IPs (one NAT gateway per ID), so they can be allowlisted before applying
  • nat_public_ips output
  • Default route table points to the first NAT gateway (fail-closed); only the NAT subnets route to the IGW; with more than one NAT gateway each subnet gets its own route table
  • ASG waits for its routes, so instances don't boot without egress
  • Plan fails on invalid setups: flag without the module VPC, cidr_block smaller than /24, more Elastic IPs than subnets, duplicate Elastic IPs
  • Minimum Terraform version is now 1.2 (preconditions); breaking for 1.0/1.1 users

Notes for the Reviewer

  • Applied in a real AWS account as a fresh install with a pre-allocated Elastic IP (single NAT gateway), on an earlier commit of this branch: launch hook passes and all AWS API calls made by the instance come from the Elastic IP (CloudTrail). Not tested there yet: the latest route table / subnet changes, more than one NAT gateway, and enabling the flag in place on an existing module VPC.
  • Changing the Elastic IPs replaces the NAT gateways (short egress interruption); documented instead of using create_before_destroy.
  • README example uses ~> 0.4.0. CHANGELOG.md and an examples/ entry are left for the release (examples are validated against the published module).

Lets cloud-connect run in private subnets and egress through a single
static IP, for accounts where AWS API calls are only allowed from
allowlisted source IPs (e.g. organization SCPs).

- enable_nat_gateway: creates a public subnet, NAT gateway and private
  route table in the module VPC; instances launch without public IPs
- nat_eip_allocation_id: reuse a pre-allocated Elastic IP
- nat_public_ip output
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T16:13:22.074807Z 1ee996b PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Comment thread vpc.tf Resolved
Comment thread vpc.tf Resolved
Comment thread vpc.tf Resolved
- default route table points to the NAT gateway when NAT is enabled;
  only the NAT subnets are associated with an explicit public route table
- one NAT gateway, Elastic IP and private route table per AZ, tunable
  via nat_gateway_count
- nat_eip_allocation_id -> nat_eip_allocation_ids, nat_public_ip ->
  nat_public_ips
- drop redundant depends_on from aws_eip.nat
Comment thread variables.tf Resolved
Comment thread vpc.tf Resolved
Comment thread vpc.tf Resolved
- nat_gateway_count defaults to replicas and is only capped by the
  number of subnets
- with NAT, instances only launch in AZs that have a NAT gateway
- first subnet uses the default route table; private route tables only
  for additional AZs
- rename public route table/association to nat
- NAT gateway count now has a single source (nat_gateway_count/replicas)
- the launch template already disables public IPs with NAT, no need to
  change the subnets
Comment thread README.md Resolved
Comment thread README.md Resolved
- bring back nat_eip_allocation_ids so egress IPs can be allowlisted
  before applying
- subnets no longer auto-assign public IPs when NAT is enabled
- nat_gateway_count defaults to 1 instead of following replicas
- fail the plan when enable_nat_gateway is set without the module VPC,
  with a cidr_block smaller than /24, or with more Elastic IPs than
  subnets (requires Terraform >= 1.2 for preconditions)
- instances launch in any subnet again; subnets without their own NAT
  gateway use the first one, so capacity or instance type gaps in one
  AZ don't block launches
- document pre-allocated Elastic IPs as the path for allowlist-first
  setups and the interruption when they change
@ianaya89
ianaya89 requested a review from shyiko November 18, 2026 15:27
denisok
denisok previously approved these changes Nov 18, 2026
Comment thread vpc.tf Outdated
count = local.create_vpc ? min(length(data.aws_availability_zones.available[0].names), 3) : 0
count = local.subnet_count
vpc_id = aws_vpc.this[0].id
cidr_block = var.cidr_block == "10.0.0.0/16" ? cidrsubnet("10.0.0.0/16", 8, count.index + 1) : cidrsubnet(var.cidr_block, 2, count.index + 1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out of scope observation (code already in master): cidrsubnet(var.cidr_block, 2, count.index + 1)
makes it impossible to have more than 3 subnets; it's also not clear why custom cidr is following a different split logic

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will check for this in different PR since will introduce breaking changes in existing setups

Comment thread vpc.tf Outdated
resource "aws_subnet" "nat" {
count = local.nat_count
vpc_id = aws_vpc.this[0].id
cidr_block = var.cidr_block == "10.0.0.0/16" ? cidrsubnet("10.0.0.0/16", 12, count.index) : cidrsubnet(var.cidr_block, 4, count.index)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same issues as in case of https://github.com/Altinity/terraform-altinitycloud-connect-aws/pull/47/changes#r4618906804 with extra:

cidr split logic leads to overlaps if/when https://github.com/Altinity/terraform-altinitycloud-connect-aws/pull/47/changes#r4618906804 gets fixed:

resource "aws_subnet" "this" {
  count.index = 0
  cidr_block = cidrsubnet("10.0.0.1/16", 2, count.index + 1) == "10.0.64.0/18"

resource "aws_subnet" "nat" {
  count.index = 4
  cidr_block = cidrsubnet("10.0.0.1/16", 4, count.index) == "10.0.64.0/20"

Comment thread vpc.tf Resolved
Comment thread vpc.tf Resolved
Comment thread README.md Resolved
Comment thread README.md Resolved
Comment thread README.md Resolved
Comment thread variables.tf Resolved
Comment thread main.tf Resolved
- one route table per subnet when there is more than one NAT gateway
- consistent Name tags (drop "private", NAT gateway uses the -nat suffix)
- NAT subnets carved from the block the instance subnets never use
- move preconditions off the autoscaling group
- drop the redundant whole-number validation
- trim the README section
Comment thread vpc.tf
create_vpc = !var.use_default_subnets && length(var.subnets) == 0
create_vpc = !var.use_default_subnets && length(var.subnets) == 0
create_nat = local.create_vpc && var.enable_nat_gateway
subnet_count = local.create_vpc ? min(length(data.aws_availability_zones.available[0].names), 3) : 0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

based on the code, when vpc is provisioned by the module, subnet_count is always 2 or 3
asg has

  vpc_zone_identifier = length(var.subnets) > 0 ? var.subnets : (
    var.use_default_subnets ? data.aws_subnets.default[0].ids : aws_subnet.this[*].id
  )

i.e. it's impossible to have a single cloud-connect running behind a nat without either paying for multiple nat gateways or paying for cross-az traffic (depending on where ec2 instance lands)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can limit the ASG to the subnets that have a NAT gateway in their AZ, so a single NAT gateway means a single AZ and no cross-AZ traffic. The remaining subnets stay unused. I'd keep them so enabling the flag on an existing install doesn't destroy subnets. OK with that?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. With NAT the ASG now only uses the subnets that have a NAT gateway in their AZ, so a single NAT gateway means a single AZ and no cross-AZ traffic. The other subnets are still created but unused, so enabling the flag on an existing install doesn't destroy subnets.

With a single NAT gateway the instance could land in another AZ and pay for cross-AZ traffic.
@ianaya89
ianaya89 requested a review from shyiko November 19, 2026 21:31
@ianaya89
ianaya89 merged commit 9fe804a into master Nov 20, 2026
3 checks passed
@ianaya89
ianaya89 deleted the feat/nat-gateway branch November 20, 2026 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants