fix(ec2): place a subnet in the zone its request names, not always az1 (#2890)

* fix(ec2): place a subnet in the zone its request names, not always az1

CreateSubnet never read AvailabilityZoneId, and hardcoded every new subnet's
zone id to the region's first zone. Two consequences, and the second bites even
a caller that never mentions a zone id: a subnet asked for us-east-1-az2 came
back in us-east-1-az1, and a subnet placed correctly by zone *name* still
reported az1, contradicting its own availabilityZone.

Anything that selects subnets by zone id therefore saw one zone -- Terraform's
aws_subnet.availability_zone_id, and any client that pairs subnets against the
DescribeAvailabilityZones list it just read.

AvailabilityZoneId is now honoured, and the zone id is derived from the zone
rather than fixed. The derivation is the mapping DescribeAvailabilityZones and
the seeded default subnets were already using independently; all three now come
through one helper, so a subnet's zone id agrees with the zone list by
construction instead of by coincidence.

A zone and a zone id that disagree are refused with InvalidParameterCombination
rather than silently resolved to one of them -- Terraform's aws_subnet forbids
setting both, so a request carrying both contradictory values is a bug in the
caller, not something to guess at. A malformed zone id is refused too.

* fix(ec2): refuse a zone id this region does not publish

The first version resolved any well-formed zone id, so us-east-1-az4 created a
subnet in a zone DescribeAvailabilityZones does not list. That is the same
inconsistency the hardcoded az1 produced, only quieter: a client pairing subnets
against the published zone inventory still cannot resolve the subnet's zone.

The three modelled zones are now one constant that DescribeAvailabilityZones,
the seeded default subnets and this validation all read, so they cannot drift,
and the error names the range rather than only rejecting.

Also logs the tolerated NumberFormatException instead of swallowing it, and
drops this branch's em-dashes, both per AGENTS.md.
This commit is contained in:
Allen Sanborn authored and GitHub committed 2026-09-01 12:13:34 -04:00
1 parent 2a637d3e05
commit c32f8b5a24
3 files changed
+266 -7

No files matched your search

@@ -2291,7 +2291,8 @@ public class Ec2QueryHandler {
String vpcId = p.getFirst("VpcId");
String cidrBlock = p.getFirst("CidrBlock");
String az = p.getFirst("AvailabilityZone");
Subnet subnet = service.createSubnet(region, vpcId, cidrBlock, az);
String azId = p.getFirst("AvailabilityZoneId");
Subnet subnet = service.createSubnet(region, vpcId, cidrBlock, az, azId);
applyResourceTags(p, region, "subnet", subnet.getSubnetId());
XmlBuilder xml = new XmlBuilder()
.start("CreateSubnetResponse", AwsNamespaces.EC2)
@@ -109,6 +109,13 @@ import jakarta.inject.Inject;
public class Ec2Service implements ContainerTeardown, ResourceProvider {
private static final Logger LOG = Logger.getLogger(Ec2Service.class);
/**
* The availability zones every region is modelled with. DescribeAvailabilityZones publishes
* exactly these, the seeded default subnets sit in them, and CreateSubnet refuses a zone id
* outside them, so the three cannot drift apart.
*/
private static final String[] MODELLED_ZONE_SUFFIXES = {"a", "b", "c"};
private static final DateTimeFormatter ISO_FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
.withZone(ZoneOffset.UTC);
private static final int DEFAULT_ROOT_VOLUME_SIZE_GIB = 8;
@@ -428,7 +435,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
vpcs.put(key(region, vpcId), defaultVpc);
// Default subnets (a/b/c)
String[] azSuffixes = {"a", "b", "c"};
String[] azSuffixes = MODELLED_ZONE_SUFFIXES;
String[] cidrBlocks = {"172.31.0.0/20", "172.31.16.0/20", "172.31.32.0/20"};
String[] subnetIds = {
defaultSubnetId(region, azSuffixes[0]),
@@ -441,7 +448,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
subnet.setCidrBlock(cidrBlocks[i]);
subnet.setState("available");
subnet.setAvailabilityZone(region + azSuffixes[i]);
subnet.setAvailabilityZoneId(region + "-az" + (i + 1));
subnet.setAvailabilityZoneId(zoneIdForZoneName(region, region + azSuffixes[i]));
subnet.setAvailableIpAddressCount(4091);
subnet.setDefaultForAz(true);
subnet.setMapPublicIpOnLaunch(true);
@@ -3020,21 +3027,91 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
// ─── Subnets ───────────────────────────────────────────────────────────────
/**
* The zone id Floci publishes for a zone name. AWS's real zone ids are opaque and per-account
* ({@code use1-az4}); Floci derives a deterministic one instead, and what matters is that
* every surface derives it the same way, DescribeAvailabilityZones, the seeded default
* subnets and CreateSubnet all come through here, so a subnet's zone id agrees with the zone
* list a client just read. A zone name that is not this region's {@code <region><letter>}
* keeps the first zone's id, which is what an unrecognised name resolved to before.
*/
static String zoneIdForZoneName(String region, String zoneName) {
if (zoneName != null && zoneName.length() == region.length() + 1 && zoneName.startsWith(region)) {
char letter = Character.toLowerCase(zoneName.charAt(zoneName.length() - 1));
if (letter >= 'a' && letter <= 'z') {
return region + "-az" + (letter - 'a' + 1);
}
}
return region + "-az1";
}
/**
* Resolves the zone a new subnet lands in from whichever of AvailabilityZone and
* AvailabilityZoneId the caller supplied. Terraform's aws_subnet exposes both
* ({@code availability_zone} and {@code availability_zone_id}) and forbids setting both at
* once, so in practice exactly one arrives; a pair that disagrees is refused rather than
* silently resolved to one of them.
*/
private String resolveSubnetZoneName(String region, String availabilityZone, String availabilityZoneId) {
if (!isSet(availabilityZoneId)) {
return isSet(availabilityZone) ? availabilityZone : region + "a";
}
String fromId = zoneNameForZoneId(region, availabilityZoneId);
if (isSet(availabilityZone) && !availabilityZone.equals(fromId)) {
throw new AwsException("InvalidParameterCombination",
"The availability zone '" + availabilityZone + "' does not match the availability zone ID '"
+ availabilityZoneId + "'", 400);
}
return fromId;
}
/**
* The inverse of {@link #zoneIdForZoneName}. An id outside the zones this region publishes is
* refused rather than resolved: placing a subnet in a zone DescribeAvailabilityZones does not
* list would leave a client unable to pair the two, which is the same inconsistency the
* hardcoded az1 produced, only quieter.
*/
private static String zoneNameForZoneId(String region, String availabilityZoneId) {
String prefix = region + "-az";
if (availabilityZoneId.startsWith(prefix)) {
try {
int index = Integer.parseInt(availabilityZoneId.substring(prefix.length()));
if (index >= 1 && index <= MODELLED_ZONE_SUFFIXES.length) {
return region + MODELLED_ZONE_SUFFIXES[index - 1];
}
} catch (NumberFormatException e) {
LOG.debugv("Availability zone ID {0} has a non-numeric zone index: {1}",
availabilityZoneId, e.getMessage());
}
}
throw new AwsException("InvalidParameterValue",
"Invalid availability zone ID: '" + availabilityZoneId + "'. This region publishes "
+ MODELLED_ZONE_SUFFIXES.length + " availability zones, " + prefix + "1 to "
+ prefix + MODELLED_ZONE_SUFFIXES.length + ".", 400);
}
public Subnet createSubnet(String region, String vpcId, String cidrBlock, String availabilityZone) {
return createSubnet(region, vpcId, cidrBlock, availabilityZone, null);
}
public Subnet createSubnet(String region, String vpcId, String cidrBlock, String availabilityZone,
String availabilityZoneId) {
if (vpcId == null || vpcId.isBlank()) {
throw new AwsException("MissingParameter", "The request must contain the parameter VpcId", 400);
}
ensureDefaultResources(region);
getRequiredVpc(region, vpcId);
String zoneName = resolveSubnetZoneName(region, availabilityZone, availabilityZoneId);
String subnetId = "subnet-" + randomHex(8);
Subnet subnet = new Subnet();
subnet.setSubnetId(subnetId);
subnet.setVpcId(vpcId);
subnet.setCidrBlock(cidrBlock);
subnet.setState("available");
subnet.setAvailabilityZone(availabilityZone != null ? availabilityZone : region + "a");
subnet.setAvailabilityZoneId(region + "-az1");
subnet.setAvailabilityZone(zoneName);
subnet.setAvailabilityZoneId(zoneIdForZoneName(region, zoneName));
subnet.setAvailableIpAddressCount(251);
subnet.setOwnerId(accountId);
subnet.setRegion(region);
@@ -5387,13 +5464,13 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
public List<Map<String, String>> describeAvailabilityZones(String region) {
List<Map<String, String>> zones = new ArrayList<>();
String[] azSuffixes = {"a", "b", "c"};
String[] azSuffixes = MODELLED_ZONE_SUFFIXES;
for (String suffix : azSuffixes) {
Map<String, String> az = new LinkedHashMap<>();
az.put("zoneName", region + suffix);
az.put("state", "available");
az.put("regionName", region);
az.put("zoneId", region + "-az" + (suffix.charAt(0) - 'a' + 1));
az.put("zoneId", zoneIdForZoneName(region, region + suffix));
az.put("zoneType", "availability-zone");
zones.add(az);
}