AWS Lambda 连接到 RedShift 超时

AWS Lambda times out connecting to RedShift

我的 Redshift 集群位于私有 VPC 中。我在 Node.js 中编写了以下 AWS Lamba,它应该连接到 Redshift(针对这个问题精心准备):

'use strict';
console.log('Loading function');

const pg = require('pg');

exports.handler = (event, context, callback) => {
var client = new pg.Client({
    user: 'myuser',
    database: 'mydatabase',
    password: 'mypassword',
    port: 5439,
    host: 'myhost.eu-west-1.redshift.amazonaws.com'
});


    // connect to our database
    console.log('Connecting...');
    client.connect(function (err) {
        if (err) throw err;

        console.log('CONNECTED!!!');

    });

};

不幸的是,我不断收到 任务在 60.00 秒后超时。我在日志中看到“Connecting...”,但从未看到“CONNECTED!!!”。

到目前为止我已采取的步骤:

那么,有人对我应该检查什么有什么建议吗?

*我应该补充一点,我不是网络专家,所以也许我在某处犯了错误。

超时可能是因为您在 VPC 中的 lambda 无法访问 Internet 以连接到您的集群(您似乎正在使用 public 主机名进行连接)。您的连接选项取决于您的 集群配置 。由于您的 lambda 函数和集群都在同一个 VPC 中,您应该使用集群的私有 IP 连接到它。对于你的情况,我认为简单地使用私有 IP 应该可以解决你的问题。

根据您的集群是否public可访问,有几点需要牢记。

  • 如果您的集群配置为不能public可访问,您可以使用私有 IP 连接到来自 VPC 中的 lambda 运行 的集群,它应该可以工作。

  • 如果您在 VPC 中有一个 public 可访问的集群,并且您想要 使用 VPC 内的私有 IP 地址连接到它,确保以下 VPC 参数为 true/yes:

    • DNS 解析
    • DNS 主机名

verify/change 这些设置的步骤已给出 here

如果您没有将这些参数设置为 true来自 VPC 内部的连接将解析为 EIP 而不是私有 IP,您的 lambda 将无法连接上网(需要NAT网关或NAT实例)

此外,文档 here.

中的重要 注释

If you have an existing publicly accessible cluster in a VPC, connections from within the VPC will continue to use the EIP to connect to the cluster even with those parameters set until you resize the cluster. Any new clusters will follow the new behavior of using the private IP address when connecting to the publicly accessible cluster from within the same VPC.

将 VPC 的 CIDR 范围添加到 Redshift 入站规则后,我的问题得到解决。

我遇到了同样的问题并按照上述步骤操作,我发现在我的情况下,问题是 lambda 位于没有到 NAT 网关的路由的子网中。所以我将 lambda 移动到一个带有 NAT 网关路由的子网中。