如何使用 Node.js 从 AWS CloudFront 连接到 AWS ElastiCache 集群?

How to connect to AWS ElastiCache Cluster from AWS CloudFront using Node.js?

我是 AWS CloudFront 和 AWS 的新手。我有一个 Next.js(React SSR 框架)网站,我使用 serverless-nextjs (https://github.com/serverless-nextjs/serverless-next.js) 将其部署到 AWS 上。但是,我还需要为我的 Web 应用程序进行某种缓存。我决定使用 AWS 的 redis ElastiCache。我在 AWS 控制台上创建了一个 redis ElastiCache 集群。

我的尝试:

我这样设置连接到 redis ElastiCache 的代码:

import redis from 'redis';
...

export async function getServerSideProps() { // Server side function for Next.js
    const cache = redis.createClient(6379, "{PRIMARY-ENDPOINT-URL-AWS}");
}

我 运行 在我的 PC 上本地访问该网站。但是,我从 redis 收到超时错误:Error: connect ETIMEDOUT.

我如何才能从 CloudFront 和我的本地 PC 连接到 redis ElastiCache 集群?

redis ElastiCache 集群截图window:

redis ElastiCache

无法直接从 AWS 外部(即您的本地工作站)连接到 ES。 ES 域被设计为只能从与您的 ES 域位于同一 VPC 中的资源(例如实例)中访问。来自 docs:

Elasticache is a service designed to be used internally to your VPC. External access is discouraged due to the latency of Internet traffic and security concerns. However, if external access to Elasticache is required for test or development purposes, it can be done through a VPN.

启用从 AWS 外部到您的 ES 的连接的唯一方法是在 home/work 网络或 Direct Connect 之间建立 VPN 连接,如 AWS docs:

中所述

This scenario is also supported providing there is connectivity between the customers’ VPC and the data center either through VPN or Direct Connect.

但是,为了快速、临时设置,您可以在本地工作站和 ES 域之间使用 ssh 隧道。这将需要隧道将通过的一些 jump/basion EC2 实例。