Repository navigation
feat: add optional NAT gateway with static Elastic IP - #47
Conversation
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
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
- 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
- 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
- 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
| 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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Will check for this in different PR since will introduce breaking changes in existing setups
| 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) |
There was a problem hiding this comment.
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"
- 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
| 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 |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Affected Components
*.tf)README.md,CONTRIBUTING.md).github/, hooks, lint config)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(defaultfalse): NAT subnets, NAT gateways and Elastic IPs in the module VPC; no public IPs on instances or subnetsnat_gateway_count(default1): 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 applyingnat_public_ipsoutputcidr_blocksmaller than /24, more Elastic IPs than subnets, duplicate Elastic IPsNotes for the Reviewer
create_before_destroy.~> 0.4.0.CHANGELOG.mdand anexamples/entry are left for the release (examples are validated against the published module).