diff --git a/ec2/README.md b/ec2/README.md new file mode 100644 index 00000000..0dda354b --- /dev/null +++ b/ec2/README.md @@ -0,0 +1,62 @@ +# EC2インスタンス管理資材 + +## NLBゲートウェイ インスタンス 起動スクリプト + +### 目的 + +Merck様のDB接続経路としてのNLBから、Auroraデータベースに踏み台アクセスを行うため、ゲートウェイの機能を提供するEC2インスタンスを稼働している。 +EC2インスタンス内では、NLBからの任意のポートをAuroraデータベースのポートにフォワーディングするために、`socat`プロセスを動かす必要がある。 +`socat`プロセスを動かすためのコマンドと、EC2インスタンス起動時にプロセスを実行するためのsystemdの設定を配置している。 + +### フォルダ構成 + +```txt +. +├── README.md -- 当ファイル +└── gateway + ├── staging -- ステージング環境用設定 + │ ├── public1-1 -- ap-northeast-1aのインスタンス要設定 + │ │ ├── socat-dbconnection-1a.service -- systemdにsocatプロセスを登録するためのファイル + │ │ └── socat-portforwarding-1a.sh -- socatでAuroraデータベースにポートフォワーディングするためのシェルスクリプト + │ └── public2-1 -- ap-northeast-1dのインスタンス要設定 + │ ├── socat-dbconnection-1d.service + │ └── socat-portforwarding-1d.sh + │ + ├── product -- 本番環境用設定 + │ ├── public1-1 + │ │ ├── socat-dbconnection-1a.service + │ │ └── socat-portforwarding-1a.sh + │ └── public2-1 + │ ├── socat-dbconnection-1d.service + │ └── socat-portforwarding-1d.sh +``` + +### ファイル配置方法(両環境共通) + +- 対象のゲートウェイEC2インスタンスにログインする + - セッションマネージャーでログインした場合は、`ec2-user`に切り替えること(`sudo su --login ec2-user`) +- 以下の操作を実行し、`socat`プロセスを起動するシェルスクリプトを配置する。 + - **ap-northeast-1aのインスタンス(public-1-1)の場合** + - `sudo vi /opt/socat-portforwarding-1a.sh`コマンドを実行する。 + - `ec2/gateway/<環境名>/public-1-1/socat-portforwarding-1a.sh`の内容をコピペして保存する。 + - `sudo chmod 774 /opt/socat-portforwarding-1a.sh`コマンドを実行する。 + - **ap-northeast-1dのインスタンス(public-2-1)の場合** + - `sudo vi /opt/socat-portforwarding-1d.sh`コマンドを実行する。 + - `ec2/gateway/<環境名>/public-2-1/socat-portforwarding-1d.sh`の内容をコピペして保存する。 + - `sudo chmod 774 /opt/socat-portforwarding-1d.sh`コマンドを実行する。 +- 以下の操作を実行し、`socat`プロセスを常駐させるためのサービス設定ファイルを配置する。 + - **ap-northeast-1aのインスタンス(public-1-1)の場合** + - `sudo vi /etc/systemd/system/socat-dbconnection-1a.service`コマンドを実行する。 + - `ec2/gateway/<環境名>/public-1-1/socat-dbconnection-1a.service`の内容をコピペして保存する。 + - `sudo chmod 774 /etc/systemd/system/socat-dbconnection-1a.service`コマンドを実行する。 + - **ap-northeast-1dのインスタンス(public-2-1)の場合** + - `sudo vi /etc/systemd/system/socat-dbconnection-1d.service`コマンドを実行する。 + - `ec2/gateway/<環境名>/public-2-1/socat-dbconnection-1d.service`の内容をコピペして保存する。 + - `sudo chmod 774 /etc/systemd/system/socat-dbconnection-1d.service`コマンドを実行する。 +- 以下の操作を実行し、`socat`プロセスを起動するスクリプトをsystemdに登録する + - **ap-northeast-1aのインスタンス(public-1-1)の場合** + - `sudo systemctl enable socat-dbconnection-1a.service`コマンドを実行し、サービスを有効化する。 + - `sudo systemctl status socat-dbconnection-1a.service`コマンドを実行し、サービスのステータスを確認する。`enabled;`となっていればOK + - **ap-northeast-1dのインスタンス(public-2-1)の場合** + - `sudo systemctl enable socat-dbconnection-1d.service`コマンドを実行し、サービスを有効化する。 + - `sudo systemctl status socat-dbconnection-1d.service`コマンドを実行し、サービスのステータスを確認する。`enabled;`となっていればOK diff --git a/ec2/gateway/product/public1-1/socat-dbconnection-1a.service b/ec2/gateway/product/public1-1/socat-dbconnection-1a.service new file mode 100644 index 00000000..551ac04d --- /dev/null +++ b/ec2/gateway/product/public1-1/socat-dbconnection-1a.service @@ -0,0 +1,10 @@ +[Unit] +Description = socat-dbconnection-1a + +[Service] +ExecStart = /opt/socat-portforwarding-1a.sh +Type = oneshot +RemainAfterExit = yes + +[Install] +WantedBy = default.target \ No newline at end of file diff --git a/ec2/gateway/product/public1-1/socat-portforwarding-1a.sh b/ec2/gateway/product/public1-1/socat-portforwarding-1a.sh new file mode 100644 index 00000000..c257a075 --- /dev/null +++ b/ec2/gateway/product/public1-1/socat-portforwarding-1a.sh @@ -0,0 +1,3 @@ +#!/bin/bash +socat tcp4-listen:40001,reuseaddr,fork TCP:mbj-newdwh2021-product-dbcluster-instance-1.chs11qsgoyix.ap-northeast-1.rds.amazonaws.com:3306 & +socat tcp4-listen:50001,reuseaddr,fork TCP:mbj-newdwh2021-product-dbcluster.cluster-chs11qsgoyix.ap-northeast-1.rds.amazonaws.com:3306 & \ No newline at end of file diff --git a/ec2/gateway/product/public2-1/socat-dbconnection-1d.service b/ec2/gateway/product/public2-1/socat-dbconnection-1d.service new file mode 100644 index 00000000..a7f9be7c --- /dev/null +++ b/ec2/gateway/product/public2-1/socat-dbconnection-1d.service @@ -0,0 +1,10 @@ +[Unit] +Description = socat-dbconnection-1d + +[Service] +ExecStart = /opt/socat-portforwarding-1d.sh +Type = oneshot +RemainAfterExit = yes + +[Install] +WantedBy = default.target \ No newline at end of file diff --git a/ec2/gateway/product/public2-1/socat-portforwarding-1d.sh b/ec2/gateway/product/public2-1/socat-portforwarding-1d.sh new file mode 100644 index 00000000..0f1d2747 --- /dev/null +++ b/ec2/gateway/product/public2-1/socat-portforwarding-1d.sh @@ -0,0 +1,3 @@ +#!/bin/bash +socat tcp4-listen:40001,reuseaddr,fork TCP:mbj-newdwh2021-product-dbcluster-instance-1-ap-northeast-1d.chs11qsgoyix.ap-northeast-1.rds.amazonaws.com:3306 & +socat tcp4-listen:50001,reuseaddr,fork TCP:mbj-newdwh2021-product-dbcluster.cluster-chs11qsgoyix.ap-northeast-1.rds.amazonaws.com:3306 & \ No newline at end of file diff --git a/ec2/gateway/staging/public1-1/socat-dbconnection-1a.service b/ec2/gateway/staging/public1-1/socat-dbconnection-1a.service index ce2d87d3..551ac04d 100644 --- a/ec2/gateway/staging/public1-1/socat-dbconnection-1a.service +++ b/ec2/gateway/staging/public1-1/socat-dbconnection-1a.service @@ -1,5 +1,5 @@ [Unit] -Description = socat_dbconnection-1a +Description = socat-dbconnection-1a [Service] ExecStart = /opt/socat-portforwarding-1a.sh diff --git a/ec2/gateway/staging/public2-1/socat-dbconnection-1d.service b/ec2/gateway/staging/public2-1/socat-dbconnection-1d.service index 06c0c649..a7f9be7c 100644 --- a/ec2/gateway/staging/public2-1/socat-dbconnection-1d.service +++ b/ec2/gateway/staging/public2-1/socat-dbconnection-1d.service @@ -1,5 +1,5 @@ [Unit] -Description = socat_dbconnection-1d +Description = socat-dbconnection-1d [Service] ExecStart = /opt/socat-portforwarding-1d.sh