aws rds 代理从 nodejs12.x 引发超时错误

Posted

技术标签:

【中文标题】aws rds 代理从 nodejs12.x 引发超时错误【英文标题】:aws rds proxy throws timeout error from nodejs12.x 【发布时间】:2020-07-31 08:31:59 【问题描述】:

当我尝试连接到 mysql rds 代理时,我遇到了连接超时。我关注了这个tutorial

这是我的代码

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 函数有一个策略“rds-db:connect”到“*”资源。此外,我检查了我的代理与我的 rds db 是否在同一个 VPC 和子网中。保存 RDS 凭据的密钥是可以的。我做错了什么?

【问题讨论】:

您是否启用了代理登录?请检查日志并更新。我已经搞定了,也许能帮上忙。 【参考方案1】:

文档声明 RDS 代理无法公开访问,因此您的 lambda 函数需要与 rds 代理位于同一安全组中。 请注意,当您将 lambda 转换为 vpc 时,您的 lambda 可能会失去访问互联网的能力。 谢谢。

【讨论】:

是的,终于工作了。我将 lambdas 放在同一个 vpc 中的 dmz 上。现在 lambdas 可以连接到 rds 代理并且仍然具有公共 ip 和互联网访问权限 RDS Proxy 和 Lambda 需要在同一个安全组还是同一个 VPC? @sdgfsdh 只是同一个 VPC。但在代理 SG 上,您需要允许来自 Lambda SG 的入站流量【参考方案2】:

如果您通过了 IAM 认证检查用户名(mysql 用户)是否具有执行 [INVOKE LAMBDA] 权限

如果 IAM 身份验证失败您应该让代理设置向导自动创建如下所示的 IAM 连接性 > IAM 角色 > 创建 IAM 角色 > IAM 身份验证 > 必需

【讨论】:

【参考方案3】:

即使在 VPC 之外,您也可以通过从相同或不同帐户进行 VPC 对等连接来连接 RDS 代理。我是为其中一个项目做的

【讨论】:

以上是关于aws rds 代理从 nodejs12.x 引发超时错误的主要内容,如果未能解决你的问题,请参考以下文章

出于开发目的,无法从本地主机连接到 AWS RDS Postgresql

AWS Lambda NodeJS 连接到 RDS Postgres 数据库

如何连接nodejs sequelize aws rds mysql数据库和ec2实例

“AWS RDS 数据库错误:ER_NO_SUCH_TABLE:表不存在”-但相同的架构在本地 mysql/Nodejs 上运行良好

将 NodeJS 运行时从 8.10 更新到 10.x 或 12.x - AWS Amplify

AWS RDS / EC2:TimeoutError:Knex:获取连接超时。游泳池可能已满