2026/07/28(火)A5M2からWSL2経由でRDSに接続する方法

投稿日:

A5M2でRDSに繋ぐ方法。aws-cliを使い、WSL2にプロキシを立てるとよい。

この方式だとSSHDを立ててトンネリングするとか、そういう作業が不要なので楽。

手順

前提としてAWSにログインしておく必要がある。

  1. InstanceId探し
    REGION=ap-northeast-1
    aws ssm describe-instance-information --region ${REGION} | jq -r '.InstanceInformationList.[].ComputerName + "\t" + .InstanceInformationList.[].InstanceId'
    
  2. DBホスト探し
    REGION=ap-northeast-1
    aws rds describe-db-instances --region ${REGION} --query 'DBInstances[].{Endpoint:Endpoint.Address,Port:Endpoint.Port}' --output table
    
  3. ブリッジの作成。これはInstanceIdに対応したDBホストを当てる必要があるが、複数ある場合どうやればいいのかはよくわかっていない
    REGION=ap-northeast-1
    INST_ID=i-xxxxxxxxxxx
    HOST=xxxxxxx.yyy.rds.amazonaws.com
    DIST_PORT=3306
    LISTEN_PORT=13306
    aws ssm start-session \
      --region ${REGION} \
      --target ${INST_ID} \
      --document-name AWS-StartPortForwardingSessionToRemoteHost \
      --parameters 'host=${HOST},portNumber=${DIST_PORT},localPortNumber=${LISTEN_PORT}'
    
  4. A5M2でDB接続を次の要領で新規作成すると繋がる
    • ホスト名:127.0.0.1
    • ポート番号:LISTEN_PORT
    • ユーザーID:DB接続するときのもの
    • パスワード:DB接続するときのもの