fix(elbv2): enforce ALB subnet availability zone rules (#1247)

* fix(elbv2): enforce ALB subnet availability zone rules

* test(cloudformation): use real default subnets in ELBv2 stack test
This commit is contained in:
shrimptails-f authored and GitHub committed 2026-06-12 22:41:17 -05:00
1 parent f8cf476848
commit 70e63caf10
3 files changed
+164 -45

No files matched your search

@@ -5127,7 +5127,7 @@ class CloudFormationIntegrationTest {
"Name": "cfn-tg",
"Protocol": "HTTP",
"Port": 80,
"VpcId": "vpc-00000001",
"VpcId": "vpc-default",
"TargetType": "ip",
"HealthCheckPath": "/health",
"Matcher": { "HttpCode": "200-299" }
@@ -295,6 +295,7 @@ class ElbV2IntegrationTest {
.formParam("Name", "set-subnets-test-lb")
.formParam("Type", "application")
.formParam("Subnets.member.1", "subnet-default-a")
.formParam("Subnets.member.2", "subnet-default-b")
.header("Authorization", AUTH)
.when()
.post("/")
@@ -318,23 +319,58 @@ class ElbV2IntegrationTest {
@Test
@Order(12)
void setSubnetsAfterSubnetlessCreateCanAdoptCustomVpc() {
String customVpcId = given()
.formParam("Action", "CreateVpc")
.formParam("CidrBlock", "10.2.0.0/16")
void createLoadBalancerWithSingleSubnetThrowsInvalidConfigurationRequest() {
given()
.formParam("Action", "CreateLoadBalancer")
.formParam("Name", "single-subnet-lb")
.formParam("Type", "application")
.formParam("Subnets.member.1", "subnet-default-a")
.header("Authorization", AUTH)
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("InvalidConfigurationRequest"));
}
@Test
@Order(13)
void createLoadBalancerWithSubnetsInSameAvailabilityZoneThrowsInvalidConfigurationRequest() {
String secondSubnetId = given()
.formParam("Action", "CreateSubnet")
.formParam("VpcId", "vpc-default")
.formParam("CidrBlock", "172.31.64.0/24")
.formParam("AvailabilityZone", "us-east-1a")
.header("Authorization", EC2_AUTH)
.when()
.post("/")
.then()
.statusCode(200)
.extract()
.path("CreateVpcResponse.vpc.vpcId");
.path("CreateSubnetResponse.subnet.subnetId");
String customSubnetId = given()
given()
.formParam("Action", "CreateLoadBalancer")
.formParam("Name", "duplicate-az-lb")
.formParam("Type", "application")
.formParam("Subnets.member.1", "subnet-default-a")
.formParam("Subnets.member.2", secondSubnetId)
.header("Authorization", AUTH)
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("InvalidConfigurationRequest"));
}
@Test
@Order(14)
void setSubnetsWithSubnetsInSameAvailabilityZoneThrowsInvalidConfigurationRequest() {
String secondSubnetId = given()
.formParam("Action", "CreateSubnet")
.formParam("VpcId", customVpcId)
.formParam("CidrBlock", "10.2.1.0/24")
.formParam("AvailabilityZone", "us-east-1c")
.formParam("VpcId", "vpc-default")
.formParam("CidrBlock", "172.31.65.0/24")
.formParam("AvailabilityZone", "us-east-1a")
.header("Authorization", EC2_AUTH)
.when()
.post("/")
@@ -345,7 +381,7 @@ class ElbV2IntegrationTest {
String subnetlessLbArn = given()
.formParam("Action", "CreateLoadBalancer")
.formParam("Name", "subnetless-lb")
.formParam("Name", "set-duplicate-az-lb")
.formParam("Type", "application")
.header("Authorization", AUTH)
.when()
@@ -358,7 +394,73 @@ class ElbV2IntegrationTest {
given()
.formParam("Action", "SetSubnets")
.formParam("LoadBalancerArn", subnetlessLbArn)
.formParam("Subnets.member.1", customSubnetId)
.formParam("Subnets.member.1", "subnet-default-a")
.formParam("Subnets.member.2", secondSubnetId)
.header("Authorization", AUTH)
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("InvalidConfigurationRequest"));
}
@Test
@Order(15)
void setSubnetsAfterSubnetlessCreateCanAdoptCustomVpc() {
String customVpcId = given()
.formParam("Action", "CreateVpc")
.formParam("CidrBlock", "10.2.0.0/16")
.header("Authorization", EC2_AUTH)
.when()
.post("/")
.then()
.statusCode(200)
.extract()
.path("CreateVpcResponse.vpc.vpcId");
String customSubnetAId = given()
.formParam("Action", "CreateSubnet")
.formParam("VpcId", customVpcId)
.formParam("CidrBlock", "10.2.1.0/24")
.formParam("AvailabilityZone", "us-east-1b")
.header("Authorization", EC2_AUTH)
.when()
.post("/")
.then()
.statusCode(200)
.extract()
.path("CreateSubnetResponse.subnet.subnetId");
String customSubnetBId = given()
.formParam("Action", "CreateSubnet")
.formParam("VpcId", customVpcId)
.formParam("CidrBlock", "10.2.2.0/24")
.formParam("AvailabilityZone", "us-east-1c")
.header("Authorization", EC2_AUTH)
.when()
.post("/")
.then()
.statusCode(200)
.extract()
.path("CreateSubnetResponse.subnet.subnetId");
String subnetlessLbArn = given()
.formParam("Action", "CreateLoadBalancer")
.formParam("Name", "subnetless-adopt-vpc-lb")
.formParam("Type", "application")
.header("Authorization", AUTH)
.when()
.post("/")
.then()
.statusCode(200)
.extract()
.path("CreateLoadBalancerResponse.CreateLoadBalancerResult.LoadBalancers.member.LoadBalancerArn");
given()
.formParam("Action", "SetSubnets")
.formParam("LoadBalancerArn", subnetlessLbArn)
.formParam("Subnets.member.1", customSubnetAId)
.formParam("Subnets.member.2", customSubnetBId)
.header("Authorization", AUTH)
.when()
.post("/")
@@ -375,12 +477,12 @@ class ElbV2IntegrationTest {
.statusCode(200)
.body("DescribeLoadBalancersResponse.DescribeLoadBalancersResult.LoadBalancers.member.VpcId",
equalTo(customVpcId))
.body("DescribeLoadBalancersResponse.DescribeLoadBalancersResult.LoadBalancers.member.AvailabilityZones.member.SubnetId",
equalTo(customSubnetId));
.body("DescribeLoadBalancersResponse.DescribeLoadBalancersResult.LoadBalancers.member.AvailabilityZones.member.size()",
equalTo(2));
}
@Test
@Order(13)
@Order(16)
void createLoadBalancerWithMissingSubnetThrowsSubnetNotFound() {
given()
.formParam("Action", "CreateLoadBalancer")
@@ -398,7 +500,7 @@ class ElbV2IntegrationTest {
// ── Target Groups ─────────────────────────────────────────────────────────
@Test
@Order(14)
@Order(20)
void createTargetGroup() {
tgArn = given()
.formParam("Action", "CreateTargetGroup")
@@ -429,7 +531,7 @@ class ElbV2IntegrationTest {
}
@Test
@Order(15)
@Order(21)
void describeTargetGroups() {
given()
.formParam("Action", "DescribeTargetGroups")
@@ -444,7 +546,7 @@ class ElbV2IntegrationTest {
}
@Test
@Order(16)
@Order(22)
void duplicateTargetGroupNameThrows() {
given()
.formParam("Action", "CreateTargetGroup")
@@ -458,7 +560,7 @@ class ElbV2IntegrationTest {
}
@Test
@Order(17)
@Order(23)
void modifyTargetGroupAttributes() {
given()
.formParam("Action", "ModifyTargetGroupAttributes")
@@ -475,7 +577,7 @@ class ElbV2IntegrationTest {
}
@Test
@Order(18)
@Order(24)
void describeTargetGroupsByMissingNameThrows() {
given()
.formParam("Action", "DescribeTargetGroups")
@@ -489,7 +591,7 @@ class ElbV2IntegrationTest {
}
@Test
@Order(19)
@Order(25)
void describeTargetGroupsByMissingArnThrows() {
given()
.formParam("Action", "DescribeTargetGroups")
@@ -506,7 +608,7 @@ class ElbV2IntegrationTest {
// ── Targets ───────────────────────────────────────────────────────────────
@Test
@Order(20)
@Order(26)
void registerTargets() {
given()
.formParam("Action", "RegisterTargets")
@@ -523,7 +625,7 @@ class ElbV2IntegrationTest {
}
@Test
@Order(21)
@Order(27)
void describeTargetHealthReturnsInitial() {
given()
.formParam("Action", "DescribeTargetHealth")
@@ -927,7 +1029,7 @@ class ElbV2IntegrationTest {
}
@Test
@Order(73)
@Order(74)
void createLoadBalancerWithMissingSubnetReturnsSubnetNotFound() {
given()
.formParam("Action", "CreateLoadBalancer")
@@ -943,7 +1045,7 @@ class ElbV2IntegrationTest {
}
@Test
@Order(74)
@Order(75)
void setSubnetsWithMissingSubnetReturnsSubnetNotFound() {
String setSubnetsLbArn = given()
.formParam("Action", "CreateLoadBalancer")