ネットワークを構築したい
今日はこの間からずっと気になっていたVPCというのを使ってネットワークを構築していきたいと思っている。
まず、

グローバルIPが振られて、
イメージとしては外からアクセスできるWebサーバと、
AWSがどんなものかわからないからか、
VPCの作成
VPCはクラウド上で仮想のネットワーク環境を構築するためのサービスらしい。そして、
とりあえず、
$ aws ec2 create-vpc --cidr-block 172.16.0.0/16 { "Vpc": { "InstanceTenancy": "default", "State": "pending", "VpcId": "vpc-6ece0d0b", "CidrBlock": "172.16.0.0/16", "DhcpOptionsId": "dopt-c83421aa" } }
Stateがpendingということは、
$ aws ec2 describe-vpcs { "Vpcs": [ { "VpcId": "vpc-6ece0d0b", "InstanceTenancy": "default", "State": "available", "DhcpOptionsId": "dopt-c83421aa", "CidrBlock": "172.16.0.0/16", "IsDefault": false }, { -略- } ] }
State がavailableになっているのを確認した。

今はこのようなVPCができている状態だと思われる。
ただ、
Subnetの作成
当初の予定どおり、
$ aws ec2 create-subnet --vpc-id vpc-6ece0d0b --cidr-block 172.16.64.0/24 { "Subnet": { "VpcId": "vpc-6ece0d0b", "CidrBlock": "172.16.64.0/24", "State": "pending", "AvailabilityZone": "ap-northeast-1c", "SubnetId": "subnet-5b1cf402", "AvailableIpAddressCount": 251 } } $ aws ec2 create-subnet --vpc-id vpc-6ece0d0b --cidr-block 172.16.32.0/24 { "Subnet": { "VpcId": "vpc-6ece0d0b", "CidrBlock": "172.16.32.0/24", "State": "pending", "AvailabilityZone": "ap-northeast-1c", "SubnetId": "subnet-5d1cf404", "AvailableIpAddressCount": 251 } }
このようにvpcのIDとCIDRブロックを指定してSubnetを作成した。今回はグローバルにアクセスできるものとできないものとの2つのネットワークを作りたいから2つ作ったけど、

たぶん、
EC2を起動して疎通の確認をしたかったけれど、
おそらく、
そういえば、
せっかくなので、
Availability Zone
Subnetを作成する時に--availability-zone オプションを指定するとAvailability Zoneを指定してSubnetを作る事ができそうだ。マジでコマンドの補完便利すぎる。
さっき作ったやつはap-northeast-1cとなっていたので、
A client error (InvalidParameterValue) occurred when calling the CreateSubnet operation: Value (ap-northeast-1b) for parameter availabilityZone is invalid. Subnets can currently only be created in the following availability zones: ap-northeast-1c, ap-northeast-1a.
と出て、
$ aws ec2 create-subnet --vpc-id vpc-6ece0d0b --cidr-block 172.16.128.0/24 --availability-zone ap-northeast-1a { "Subnet": { "VpcId": "vpc-6ece0d0b", "CidrBlock": "172.16.128.0/24", "State": "pending", "AvailabilityZone": "ap-northeast-1a", "SubnetId": "subnet-ea0fd89d", "AvailableIpAddressCount": 251 } } $ aws ec2 create-subnet --vpc-id vpc-6ece0d0b --cidr-block 172.16.160.0/24 --availability-zone ap-northeast-1a { "Subnet": { "VpcId": "vpc-6ece0d0b", "CidrBlock": "172.16.160.0/24", "State": "pending", "AvailabilityZone": "ap-northeast-1a", "SubnetId": "subnet-d40fd8a3", "AvailableIpAddressCount": 251 } }
ap-northeast-1aにもSubnetを作ってみた。
今はきっとこの図のようになっているはずだ。

なっているはずだけれども、
ルーティング
いい加減にEC2にSSHで接続できないので、
そろそろ本気でEC2に接続できるように原因を考えていく。なんとなくだけど、
$ aws ec2 create-[TAB]
この時にcreate-internet-gatewayがあるのを見ていて、
Internet Gateway
というわけで、
$ aws ec2 create-internet-gateway { "InternetGateway": { "Tags": [], "InternetGatewayId": "igw-da14fdbf", "Attachments": [] } }
結構あっさりと作る事ができた。作るときにvpc-idを指定するのかと思ったけど、
$ aws ec2 help |grep internet o attach-internet-gateway o create-internet-gateway o delete-internet-gateway o describe-internet-gateways o detach-internet-gateway
ほう。attach-internet-gatewayというのがある。コレにvpc-idを指定するオプションがあれば、
$ aws ec2 attach-internet-gateway --vpc-id vpc-6ece0d0b --internet-gateway-id igw-da14fdbf
あった。vpc-idを指定する事ができた。これでVPCにinternet gatewayをアタッチすることができた。

