Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ALBの疎通確認をWebサーバーなしでやる

 ALBの疎通確認をWebサーバーなしでやる

Avatar for jhashimoto

jhashimoto

June 18, 2026

More Decks by jhashimoto

Other Decks in Technology

Transcript

  1. 前提 インフラ担当とアプリ担当が別れている インフラ担当: ALB とEC2 の構築 アプリ担当: EC2 のOS より上のレイヤーを担当

    インフラ担当としてALB -> EC2 の疎通確認をして引き渡したい JAWS-UG 群馬 #35 3 / 14
  2. 構成 以下の経路を想定。 クライアント │ HTTPS:443 ▼ ALB + ACM │

    HTTP:8080 ▼ ターゲットグループ │ HTTP:8080 ▼ EC2インスタンス JAWS-UG 群馬 #35 4 / 14
  3. 案1: nc コマンド (1/2) nc コマンドのシンプルな使い方。 # サーバーで待ち受けておく nc -lk

    8080 # クライアントからリクエスト echo "OK" | nc -q 0 {サーバーIP} 8080 -l : Listen モード -k : Keep inbound sockets open for multiple connects. 切断後も同じポートで次の 接続を待ち続ける -q 0 : quit after EOF on stdin and delay of secs. STDIN で EOF 後すぐに閉じる JAWS-UG 群馬 #35 7 / 14
  4. 案2: Python でWeb サーバーを立てる (2/2) PORT=8080 python3 -c " import

    http.server, socketserver, os PORT = int(os.environ['PORT']) class Handler(http.server.BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.end_headers() self.wfile.write(b'OK\n') def log_message(self, format, *args): pass # TCPサーバの終了直後にもう一度起動したときの「Address already in use」エラーを避ける # https://www.geekpage.jp/programming/winsock/so_reuseaddr.php socketserver.TCPServer.allow_reuse_address = True with socketserver.TCPServer(('', PORT), Handler) as httpd: print(f'Listening on port {PORT}') try: httpd.serve_forever() # Ctrl + C による KeyboardInterruptを抑制 except KeyboardInterrupt: pass " JAWS-UG 群馬 #35 10 / 14
  5. 補足: コマンド実行後にPORT 変数は参照されるか? A: 参照されないので、環境を汚さない コマンドの前に KEY=VALUE の形式で書くことを「パラメータ代入」と呼び(シェルの 機能) 、そのコマンドの実行中だけ環境変数として参照できる。

    実験結果: $ PORT=8080 env | grep PORT # PORTを環境変数として参照できる PORT=8080 $ echo ${PORT} # コマンド実行後のシェル自身にはPORTが残っていない(何も出力されない) $ 参考: https://astro.uni-bonn.de/~sysstw/CompMan/gnu/bashref.html#TOC49 JAWS-UG 群馬 #35 11 / 14
  6. まとめ Web サーバーを構成していないターゲットでも、nc コマンドやPython の組み込み HTTP サーバーで疎通確認ができる nc コマンドは手軽だが、ヘルスチェックを通すにはHTTP レスポンスを自分で組み

    立てる必要がある Python の組み込みHTTP サーバーなら、ワンライナーでもHTTP サーバーを起動でき る インフラ担当の責任範囲内(ミドルウェア追加なし)で、ALB -> EC2 の疎通確認 が完結できる JAWS-UG 群馬 #35 13 / 14