Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find route_table_id by subnet_id using boto3

I'm trying to figure out how to find route_table_id based upon a subnet_id(in a vpc) using boto3. There is api in boto3(v1.3.1), ec2.RouteTableAssociation(id). With correct route_table_association_id, try to access the resource's attribute route_table_id, it gives exception as below:

In [19]: rta.route_table_id
---------------------------------------------------------------------------
ResourceLoadException                     Traceback (most recent call last)
<ipython-input-19-a7f51e66ef03> in <module>()
----> 1 rta.route_table_id

/usr/local/lib/python2.7/site-packages/boto3/resources/factory.pyc in property_loader(self)
    341                 else:
    342                     raise ResourceLoadException(
--> 343                         '{0} has no load method'.format(self.__class__.__name__))
    344
    345             return self.meta.data.get(name)

ResourceLoadException: ec2.RouteTableAssociation has no load method

I'm looking for a clue why this failed.

James

like image 252
James Avatar asked Jun 20 '16 23:06

James


People also ask

How do you cancel an instance on Boto3?

1 Answer. You can do this very easily using the boto3 library. all you need is to create a list of ids of all the ec2 instances you wish to terminate. Then using the filter method pass in that list of ids as a keyworded argument named InstanceIds and call the terminate method on the returned value and you are done.


2 Answers

figure out to do this way:

response = client.describe_route_tables(
    Filters=[
        {
            'Name': 'association.subnet-id',
            'Values': [
                source_subnet_id
            ]
        }
    ]
)


print(response['RouteTables'][0]['Associations'][0]['RouteTableId'])
like image 110
James Avatar answered Nov 15 '22 10:11

James


Try describe_route_tables.

All required info is under the responding JSON ['RouteTables'][*]["Associations"]

like image 38
mootmoot Avatar answered Nov 15 '22 09:11

mootmoot