LoginSignup
4
2

More than 1 year has passed since last update.

【初心者】AWS Wavelengthを使ってみる #8 (iptables 中継サーバによるキャリア網以外からのアクセス)

Posted at

1. はじめに

  • AWS Wavelengthはキャリア網(日本ではKDDI網)からのアクセスのみが許可されており、Wavelength Zone内にWebサーバを立てても、インターネットからアクセスすることはできない。検証などの際に不便なため、東京リージョンにiptablesによる中継サーバを立てて、Wavelength ZoneのWebサーバに対してインターネット経由でのアクセスを可能にする。

2. やったこと

  • 東京リージョンとWavelength Zone(東京)の両方にsubnetを持つVPCを作成する。
  • Wavelength Zone (東京) に、Webサーバ(nginx)を立てる。
  • 東京リージョンに中継用のサーバ(Amazon Linux 2 のVM内にてiptablesを設定)を立てる。
  • 手元のPC - インターネット - 中継サーバ - AWS VPC内ネットワーク - Webサーバ の経路でhttpアクセスを行う。

3. 構成図

iptables構成図.png

4. 手順

東京リージョンに作成する中継サーバ(Amazon Linux 2) の設定内容を記載する。

4.1 IPフォワーディングの有効化

  • /etc/sysctl.conf に以下を追記する。
/etc/sysctl.conf
net.ipv4.ip_forward = 1
  • networkをrestartして設定を有効化する。
[ec2-user@ip-10-0-1-184 etc]$ sudo sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 0
[ec2-user@ip-10-0-1-184 etc]$ sudo systemctl restart network
[ec2-user@ip-10-0-1-184 etc]$ sudo sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 1

4.2 iptablesの設定

  • 自分宛てのhttp(80/tcp)のパケットのDSTIPをWavelength上のWebサーバのIPアドレスに書き換える。また、SRCIPを自身のIPアドレスに書き換える。
[ec2-user@ip-10-0-1-184 ~]$ sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 10.0.0.27:80
[ec2-user@ip-10-0-1-184 ~]$ sudo iptables -t nat -A POSTROUTING -p tcp -d 10.0.0.27 --dport 80 -j MASQUERADE
  • 手元のPCから、インターネット経由で「http://中継サーバのEIP/」 にアクセスすると、中継サーバからWavelength上のWEBサーバへパケットが転送され、Webの画面を表示することができる。

iptables01.png

4.3 設定の保存

  • OS再起動しても設定が維持されるようにする。設定を保存するためのコマンドが使えるようにパッケージをインストールした上で、「service iptables save」で設定を保存する。
[ec2-user@ip-10-0-1-184 ~]$ sudo yum install iptables-services
[ec2-user@ip-10-0-1-184 ~]$ sudo systemctl enable iptables
[ec2-user@ip-10-0-1-184 ~]$ sudo service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
  • 設定が保存されていることを確認する。
[ec2-user@ip-10-0-1-184 ~]$ sudo cat /etc/sysconfig/iptables
# Generated by iptables-save v1.8.4 on Fri Dec  9 14:53:39 2022
*nat
:PREROUTING ACCEPT [60:3120]
:INPUT ACCEPT [60:3120]
:OUTPUT ACCEPT [135:10974]
:POSTROUTING ACCEPT [135:10974]
-A PREROUTING -p tcp -m tcp --dport 80 -j DNAT --to-destination 10.0.0.27:80
-A POSTROUTING -d 10.0.0.27/32 -p tcp -m tcp --dport 80 -j MASQUERADE
COMMIT
# Completed on Fri Dec  9 14:53:39 2022

5. 参考サイト

6. 所感

  • サーバとクライアントを直接通信させることができない環境で中継サーバを立てることはよくありそうなので、今回の使い方以外でもいろいろ使えるように設定を理解していきたい。
4
2
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
4
2