SSHトンネルでサーバーにSSH接続を行う。
踏み台にするEC2サーバーと、接続するRDSのエンドポイントとポートを指定する。
公式ドキュメント
Welcome to sshtunnel’s documentation!
インストール
py -m pip install sshtunnel
インポート
from sshtunnel import SSHTunnelForwarder
サーバー設定
ec2_server = SSHTunnelForwarder(
(EC2_instabnce_globalIP, 22),
ssh_host_key = None,
ssh_username = 'user-id',
ssh_password = None,
ssh_pkey = 'D:\xxxxxxxx',
remote_bind_address = (RDS_database_endpoint, 3306),
local_bind_address = ('localhost', 10022) # Pythonを実行するPCのアドレス(localhost=127.0.0.0))と適当なPort
)
サーバーの開始
ec2_server.start()
サーバーの停止
ec2_server.stop()