4

I'm getting a connection timeout when I try to connect to mysql rds proxy. I'm followed this tutorial

This is my code

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;
}

And this is the response

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

I already checked that my lambda function has a policy "rds-db:connect" to "*" resource. Besides, I checked that my proxy is in the same VPC and subnet that my rds db. The secret that holds the credentials to RDS is ok. What I am doing wrong?

4

3 回答 3

5

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

于 2020-05-22T11:13:42.690 回答
0

通过从相同或不同账户进行 VPC 对等连接,您甚至可以在 VPC 外部连接 RDS 代理。我为其中一个项目做的

于 2020-12-21T17:54:26.373 回答
0
  • 如果您通过 IAM 认证
    ,请检查用户名(mysql 用户)是否具有执行 [INVOKE LAMBDA] 权限

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

于 2020-07-28T08:56:11.230 回答