Skip to content

Commit 246221e

Browse files
aknyshclaude
andcommitted
Fix test failures: validation enforcement and subnet count
**Fixes:** 1. **TestValidationMutualExclusivity** - Replace non-blocking `check` block with `null_resource` precondition - Terraform `check` blocks only produce warnings, they don't fail the plan - Added `null_resource` with lifecycle precondition to properly fail when both NAT placement methods are specified - Added null provider (>= 3.0) to required_providers - Test now correctly expects plan failure when `nat_gateway_public_subnet_indices` AND `nat_gateway_public_subnet_names` are both set 2. **TestNATPlacementByName** - Add explicit subnet counts to test fixture - Test expected 4 public subnets (2 per AZ × 2 AZs) but only got 2 - Root cause: Missing `public_subnets_per_az_count` and `private_subnets_per_az_count` in test config - The dynamic-subnets module requires both count AND names when using named subnets - Added `public_subnets_per_az_count: 2` and `private_subnets_per_az_count: 2` to match the passing nat-by-index test pattern **Files Changed:** - src/main.tf: Removed `check` block, added `null_resource` with precondition and depends_on - src/versions.tf: Added null provider requirement - test/fixtures/stacks/catalog/usecase/nat-by-name.yaml: Added explicit subnet counts 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 81658d1 commit 246221e

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

src/main.tf

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,6 @@ module "vpc" {
9696
context = module.this.context
9797
}
9898

99-
# Validate NAT Gateway placement variable mutual exclusivity at plan time
100-
check "nat_placement_mutual_exclusivity" {
101-
assert {
102-
condition = !local.nat_placement_conflict
103-
error_message = "Cannot specify both nat_gateway_public_subnet_indices and nat_gateway_public_subnet_names. Choose one NAT placement method or leave both null for default behavior (NAT in all public subnets)."
104-
}
105-
}
106-
10799
# We could create a security group per endpoint,
108100
# but until we are ready to customize them by service, it is just a waste
109101
# of resources. We use a single security group for all endpoints.
@@ -151,10 +143,26 @@ module "vpc_endpoints" {
151143
context = module.this.context
152144
}
153145

146+
# Validation resource to check NAT Gateway placement variable mutual exclusivity
147+
# This must run before the subnets module to catch configuration errors at plan time
148+
resource "null_resource" "nat_placement_validation" {
149+
count = local.enabled ? 1 : 0
150+
151+
lifecycle {
152+
precondition {
153+
condition = !local.nat_placement_conflict
154+
error_message = "Cannot specify both nat_gateway_public_subnet_indices and nat_gateway_public_subnet_names. Choose one NAT placement method or leave both null for default behavior (NAT in all public subnets)."
155+
}
156+
}
157+
}
158+
154159
module "subnets" {
155160
source = "cloudposse/dynamic-subnets/aws"
156161
version = "3.0.1"
157162

163+
# Ensure validation runs before subnets module
164+
depends_on = [null_resource.nat_placement_validation]
165+
158166
availability_zones = local.availability_zones
159167
availability_zone_ids = local.availability_zone_ids
160168
ipv4_cidr_block = [module.vpc.vpc_cidr_block]

src/versions.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@ terraform {
66
source = "hashicorp/aws"
77
version = ">= 5.0.0"
88
}
9+
null = {
10+
source = "hashicorp/null"
11+
version = ">= 3.0"
12+
}
913
}
1014
}

test/fixtures/stacks/catalog/usecase/nat-by-name.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ components:
99
- "b"
1010
- "c"
1111
# Test named subnet configuration
12+
public_subnets_per_az_count: 2
1213
public_subnets_per_az_names: ["nat", "web"]
14+
private_subnets_per_az_count: 2
1315
private_subnets_per_az_names: ["app", "database"]
1416
# Test NAT Gateway placement by name - only in "nat" named subnets
1517
nat_gateway_public_subnet_names: ["nat"]

0 commit comments

Comments
 (0)