LoginSignup
0
0

NLBを使ってVPCピアリング環境下でEdge to Edgeを通信する

Posted at

はじめに

AWSではDirectConnectやVPCピアリングを使う環境下において、端から端(Edge to Edge)のネットワークへ通信ができない。そんな状況でもTCP通信できるようにしたい。シンプルなやりたいことに対して、あえてProxyサーバーを設けてメンテナンス工数を増やすもの違う気がするので、今回はELB(NLB)を使うことにした。

現状

そのままではエッジツーエッジ接続ができない。
image.png

やりたいこと

NLBを使うと…通信できるはず
image.png

今回作るもの

すでに存在するVPCNLBVPCid、サブネットNLBsubnetID内にNLBを作成する。作成するものは3つ。CloudFormationで用意する。使わないときはスタックごと立ち下げる。

  • NLB本体
  • NLBのターゲットグループ:ルーティング対象
  • リスナー:NLB本体とターゲットグループの接続を定義

CloudFormationテンプレート

今回ターゲットグループの対象には2つのIPアドレス(Target01IpAddress0*)を指定するようにした。注意点は、ターゲットグループを作成する際、対象となるIPアドレスが存在しないとエラーが発生してスタック作成が失敗するところ。 ターゲットグループの対象とするリソースを用意してからスタックを作る必要がある。作ったものはGitHubに置いた。

template.yaml
AWSTemplateFormatVersion: "2010-09-09"
Parameters:
  UUID:
    Description: UUID of stack items
    Type: String
  NLBVPCid:
    Description: VPC whare to place NLB
    Type: String
    Default: vpc-xxxxxxxxxxxxxxxxx

  NLBsubnetID:
    Description: subnetID
    Type: AWS::EC2::Subnet::Id
    Default: subnet-xxxxxxxxxxxxxxxxx

  #you add more ipaddr&port as much as you want
  PortToSend01:
    Description: TCP Port to send
    Type: Number
    Default: "3389"
  Target01IpAddress01:
    Description: NLB target ip address
    Type: String
    Default: "10.0.2.xxx"
  Target01IpAddress02:
    Description: NLB target ip address
    Type: String
    Default: "10.0.2.xxx"

Resources:
  NLB:
    Type: AWS::ElasticLoadBalancingV2::LoadBalancer
    Properties: 
      IpAddressType: ipv4
      Name: !Sub "myNLB-${UUID}"
      Scheme: internal
      Subnets:
        - !Ref NLBsubnetID
      Type: network
  NLBTargetGroup01:
    Type: AWS::ElasticLoadBalancingV2::TargetGroup
    Properties: 
      HealthCheckEnabled: true
      IpAddressType: ipv4
      Name: !Sub "myNLBTargetGroup01-${UUID}"
      Port: !Ref PortToSend01
      Protocol: TCP
      Targets: 
        - Id: !Ref Target01IpAddress01
          Port: !Ref PortToSend01
        - Id: !Ref Target01IpAddress02
          Port: !Ref PortToSend01
      TargetType: ip
      VpcId: !Ref NLBVPCid
  NLBListener:
    Type: AWS::ElasticLoadBalancingV2::Listener
    Properties: 
      DefaultActions: 
        - Type: forward
          TargetGroupArn: !Ref NLBTargetGroup01
      LoadBalancerArn: !Ref NLB
      Port: !Ref PortToSend01
      Protocol: TCP

終わりに

今回の使い方において良いところは拡張性だと思う。NLBは高可用性がAWS側で担保されている(≒単一障害点にならない)ので、今後VPCがどれだけ増えても、構成そのままにNLBをそのまま使うことができる。

痒い所もある。ELBでは、ターゲットグループのターゲットとしてIPアドレスorインスタンスIDを指定する。ドメイン名が用意されていても、指定ができない。(プライベートネットワークでもドメイン名を指定したいような場合、結構あると思うのだけど。)IPアドレスがころころ変わってしまうような環境の場合は、Lambdaを使って、スタックの更新を自動で行うようにするしかないかな...。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0