【AWS CLI】Transit Gatewayのルートテーブルの詳細出力編

記事タイトルとURLをコピーする

こんにちは。AWS CLIが好きな福島です。

はじめに

今回は、TGWのルートテーブルを取得する方法をご紹介いたします。

その他のTGWの情報を取得するコマンドはこちらにまとめているため、 ご興味がありましたら、ご確認いただけますと幸いです。

その他のAWS CLI関連の記事

私はよくqueryを使うため、queryの使い方が分からない方は、こちらを参照していただけますと幸いです。

利用するコマンド,サブコマンド

まず、AWS CLIの構造は以下の通りです。

aws <command> <subcommand> [options and parameters]

上記を前提に今回使う <command>,<subcommand>は、以下の通りです。

<command>

  • ec2

※マネジメントコンソールでは、VPCリソースの中にTGWは定義されていますが、AWS CLIの場合、ec2の中に含まれます。

<subcommand>

  • ①search-transit-gateway-routes
    ⇒TGWのルートテーブルの詳細を出力します。

では、ここから実際のコマンドを記載いたします。

search-transit-gateway-routes

このサブコマンドでは、TGWのIDを引数に指定する必要があるため、変数に定義しておきます。 ※TGWのIDを指定するのは大変なため、指定しなくても問題ないコマンドを後半に記載しております。

例)

TGW_ROUTE_ID=tgw-rtb-037f8142479810d43

①Activeなルートの出力

aws ec2 search-transit-gateway-routes --transit-gateway-route-table-id $TGW_ROUTE_ID --filters "Name=state,Values=active"  --query "Routes[].[DestinationCidrBlock,TransitGatewayAttachments[0].ResourceId,TransitGatewayAttachments[0].TransitGatewayAttachmentId,TransitGatewayAttachments[0].ResourceType,Type,State]" --output text
  • 実行結果
192.168.0.0/16       vpc-0fee138d3e0deef81   tgw-attach-04921ff7a05e83c2b    vpc     static  active
10.0.0.0/24     vpc-0fee138d3e0deef81   tgw-attach-04921ff7a05e83c2b    direct-connect-gateway     propagated      active

②blackholeなルートを出力

aws ec2 search-transit-gateway-routes --transit-gateway-route-table-id $TGW_ROUTE_ID --filters "Name=state,Values=blackhole"  --query "Routes[].[DestinationCidrBlock,TransitGatewayAttachments[0].ResourceId,TransitGatewayAttachments[0].TransitGatewayAttachmentId,TransitGatewayAttachments[0].ResourceType,Type,State]" --output text
  • 実行結果
192.168.0.0/24    None    None    None    static  blackhole

③全てのルートを出力

本コマンドは、filtersオプションが必須のため、Activeとblackholeなルートを出力するようfiltersを指定しています。

aws ec2 search-transit-gateway-routes --transit-gateway-route-table-id $TGW_ROUTE_ID --filters "Name=state,Values=active,blackhole"  --query "Routes[].[DestinationCidrBlock,TransitGatewayAttachments[0].ResourceId,TransitGatewayAttachments[0].TransitGatewayAttachmentId,TransitGatewayAttachments[0].ResourceType,Type,State]" --output text
  • 実行結果
192.168.0.0/16       vpc-0fee138d3e0deef81   tgw-attach-04921ff7a05e83c2b    vpc     static  active
10.0.0.0/24     vpc-0fee138d3e0deef81   tgw-attach-04921ff7a05e83c2b    direct-connect-gateway     propagated      active
192.168.0.0/24    None    None    None    static  blackhole

④静的ルートを出力

aws ec2 search-transit-gateway-routes --transit-gateway-route-table-id $TGW_ROUTE_ID --filters "Name=type,Values=static"  --query "Routes[].[DestinationCidrBlock,TransitGatewayAttachments[0].ResourceId,TransitGatewayAttachments[0].TransitGatewayAttachmentId,TransitGatewayAttachments[0].ResourceType,Type,State]" --output text
  • 実行結果
192.168.0.0/16       vpc-0fee138d3e0deef81   tgw-attach-04921ff7a05e83c2b    vpc     static  active
192.168.0.0/24    None    None    None    static  blackhole

⑤動的ルートを出力

aws ec2 search-transit-gateway-routes --transit-gateway-route-table-id $TGW_ROUTE_ID --filters "Name=type,Values=propagated"  --query "Routes[].[DestinationCidrBlock,TransitGatewayAttachments[0].ResourceId,TransitGatewayAttachments[0].TransitGatewayAttachmentId,TransitGatewayAttachments[0].ResourceType,Type,State]" --output text
  • 実行結果
10.0.0.0/24     vpc-0fee138d3e0deef81   tgw-attach-04921ff7a05e83c2b    direct-connect-gateway     propagated      active

全てのTGWのルートテーブルの出力

aws ec2 describe-transit-gateway-route-tables --query "TransitGatewayRouteTables[].[Tags[?Key=='Name'] | [0].Value,TransitGatewayRouteTableId]" --output text | while read line
do 
   name_tag=$(echo $line | awk '{print $1}')
   tgw_route_id=$(echo $line | awk '{print $2}')
   echo "TGWRouteTable:$name_tag($tgw_route_id)"
   aws ec2 search-transit-gateway-routes --transit-gateway-route-table-id $tgw_route_id --filters "Name=state,Values=active,blackhole"  --query "Routes[].[DestinationCidrBlock,TransitGatewayAttachments[0].ResourceId,TransitGatewayAttachments[0].TransitGatewayAttachmentId,TransitGatewayAttachments[0].ResourceType,Type,State]" --output text
done
  • 実行結果
TGWRouteTable:test-route01(tgw-rtb-037f8142479810d43)
192.168.0.0/16       vpc-0fee138d3e0deef81   tgw-attach-04921ff7a05e83c2b    vpc     static  active
10.0.0.0/24     vpc-0fee138d3e0deef81   tgw-attach-04921ff7a05e83c2b    direct-connect-gateway     propagated      active
192.168.0.0/24    None    None    None    static  blackhole
TGWRouteTable:test-route02(tgw-rtb-0c86e3167ff2e5d3c)
192.168.100.0/24       vpc-0fee138d3510dosf0   tgw-attach-04921ff709sd03sl0    vpc     static  active

終わりに

今回は、TGWのルートテーブルの詳細を出力するコマンドをご紹介いたしました。 どなたかのお役に立てれば幸いです。

福島 和弥 (記事一覧)

2019/10 入社

AWS CLIが好きです。

AWS資格12冠。2023 Japan AWS Partner Ambassador/APN ALL AWS Certifications Engineer。