#!/usr/bin/env python3 import json import sys subnets = [ { "az": subnet.get("AvailabilityZone", ""), "id": subnet.get("SubnetId", ""), "vpc": subnet.get("VpcId", ""), } for subnet in json.load(sys.stdin).get("Subnets", []) if subnet.get("MapPublicIpOnLaunch") ] subnets.sort(key=lambda subnet: (subnet["az"], subnet["vpc"], subnet["id"])) for candidate in subnets: if candidate["id"] and candidate["az"] and candidate["vpc"] and any( sibling["id"] and sibling["vpc"] == candidate["vpc"] and sibling["az"] and sibling["az"] != candidate["az"] for sibling in subnets ): print(candidate["id"]) break else: sys.exit("no available public subnet belongs to a VPC with public subnets in two availability zones")