Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

Delete Routing Tables Boto3

  • 24-03-2018 09:11PM
    #1
    Registered Users, Registered Users 2 Posts: 1,699 ✭✭✭


    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, Registered Users 2 Posts: 1,699 ✭✭✭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,282 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