Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Delete Routing Tables Boto3

Options
  • 24-03-2018 10:11pm
    #1
    Registered Users Posts: 1,704 ✭✭✭


    Hey so what im trying to do is destroy everything to do with my VPC, I get this error
    botocore.exceptions.ClientError: An error occurred (DependencyViolation) when calling the DeleteVpc operation: The vpc 'vpc-fb30b29d' has dependencies and cannot be deleted.
    

    I'm pretty sure this error is because my routing tables are not being destroyed, the code is:
    for rt in vpc.route_tables.all():
            for rta in rt.associations:
                if not rta.main:
                    rta.delete()
        print('Routing Tables Done')
    
    But I really dislike this version since its so blunt, I want to delete by associated VPC. All my tables have the VPC Tag populated vpc-fb30b29d | JtR-Cloud so I would like to specify "VPC_ID + | JtR-Cloud" and loop until they are gone. But I'm lost for how the hell to do this, anyone any ideas?


Comments

  • Registered Users Posts: 1,704 ✭✭✭Doylers


    Answering my own question:

    Order does matter, delete in this order:
    All instances to remove public IP's
    Detach gateway
    Subnets
    Route table
    Endpoints
    Security Groups
    Network Interfaces
    Vpc itself

    Code to delete route tables and any associations below:
    rtl = vpc.route_tables.all()
        count = sum(1 for _ in rtl)
        while count > 1:
    
         #delete all route table associations
            for rt in rtl:
                for rta in rt.associations:
                    if not rta.main:
                        print("Deleting route table associations")
                        rta.delete()
    
                for r in rt.routes:
                    try:
                        x = r.delete()
                       #  print(x)
                    except:
                        pass
                try:
                    rt.delete()
                except:
                    pass
                     
            rtl = vpc.route_tables.all()
            count = sum(1 for _ in rtl)
    


  • Moderators, Computer Games Moderators Posts: 4,281 Mod ✭✭✭✭deconduo


    I'd recommend looking into either Terraform or Cloudformation to manage AWS resources. It makes life a lot easier compared to using the SDKs - unless you have a specific reason to use Boto.


Advertisement