Ruby 如何确定AWS VPC子网是否为;“公共子网”;?

Ruby 如何确定AWS VPC子网是否为;“公共子网”;?,ruby,amazon-web-services,amazon-vpc,Ruby,Amazon Web Services,Amazon Vpc,我想知道给定的AWS VPC子网是否是网络,即是否可以直接访问internet。据我所知,为此,我需要检查与该子网关联的路由表是否与internet网关关联,即它有一个带有“destination\u cidr\u block”=“0.0.0.0/0”和“gateway\u id”=“igw blahblah”的条目。因此,使用Ruby AWS SDK,我做了如下工作: def is_public_subnet(subnet_id) client = AWS::EC2::Client.n

我想知道给定的AWS VPC子网是否是网络,即是否可以直接访问internet。据我所知,为此,我需要检查与该子网关联的路由表是否与internet网关关联,即它有一个带有
“destination\u cidr\u block”=“0.0.0.0/0”
“gateway\u id”=“igw blahblah”
的条目。因此,使用Ruby AWS SDK,我做了如下工作:

def is_public_subnet(subnet_id)
    client = AWS::EC2::Client.new
    rt = client.describe_route_tables(:filters => [:name => "association.subnet-id", :values => [subnet_id]])
    if rt[:route_table_set].empty?
        # no route table associated with it. Must be using main route table.
        rt = client.describe_route_tables(:filters => [:name => "association.main", :values => ["true"]])
    end
    rt[:route_table_set][0][:route_set].each do |route|
        if route[:destination_cidr_block] == "0.0.0.0/0"
            if route[:gateway_id].start_with?("igw-")
                return true
            end
         end
    end
    return false
end

我想问,在方法和ruby sdk代码方面,是否有更好的方法来实现这一点。

我不能说ruby代码是有效的,但方法是有效的:如果子网路由到internet网关,则子网是公共的,如果子网连接到弹性IP,则可以访问它的实例