繁体   English   中英

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

[英]How to find out if an AWS VPC Subnet is a “public subnet”?

我想确定给定的AWS VPC子网是否是“公共子网”,即它是否可以直接访问互联网。 据我了解,为此,我需要检查与该子网关联的路由表是否与Internet网关关联,即它是否具有"destination_cidr_block"="0.0.0.0/0""gateway_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网关,则该子网是公共的;如果将实例连接到Elastic IP,它的实例将可以访问。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM