Closed7
RDS Proxy から Secret Manager を参照する際にKey名に気をつけないといけなかった

RDS Proxy から Aurora (MySQL) に接続しようとした際に 接続情報を Secret Manager を使って管理していたが設定する値がよくなくてうまく接続できなかった。その解消方法について記録。

RDS、及び RDS Proxy の設定はどちらも構築済みであり、AWSマネジメントコンソール上で見ても利用可能なステータスに切り替わっている状態。
しかし、EC2などの踏み台からRDS経由で接続しに行こうとした際に以下のエラーが出ておりうまく接続できない。
# ssm 接続してRDS Proxy にアクセスするも失敗
sh-5.2$ mysql -h sample-core-db-proxy.proxy-comphbxyz54t.ap-northeast-1.rds.amazonaws.com -P 3306 -u core -ppassword
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 2
RDS Proxy の状態を確認してみると以下のような状況。
$ aws rds describe-db-proxy-targets --db-proxy-name sample-core-db-proxy | jq .
{
"Targets": [
{
"RdsResourceId": "sample-core-db",
"Port": 3306,
"Type": "TRACKED_CLUSTER"
},
{
"Endpoint": "sample-core-db-instance-1.comphbxyz54t.ap-northeast-1.rds.amazonaws.com",
"TrackedClusterId": "sample-core-db",
"RdsResourceId": "sample-core-db-instance-1",
"Port": 3306,
"Type": "RDS_INSTANCE",
"Role": "READ_WRITE",
"TargetHealth": {
"State": "UNAVAILABLE",
"Reason": "AUTH_FAILURE",
"Description": "Proxy does not have any registered credentials"
}
}
]
}
Proxy does not have any registered credentials
というエラーが出ている。RDS Proxy がRDSの接続情報を正しく認識できていない模様。

なお、Secret Manager は既に設定しており、以下のような状況
$ aws secretsmanager get-secret-value --secret-id "core/test/sample-rds-cluster" | jq
{
"ARN": "arn:aws:secretsmanager:ap-northeast-1:873276765047:secret:core/test/sample-rds-cluster-Iexcrw",
"Name": "core/test/sample-rds-cluster",
"VersionId": "5220cd08-6716-4b52-b11e-7d8d14ab1451",
"SecretString": "{\n \"MYSQL_DATABASE\":\"test_core\",\n \"MYSQL_HOST\":\"sample-core-db-proxy.proxy-comphbxyz54t.ap-northeast-1.rds.amazonaws.com\",\n \"MYSQL_PASSWORD\":\"password\",\n \"MYSQL_PORT\":\"3306\",\n \"MYSQL_USERNAME\":\"core\"\n }",
"VersionStages": [
"AWSCURRENT"
],
"CreatedDate": "2025-06-23T00:09:27.327000+09:00"
}

公式や、同じような境遇に遭遇している方いないか探すもいない。AIに状況伝えたところ以下が原因とのこと。
良くない例(今の設定)、これだと RDS Proxy が認識できない模様。
{
"MYSQL_USER": "core",
"MYSQL_PASSWORD": "password"
}
良い例(最低限、username, passwordがあれば良いとのこと)
{
"username": "core",
"password": "password"
}

試しに、Secret Manager の値を更新するため以下実行
$ aws secretsmanager put-secret-value \
--secret-id "core/test/sample-rds-cluster" \
--secret-string '{
"dbname": "test_core",
"host": "sample-core-db-proxy.proxy-comphbxyz54t.ap-northeast-1.rds.amazonaws.com",
"port": "3306",
"username": "core",
"password": "password"
}' | jq .
{
"ARN": "arn:aws:secretsmanager:ap-northeast-1:${aws-account-id}:secret:core/test/sample-rds-cluster-Iexcrw",
"Name": "core/test/sample-rds-cluster",
"VersionId": "94235d1b-71f7-4015-9c0f-d30d273b1914",
"VersionStages": [
"AWSCURRENT"
]
}

RDS Proxy の状態も AVAILABLE
になったことを確認
$ aws rds describe-db-proxy-targets --db-proxy-name sample-core-db-proxy | jq .
{
"Targets": [
{
"RdsResourceId": "sample-core-db",
"Port": 3306,
"Type": "TRACKED_CLUSTER"
},
{
"Endpoint": "sample-core-db-instance-1.comphbxyz54t.ap-northeast-1.rds.amazonaws.com",
"TrackedClusterId": "sample-core-db",
"RdsResourceId": "sample-core-db-instance-1",
"Port": 3306,
"Type": "RDS_INSTANCE",
"Role": "READ_WRITE",
"TargetHealth": {
"State": "AVAILABLE"
}
}
]
}

EC2 から ssm 接続で RDS Proxy を経由して DB にアクセスできることを確認。
sh-5.2$ mysql -h sample-core-db-proxy.proxy-comphbxyz54t.ap-northeast-1.rds.amazonaws.com -P 3306 -u core -ppassword
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2289421174
Server version: 8.0.39 8bc99e28
Copyright (c) 2000, 2025, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
このスクラップは3ヶ月前にクローズされました