ここまでできたところで、
Route Table
さっきなんとなくやったけど、
$ aws ec2 help |grep *****
これで目星をつけた名前で探すとなんとなくそれっぽいのを見つけられるってこと。
今回もルーティングについて何かないかなと探してみる。もう補完とこの方法でドキュメント見なくても、
$ aws ec2 help |grep route o associate-route-table o create-route o create-route-table o create-vpn-connection-route o delete-route o delete-route-table o delete-vpn-connection-route o describe-route-tables o disable-vgw-route-propagation o disassociate-route-table o enable-vgw-route-propagation o replace-route o replace-route-table-association
ほら、
$ aws ec2 describe-route-tables { "RouteTables": [ ?略? { "Associations": [ { "RouteTableAssociationId": "rtbassoc-fffa2e9a", "Main": true, "RouteTableId": "rtb-c33df4a6" } ], "RouteTableId": "rtb-c33df4a6", "VpcId": "vpc-6ece0d0b", "PropagatingVgws": [], "Tags": [], "Routes": [ { "GatewayId": "local", "DestinationCidrBlock": "172.16.0.0/16", "State": "active", "Origin": "CreateRouteTable" } ] } ] }
describe-route-tableで見てみると、
Route Tableがあるのであればそこに外に出て行くような設定をしてあげれば良いはず。たぶんだけれど、
補完があれば、
$ aws ec2 create-route --gateway-id igw-da14fdbf --route-table-id rtb-c33df4a6 --destination-cidr-block 0.0.0.0/0
やはりオプションがあったから、
$ aws ec2 describe-route-tables --route-table-ids rtb-c33df4a6 { "RouteTables": [ { "Associations": [ { "RouteTableAssociationId": "rtbassoc-fffa2e9a", "Main": true, "RouteTableId": "rtb-c33df4a6" } ], "RouteTableId": "rtb-c33df4a6", "VpcId": "vpc-6ece0d0b", "PropagatingVgws": [], "Tags": [], "Routes": [ { "GatewayId": "local", "DestinationCidrBlock": "172.16.0.0/16", "State": "active", "Origin": "CreateRouteTable" }, { "GatewayId": "igw-da14fdbf", "DestinationCidrBlock": "0.0.0.0/0", "State": "active", "Origin": "CreateRoute" } ] } ] }
指定した後のdescribe-route-tablesを見てみる。さっきとRoutesの部分が変わっているのでわかりやすい。たぶんこれで、
試してみると、
ネットワークに接続されたサーバにSSHで接続できることがこんなに嬉しいとは思わなかった。
しかしひとつ問題が発生した。ローカルでデータベースとかを置きたいなと思っていたネットワークにも直接SSHで接続できてしまっている。これじゃダメだ。グローバルのIPアドレスを与えなきゃいいのかもしれないけれど、
なんとかして、

このRouterのルーティングテーブルを書き換えれば、
とりあえず、
$ aws ec2 help |grep route
associate-route-table???
なんかこれ怪しい気がする。オプションを見てみると--route-table-idと--subnet-idの2つがある。これはたぶんSubnetごとにRoute Tableを変更することができそうな予感。
理想としてはこうなれば良いんじゃないかと思う。

というわけで、
$ aws ec2 create-route-table --vpc-id vpc-6ece0d0b { "RouteTable": { "Associations": [], "RouteTableId": "rtb-7335fc16", "VpcId": "vpc-6ece0d0b", "PropagatingVgws": [], "Tags": [], "Routes": [ { "GatewayId": "local", "DestinationCidrBlock": "172.16.0.0/16", "State": "active", "Origin": "CreateRouteTable" } ] } }
作った時にはdestination 172.
$ aws ec2 associate-route-table --subnet-id subnet-d40fd8a3 --route-table-id rtb-7335fc16 { "AssociationId": "rtbassoc-d1e236b4" }
$ aws ec2 associate-route-table --subnet-id subnet-5b1cf402 --route-table-id rtb-7335fc16 { "AssociationId": "rtbassoc-dce236b9" }
このコマンドを投入した直後には、
いい感じだ。思っていた通りの動きをしてくれた。
グローバルのネットワークにいる、
[ec2-user@ip-172-16-32-219 $ ping 172.16.64.232 PING 172.16.64.232 (172.16.64.232) 56(84) bytes of data. 64 bytes from 172.16.64.232: icmp_seq=1 ttl=64 time=0.466 ms 64 bytes from 172.16.64.232: icmp_seq=2 ttl=64 time=1.55 ms 64 bytes from 172.16.64.232: icmp_seq=3 ttl=64 time=0.431 ms ^C --- 172.16.64.232 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2405ms rtt min/avg/max/mdev = 0.431/0.817/1.554/0.521 ms
これで当初思い描いていた、
やりたいことを先に思い浮かべたら大体のことはAWSでできそうな気がするけど、
なんだかそれらしくなってきた、
