AWS CLIを使用してEC2でVPC Reachability Analyzerをしてみた。

2022.04.15

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

VPC Reachability Analyzerとは?

VPC Reachability Analyzerは分析ツールです。Reachability AnalyzerでVPC内の2つのリソース間の接続性テストを実行できます。Reachability Analyzerは、ソースリソースからデスティネーションリソースに到達可能かどうかを判断することができます。

Reachability status はReachable とき :Reachability Analyzerは、ソースとデスティネーションの間の仮想ネットワークパスの詳細を作成します。

Reachability statusNot reachable とき : Reachability Analyzerは、ブロックしているコンポーネントを特定します。

 

次のリソースタイプは、ソースとデスティネーションとしてサポートされます:

  • Transit gateways
  • Transit gateway attachments
  • VPN gateways
  • Instances
  • Internet gateways
  • Network interfaces
  • VPC endpoints
  • VPC peering connections

 

必要条件 :  ソースとデスティネーションは同じAWSアカウント、同じリージョン、同じVPC またはVPC peering connectionで接続されているVPCsにある必要があります。

この記事では、AWS CLIを使用してEC2でVPC Reachability Analyzerをしてみました。

以下を試しました:

  • インターネットゲートウェイからパブリックインスタンスへの到達可能性
  • インターネットゲートウェイからプライベートインスタンスへの到達可能性
  • パブリックインスタンスからプライベートインスタンスへの到達可能性

 

やってみた

  • この設定で 2つのEC2インスタンスを作成しておきます。
    • パブリックインスタンス
      • AMI : Amazon Linux 2 AMI
      • インスタンスタイプ : t2.micro
      • サブネット:パブリックサブネット
      • Security Group :
        •  Inbound :SSH
    • プライベートインスタンス
      • AMI : Amazon Linux 2 AMI
      • インスタンスタイプ : t2.micro
      • サブネット:プライベートサブネット
      • Security Group : 最初はインバウンドルールはありません

 

 

インターネットゲートウェイからパブリックインスタンスへの到達可能性

パスの作成

  • 次のコマンドを使用してパスを作成しておきます。
    • Source : Internet Gateway
    • Destination : public Instance

 

//Command to create path
aws ec2 create-network-insights-path --source igw-000000001 --destination  i-06200000000090 --destination-port 22 --protocol TCP --region us-east-1

//Output
{
    "NetworkInsightsPath": {
        "NetworkInsightsPathId": "nip-0620000000001",
        "NetworkInsightsPathArn": "arn:aws:ec2:us-east-1:xxxxxxxxx:network-insights-path/nip-06200000000001",
        "CreatedDate": "2022-04-15T02:16:12.499000+00:00",
        "Source": " igw-000000001",
        "Destination": "i-06200000000090",
        "Protocol": "tcp",
        "DestinationPort": 22
    }
}

 

パスを分析する

  • 次のコマンドを使用して、パスに指定したプロトコルとポートを使用して宛先に到達できるかどうかを確認しておきます。
    • network-insights-path-id : pathコマンド出力からNetworkInsightsPathId

 

//Command to analyse the path
aws ec2 start-network-insights-analysis --network-insights-path-id nip-0620000000001 --region us-east-1 


//Output
{
    "NetworkInsightsAnalysis": {
        "NetworkInsightsAnalysisId": "nia-0c0000000001",
        "NetworkInsightsAnalysisArn": "arn:aws:ec2:us-east-1:xxxxxxx:network-insights-analysis/nia-0c0000000001",
        "NetworkInsightsPathId": "nip-0620000000001",
        "StartDate": "2022-04-15T02:18:14.068000+00:00",
        "Status": "running"
    }
}

 

到達可能性を確認する

  • 到達可能性を確認するには、[describe-network-insights-analyses]コマンドを使用しておきます。
    • network-insights-analysis-ids : 前のコマンドからのNetworkInsightsAnalysisId

 

//Command to describe the analysis
aws ec2 describe-network-insights-analyses --network-insights-analysis-ids nia-0c0000000001 --region us-east-1


