fix(elbv2): support explicit ALB zone-rule overrides (#8533)

* fix(elbv2): apply regional ALB zone defaults with overrides
* fix(elbv2): restore legacy subnet snapshots during stack rollback
* fix(elbv2): require two ALB zones for the AWS default
* fix(elbv2): simplify explicit ALB zone rules
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-11-16 07:42:42 -04:00
1 parent bd3431283b
commit 4a5d5fbb49
12 files changed
+369 -23

No files matched your search

@@ -3890,6 +3890,18 @@ public interface EmulatorConfig {
/** Default idle-pool timeout in seconds; zero disables expiry. Target Keep-Alive may override it. */
@WithDefault("60")
int backendKeepAliveTimeoutSeconds();
/** AWS defaults unless explicitly overridden for local deployments. */
@WithDefault("aws")
AlbZoneRule albZoneRule();
/** A region entry takes precedence over the global rule, including an explicit AWS reset. */
Map<String, AlbZoneRule> albZoneRuleByRegion();
}
enum AlbZoneRule {
AWS,
ALLOW_SINGLE_ZONE
}
interface EksServiceConfig {
@@ -269,7 +269,7 @@ public class ElbV2CfnProvisioner implements CfnResourceProvisioner {
}
String arn = resource.getPhysicalId();
List<String> priorSubnets = prior.isEmpty() ? List.of() : List.of(prior.split(","));
elbV2Service.setSubnets(AwsArnUtils.parse(arn).region(), arn, priorSubnets);
elbV2Service.restoreSubnetSnapshot(AwsArnUtils.parse(arn).region(), arn, priorSubnets);
recordUsedSubnets(resource, priorSubnets);
resource.getAttributes().remove(CfnRollback.LOAD_BALANCER_SUBNETS_UPDATE_SNAPSHOT_ATTR);
return true;
@@ -303,7 +303,7 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc
String id = randomHex16();
String arn = AwsArnUtils.Arn.of("elasticloadbalancing", region, regionResolver.getAccountId(), "loadbalancer/" + typePrefix + "/" + name + "/" + id).toString();
String dnsName = name + "-" + id + ".elb." + loadBalancerDnsSuffix();
requireSubnets(lbType, subnets);
requireSubnets(region, lbType, subnets);
String vpcId = resolveSubnetVpcId(region, lbType, subnets);
LoadBalancer lb = new LoadBalancer();
@@ -332,7 +332,7 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc
/**
* Every load balancer type takes {@code Subnets} or {@code SubnetMappings}, and an Application Load
* Balancer takes at least two, so a CreateLoadBalancer with fewer is refused before anything is
* Balancer takes at least two by default, so a CreateLoadBalancer with fewer is refused before anything is
* created, with the message AWS gives an Application or a Network Load Balancer. A Gateway Load
* Balancer, which takes subnets the way a Network Load Balancer does, gets the Network one. AWS
* counts before it looks a subnet up, so one subnet that does not exist gets this error too, not
@@ -342,11 +342,11 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc
* a SetSubnets that names none, and {@link #setSubnets} on a list that names some, once it has the
* balancer's type; whether the subnets span two Availability Zones is checked after they are looked
* up. No public report shows what AWS answers a SetSubnets with one subnet, so it gets the create
* message and order.
* message and order. Explicit global or regional single-zone overrides reduce the minimum to one.
*/
private static void requireSubnets(String lbType, List<String> subnets) {
private void requireSubnets(String region, String lbType, List<String> subnets) {
int count = subnets == null ? 0 : subnets.size();
if ("application".equals(lbType) && count < 2) {
if ("application".equals(lbType) && requiresTwoAlbZones(region) && count < 2) {
throw new AwsException("ValidationError",
"At least two subnets in two different Availability Zones must be specified", 400);
}
@@ -355,6 +355,24 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc
}
}
private boolean requiresTwoAlbZones(String region) {
EmulatorConfig.AlbZoneRule rule = EmulatorConfig.AlbZoneRule.AWS;
if (config != null) {
EmulatorConfig.ElbV2ServiceConfig settings = config.services().elbv2();
if (settings.albZoneRule() != null) {
rule = settings.albZoneRule();
}
Map<String, EmulatorConfig.AlbZoneRule> regions = settings.albZoneRuleByRegion();
if (regions != null) {
rule = regions.getOrDefault(region, rule);
}
}
return switch (rule) {
case AWS -> true;
case ALLOW_SINGLE_ZONE -> false;
};
}
/**
* A subnet is in use while a load balancer is configured with it. Account-scoped through the
* request-scoped storage, and a target group does not count: it pins nothing in AWS either.
@@ -489,7 +507,7 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc
void setSubnetsFromRequest(String region, String arn, List<String> subnets) {
LoadBalancer lb = requireLoadBalancer(region, arn);
if (subnets.isEmpty()) {
requireSubnets(lb.getType(), subnets);
requireSubnets(region, lb.getType(), subnets);
}
setSubnets(region, arn, subnets);
}
@@ -500,13 +518,22 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc
* moves a balancer an earlier Floci version saved without subnets back onto none.
*/
public void setSubnets(String region, String arn, List<String> subnets) {
setSubnets(region, arn, subnets, false);
}
/** Restores CloudFormation's saved subnet snapshot, including legacy duplicate-zone assignments. */
public void restoreSubnetSnapshot(String region, String arn, List<String> subnets) {
setSubnets(region, arn, subnets, true);
}
private void setSubnets(String region, String arn, List<String> subnets, boolean restoreSnapshot) {
ec2Service.runWithSubnetReferencesHeld(region, () -> {
LoadBalancer lb = requireLoadBalancer(region, arn);
// Counted as on create, before any subnet is looked up; an empty list is taken, as above.
if (subnets != null && !subnets.isEmpty()) {
requireSubnets(lb.getType(), subnets);
requireSubnets(region, lb.getType(), subnets);
}
String vpcId = resolveSubnetVpcId(region, lb.getType(), subnets);
String vpcId = resolveSubnetVpcId(region, lb.getType(), subnets, restoreSnapshot);
if (vpcId != null && lb.getVpcId() != null && !lb.getVpcId().equals(vpcId)) {
throw new AwsException("InvalidConfigurationRequest",
"All subnets must belong to the load balancer VPC.", 400);
@@ -1724,6 +1751,11 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc
}
private String resolveSubnetVpcId(String region, String lbType, List<String> subnetIds) {
return resolveSubnetVpcId(region, lbType, subnetIds, false);
}
private String resolveSubnetVpcId(String region, String lbType, List<String> subnetIds,
boolean restoreSnapshot) {
if (subnetIds == null || subnetIds.isEmpty()) {
return null;
}
@@ -1736,10 +1768,14 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc
.map(Subnet::getAvailabilityZone)
.distinct()
.count();
if (distinctAvailabilityZones < 2) {
if (requiresTwoAlbZones(region) && distinctAvailabilityZones < 2) {
throw new AwsException("InvalidConfigurationRequest",
"Application Load Balancers must be attached to subnets in at least two Availability Zones.", 400);
}
if (!restoreSnapshot && distinctAvailabilityZones < subnetIds.size()) {
throw new AwsException("InvalidConfigurationRequest",
"A load balancer cannot be attached to multiple subnets in the same Availability Zone", 400);
}
}
Set<String> vpcIds = subnetIds.stream()