import boto3 import botocore.exceptions GP3_MIN_IOPS = 3000 GP3_MIN_THROUGHPUT = 125 GP3_MAX_THROUGHPUT = 250 if __name__ == "__main__": region = "us-west-2" client = boto3.client("ec2", region_name=region) dry_run = True response = client.describe_volumes() for result in response["Volumes"]: if result["VolumeType"] == "gp2": VolumeId = result["VolumeId"] Iops = result["Iops"] if result["Iops"] >= GP3_MIN_IOPS else GP3_MIN_IOPS CalculatedThroughput = int(result["Iops"] * result["Size"] / 1024) if CalculatedThroughput >= GP3_MAX_THROUGHPUT: Throughput = GP3_MAX_THROUGHPUT elif CalculatedThroughput < GP3_MIN_THROUGHPUT: Throughput = GP3_MIN_THROUGHPUT else: Throughput = CalculatedThroughput report = ( f"{result['VolumeId']} gp2: {result['Size']}gb {result['Iops']}" + f" {CalculatedThroughput} {Throughput} ||| " + f"gp3: {result['Size']}gb {Iops} {Throughput}" ) print(report) try: modify = client.modify_volume( VolumeId=result["VolumeId"], VolumeType="gp3", Iops=Iops, Throughput=Throughput, DryRun=dry_run, ) except botocore.exceptions.ClientError as error: if error.response["Error"]["Code"] == "DryRunOperation": print("DryRun operation") else: raise error