RDS Proxy経由でRDSにつながらない場合対処法

RDS Proxy経由でRDSにつながらない場合対処法

RDS Proxyの設定は結構ややこしいです。

version
MySQL8.0.28

ターゲットグループの設定

RDS Proxy経由でRDSにつながらない場合対処法

関連付けられたデータベースが設定されているか確認します。されていない場合は、編集をクリックして設定します。

RDS Proxy経由でRDSにつながらない場合対処法

コマンドで接続状態を確認

LambdaからRDSには繋がるのにRDSProxy経由すると接続できない場合、コマンドで状態を確かめます。

$ aws rds describe-db-proxy-targets --db-proxy-name <プロキシ識別子>
{
    "Targets": [
        {
            "RdsResourceId": "xxxxxxx",
            "Port": 3306,
            "Type": "TRACKED_CLUSTER"
        },
        {
            "Endpoint": "xxxxxxx-instance-1.xxxxxxxxxx.ap-northeast-1.rds.amazonaws.com",
            "TrackedClusterId": "xxxxxxx",
            "RdsResourceId": "xxxxxxx-instance-1",
            "Port": 3306,
            "Type": "RDS_INSTANCE",
            "Role": "UNKNOWN",
            "TargetHealth": {
                "State": "UNAVAILABLE",
                "Reason": "PENDING_PROXY_CAPACITY",
                "Description": "DBProxy Target is waiting for proxy to scale to desired capacity"
            }
        }
    ]
}

DBProxy Target is waiting for proxy to scale to desired capacity

30分程度待ってから再度接続を試みてください。

            "TargetHealth": {
                "State": "UNAVAILABLE",
                "Description": "DBProxy Target unavailable due to an internal error"
            }

次に出るエラーがこのエラーだとセキュリティグループ周りのエラーです。

VPCLambda → RDS Proxy → RDSというフローなのでセキュリティグループの設定は以下の通りにします。

セキュリティグループタイプソース
VPCLambdaのセキュリティグループインバウンド未設定インバウンド未設定
RDSのセキュリティグループMYSQL/AuroraRDS Proxyのセキュリティグループ
RDS ProxyのセキュリティグループMYSQL/Aurora0.0.0.0/0

VPC Lambdaのセキュリティグループのインバウンドは未設定で問題ありません。

これで再度コマンド実行します。

$ aws rds describe-db-proxy-targets --db-proxy-name <プロキシ識別子>
{
    "Targets": [
        {
            // ....省略
            "Port": 3306,
            "Type": "RDS_INSTANCE",
            "TargetHealth": {
                "State": "AVAILABLE" // OK
            }
        }
    ]
}

これで接続OKです。

VPC Lambdaから接続

同じVPC上に配置したVPC Lambdaから接続します。

import * as mysql from 'promise-mysql'

export async function handler(event, context) {
    const conn = await mysql.createConnection({
        host: 'rdspro.proxy-cjvwtdcasc7l.ap-northeast-1.rds.amazonaws.com',
        port: 3306,
        user: 'admin',
        password: '12345678',
        database: 'mysql',
        dateStrings: true
      })
      const results = await conn.query('select * from mysql.user',[])
      console.log(results)
      await conn.end()
    return {
      statusCode: 200,
      body: 'Hello World!!!'
    }
  }

RDS Proxyのcloudwatchのログが出ていればVPC Lambdaからの接続はOKです。

RDS Proxy経由でRDSにつながらない場合対処法

RDS Proxy経由でRDSにつながらない場合対処法

参考サイト

RDS Proxy のトラブルシューティング - Amazon Relational Database Service
RDS Proxy の問題をトラブルシューティングする方法について説明します。

https://qiita.com/mmmm/items/0486f6f4be44109b5e32

コメント

タイトルとURLをコピーしました