Created
December 31, 2023 08:11
-
-
Save schlarpc/32413dba4f2a55f39e07b1010eb03cf7 to your computer and use it in GitHub Desktop.
boto3 enable all local zones
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
ec2 = boto3.client("ec2", region_name="us-east-1") | |
for region in ec2.describe_regions()["Regions"]: | |
regional_ec2 = boto3.client("ec2", region_name=region["RegionName"]) | |
done = set() | |
for zone in regional_ec2.describe_availability_zones(AllAvailabilityZones=True)["AvailabilityZones"]: | |
if zone["OptInStatus"] == "opted-in": | |
response = regional_ec2.get_paginator("describe_spot_price_history").paginate( | |
AvailabilityZone=zone["ZoneName"], | |
ProductDescriptions=["LINUX/UNIX", "Linux/UNIX (Amazon VPC)"], | |
) | |
for item in response.search("SpotPriceHistory"): | |
print(item["AvailabilityZone"], item["InstanceType"], item["SpotPrice"]) | |
if not (zone["State"] == "available" and zone["OptInStatus"] == "not-opted-in"): | |
continue | |
if zone["GroupName"] in done: | |
continue | |
try: | |
response = regional_ec2.modify_availability_zone_group( | |
GroupName=zone["GroupName"], | |
OptInStatus="opted-in", | |
) | |
except Exception as ex: | |
if not ex.response.get("Error", {}).get("Code") == "InvalidAZGroup.NotFound": | |
raise | |
done.add(zone["GroupName"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment