1

特定の AWS VPC サブネットが「パブリック サブネット」であるかどうか、つまり、インターネットに直接アクセスできるかどうかを確認したいと考えています。私が理解しているように、これには、そのサブネットに関連付けられたルートテーブルがインターネットゲートウェイに関連付けられているかどうかを確認する必要があります。つまり、とのエントリが"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コードの両方の観点から、これを行うためのより良い方法があるかどうかを尋ねたい.

4

1 に答える 1

1

ルビーコードについては言えませんが、アプローチは有効です。インターネットゲートウェイにルーティングする場合、サブネットはパブリックであり、Elastic IP にアタッチされている場合、そのインスタンスに到達できます。

于 2014-05-23T11:31:23.857 に答える