使用 boto3 通过子网 ID 查找 route_table_id

Jam*_*mes 5 python amazon-ec2 boto3

我试图弄清楚如何使用 boto3 根据子网 ID(在 vpc 中)查找 route_table_id。boto3(v1.3.1)、ec2.RouteTableAssociation(id)中有api。使用正确的route_table_association_id,尝试访问资源的属性route_table_id,它给出如下异常:

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
Run Code Online (Sandbox Code Playgroud)

我正在寻找为什么会失败的线索。

詹姆士

Jam*_*mes 7

想办法这样做:

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


print(response['RouteTables'][0]['Associations'][0]['RouteTableId'])
Run Code Online (Sandbox Code Playgroud)