import boto3 import logging import os REGION = os.environ["AWS_REGION"] if "AWS_REGION" in os.environ else "eu-west-1" ec2 = boto3.client("ec2", region_name=REGION) def get_regions(): return [x["RegionName"] for x in ec2.describe_regions()["Regions"]] def disable_map_public_ip_on_subnets(region): rc = boto3.client("ec2", region_name=region) subnets = rc.describe_subnets()["Subnets"] for subnet in subnets: if subnet["MapPublicIpOnLaunch"] == True: res = rc.modify_subnet_attribute( SubnetId=subnet["SubnetId"], MapPublicIpOnLaunch={"Value": False} ) print(res) for region in get_regions(): disable_map_public_ip_on_subnets(region)