簡體   English   中英

aws rds 代理從 nodejs12.x 引發超時錯誤

[英]aws rds proxy throws timeout error from nodejs12.x

當我嘗試連接到 mysql rds 代理時出現連接超時。 我遵循本教程

這是我的代碼

import mysql2 from 'mysql2';
import AWS from 'aws-sdk';
const getConnection = async () => {
    const signer = new AWS.RDS.Signer({
        username: 'my-user-name',
        hostname: 'proxy-name.proxy-someid.us-east-1.rds.amazonaws.com',
        port: 3306
    });

    console.info('Connecting to MySQL proxy via IAM authentication');

    const rdsSignerAuth = () => () => {
        console.info('CALL rdsSignerAuth');
        return signer.getAuthToken({
            username: 'my-user-name',
            region: 'us-east-1',
            hostname: 'proxy-name.proxy-someid.us-east-1.rds.amazonaws.com',
            port: 3306
        });
    };

    let connection;
    try {
        connection = await mysql2.createConnection({
            host: 'proxy-name.proxy-someid.us-east-1.rds.amazonaws.com',
            user: 'my-user-name',
            database: 'database-name',
            connectTimeout: 60000,
            ssl: { rejectUnauthorized: false },
            authPlugins: { mysql_clear_password: rdsSignerAuth },
        });
        console.info('Connected');
    }
    catch (e) {
        console.error(`MySQL connection error: ${e}`);
        throw e;
    }
    return connection;
};
const mysql2Impl = async () => {
    const connection = await getConnection();
    //console.info({ type: 'connection', connection });
    const result = await connection.promise().query('select * from destiny;');
    console.info({ type: 'result', result });
};
export async function testRdsProxy(event, context){
    console.info(JSON.stringify({ event, context }));
    await mysql2Impl();
    return 200;
}

這就是回應

Error {
    code: 'ETIMEDOUT',
    errno: undefined,
    message: 'connect ETIMEDOUT',
    sqlState: undefined,
  }

我已經檢查過我的 lambda function 有一個策略“rds-db:connect”到“*”資源。 此外,我檢查了我的代理與我的 rds db 是否在同一個 VPC 和子網中。 保存 RDS 憑據的密鑰是可以的。 我做錯了什么?

該文檔指出,RDS 代理無法公開訪問,因此您的 lambda function 需要與 rds 代理位於同一安全組中。 請注意,當您將 lambda 設置為 vpc 時,您的 lambda 可能會失去訪問互聯網的能力。 謝謝你。

  • 如果您通過 IAM 認證
    檢查用戶名(mysql 用戶)是否有執行 [INVOKE LAMBDA] 權限

  • 如果 IAM 身份驗證失敗
    您應該讓代理設置向導自動創建一個 IAM,如下所示
    連接 > IAM 角色 >創建 IAM 角色
    > IAM 身份驗證 >必需

通過從相同或不同賬戶進行 VPC 對等連接,您甚至可以在 VPC 外部連接 RDS 代理。 我為其中一個項目做的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM