Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
fix(ec2): use real AZ ids, not a copy of the zone name (#1255)
Maximilian Bandle87a80d5eb547
diff --git a/ministack/services/ec2.py b/ministack/services/ec2.pyindex 2b561162..d2ed0431 100644--- a/ministack/services/ec2.py+++ b/ministack/services/ec2.py@@ -2341,14 +2341,37 @@ def _describe_vpc_endpoint_services(p):# Availability Zones# ---------------------------------------------------------------------------+_AZ_ID_DIRECTIONS = {+ "north": "n", "south": "s", "east": "e", "west": "w", "central": "c",+ "northeast": "ne", "northwest": "nw", "southeast": "se", "southwest": "sw",+}+++def _az_id_prefix(region):+ """Region -> the AZ-id prefix AWS codes it with: eu-central-1 -> euc1, ap-southeast-2 -> apse2."""++ parts = region.split("-")+ if len(parts) < 3:+ return region.replace("-", "")+ geo, middles, index = parts[0], parts[1:-1], parts[-1]+ coded = "".join(_AZ_ID_DIRECTIONS.get(part, part[:1]) for part in middles)+ return f"{geo}{coded}{index}"++def _describe_availability_zones(p):- azs = [f"{get_region()}a", f"{get_region()}b", f"{get_region()}c"]+ """AZ ids are deliberately unlike the zone names: AWS shuffles names per account, so AZa is different,+ while az1 is the same across accounts. The shuffle on AWS is per-account stable."""++ region = get_region()+ prefix = _az_id_prefix(region)+ # 3 AZs with a=1, b=2, c=3 for ministack+ zones = [(f"{region}{letter}", f"{prefix}-az{n}") for n, letter in enumerate("abc", start=1)]items = "".join(f"""<item>- <zoneName>{az}</zoneName>+ <zoneName>{name}</zoneName><zoneState>available</zoneState>- <regionName>{get_region()}</regionName>- <zoneId>{az}</zoneId>- </item>""" for az in azs)+ <regionName>{region}</regionName>+ <zoneId>{zone_id}</zoneId>+ </item>""" for name, zone_id in zones)return _xml(200, "DescribeAvailabilityZonesResponse",f"<availabilityZoneInfo>{items}</availabilityZoneInfo>")diff --git a/tests/test_ec2.py b/tests/test_ec2.pyindex 13f007b4..d6119a13 100644--- a/tests/test_ec2.py+++ b/tests/test_ec2.py@@ -86,6 +86,29 @@ def test_ec2_describe_availability_zones(ec2):assert any("us-east-1" in az for az in azs)+def test_ec2_availability_zone_ids_are_region_coded(ec2):+ """ZoneId is the account-stable id ("use1-az1"), never the account-local ZoneName: code+ handed an id has to resolve it here, and an id that equals the name hides that requirement+ until it runs against real AWS."""+ zones = ec2.describe_availability_zones()["AvailabilityZones"]+ assert [z["ZoneId"] for z in zones] == ["use1-az1", "use1-az2", "use1-az3"]+ assert all(z["ZoneId"] != z["ZoneName"] for z in zones)+ assert [z["ZoneName"] for z in zones] == ["us-east-1a", "us-east-1b", "us-east-1c"]+ assert all(z["RegionName"] == "us-east-1" and z["State"] == "available" for z in zones)++ # The prefix follows AWS's own coding, including the compound directions.+ from ministack.services.ec2 import _az_id_prefix+ assert _az_id_prefix("eu-central-1") == "euc1"+ assert _az_id_prefix("ap-southeast-2") == "apse2"+ assert _az_id_prefix("ap-northeast-1") == "apne1"+ assert _az_id_prefix("us-west-2") == "usw2"+ assert _az_id_prefix("ca-central-1") == "cac1"+ assert _az_id_prefix("sa-east-1") == "sae1"+ # A partitioned region keeps every part between the geography and the index.+ assert _az_id_prefix("us-gov-west-1") == "usgw1"+ assert _az_id_prefix("cn-northwest-1") == "cnnw1"++def test_ec2_describe_regions_returns_commercial_regions(ec2):"""DescribeRegions must list at least the four legacy us-* regionswith opt-in-not-required, and emit the shape AWS returns."""