//Output
{
    "NetworkInsightsAnalyses": [
        {
            "NetworkInsightsAnalysisId": "nia-0c0000000001",
            "NetworkInsightsAnalysisArn": "arn:aws:ec2:us-east-1:xxxxxx:network-insights-analysis/nia-0c0000000001",
            "NetworkInsightsPathId": "nip-0620000000001",
            "StartDate": "2022-04-15T02:18:14.068000+00:00",
            "Status": "succeeded",
            "NetworkPathFound": true,
            .........

 

  • [NetworkPathFound": true]は、パブリックインスタンスがインターネットゲートウェイから到達可能であることを示します。
  • 到達可能な場合は、非常に詳細で長い出力が得られます。

 

 

インターネットゲートウェイからプライベートインスタンスへの到達可能性

  • 次のコマンドを使用して、インターネットゲートウェイからプライベートインスタンスの到達可能性を確認しておきます。
    • Source : Internet Gateway
    • Destination : private Instance

 

//Command to create path
aws ec2 create-network-insights-path --source igw-000000001  --destination  i-0620000000008 --destination-port 22 --protocol TCP --region us-east-1
//Command to analyse the path
aws ec2 start-network-insights-analysis --network-insights-path-id nip-0620000000002 --region us-east-1 
//Command to describe the analysis
aws ec2 describe-network-insights-analyses --network-insights-analysis-ids nia-0c0000000002 --region us-east-1


//Output
{
    "NetworkInsightsAnalyses": [
        {
            "NetworkInsightsAnalysisId": "nia-0c0000000002",
            "NetworkInsightsAnalysisArn": "arn:aws:ec2:us-east-1:xxxxxxx:network-insights-analysis/nia-0c0000000002",
            "NetworkInsightsPathId": "nip-0620000000002",
            "StartDate": "2022-04-15T02:25:23.119000+00:00",
            "Status": "succeeded",
            "NetworkPathFound": false,
            ...........

 

  • [NetworkPathFound": false]は、プライベートインスタンスがインターネットゲートウェイから到達できないことを示します。

 

パブリックインスタンスからプライベートインスタンスへの到達可能性

  • ここでは、プライベートインスタンスにはインバウンドルールがありません。
  • 次のコマンドを使用して、パブリックインスタンスからプライベートインスタンスの到達可能性を確認しておきます。
    • Source : public Instance
    • Destination : private Instance

 

//Command to create path
aws ec2 create-network-insights-path --source i-06200000000090  --destination  i-0620000000008 --destination-port 22 --protocol TCP --region us-east-1
//Command to analyse the path
aws ec2 start-network-insights-analysis --network-insights-path-id nip-0620000000003 --region us-east-1 
//Command to describe the analysis
aws ec2 describe-network-insights-analyses --network-insights-analysis-ids nia-0c0000000003 --region us-east-1


//Output
{
    "NetworkInsightsAnalyses": [
        {
            "NetworkInsightsAnalysisId": "nia-0c0000000003",
            "NetworkInsightsAnalysisArn": "arn:aws:ec2:us-east-1:xxxxxxx:network-insights-analysis/nia-0c0000000003",
            "NetworkInsightsPathId": "nip-0620000000003",
            "StartDate": "2022-04-15T02:25:23.119000+00:00",
            "Status": "succeeded",
            "NetworkPathFound": false,
             ........

 

  • [NetworkPathFound": false] :プライベートインスタンスのサブネットグループにはパブリックサブネットからのアクセスを許可するSSHがないため、パブリックインスタンスからプライベートインスタンスに到達することはできません。

 

 

  • パブリックインスタンスのセキュリティグループをソースとして、SSHインバウンドルールをプライベートサブネットセキュリティグループに追加しておきます。
  • コマンドを再度実行して、パブリックインスタンスからプライベートインスタンスの到達可能性を確認しておきます。

 

//Command to create path
aws ec2 create-network-insights-path --source i-06200000000090  --destination  i-0620000000008 --destination-port 22 --protocol TCP --region us-east-1
//Command to analyse the path
aws ec2 start-network-insights-analysis --network-insights-path-id nip-0620000000004 --region us-east-1 
//Command to describe the analysis
aws ec2 describe-network-insights-analyses --network-insights-analysis-ids nia-0c0000000004 --region us-east-1


//Output
{
    "NetworkInsightsAnalyses": [
        {
            "NetworkInsightsAnalysisId": "nia-0c0000000004",
            "NetworkInsightsAnalysisArn": "arn:aws:ec2:us-east-1:xxxxxxx:network-insights-analysis/nia-0c0000000004",
            "NetworkInsightsPathId": "nip-0620000000004",
            "StartDate": "2022-04-15T02:25:23.119000+00:00",
            "Status": "succeeded",
            "NetworkPathFound": true,
             ........

 

  • [NetworkPathFound": true] : SSHルールをプライベートインスタンスに追加すると、パブリックインスタンスから到達可能になりました。

 

 

  • コンソールで作成されたパスを見ることができます。

 

まとめ

AWS CLIを使用してEC2でVPC Reachability Analyzerをしてみました。Reachability Analyzerを使用して、VPC内の他のリソースの到達可能性を確認できます。Reachability Analyzerを使用すると、接続の問題をトラブルシューティングしたり、接続を確認したりできます。

Reference : Reachability Analyzer using AWS CLI