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

标签 node.js amazon-web-services redis amazon-cloudfront amazon-elasticache

我对 AWS CloudFront 和 AWS 总体来说是新手。我有一个 Next.js(React SSR 框架)网站,我使用 serverless-nextjs ( https://github.com/serverless-nextjs/serverless-next.js ) 将其部署到 AWS 上。但是,我还需要为我的网络应用程序进行某种缓存。我决定使用 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}");
}

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

我如何从 CloudFront 和本地 PC 连接到 Redis ElastiCache 集群?

redis ElastiCache Cluster 窗口的屏幕截图:

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 的连接的唯一方法是在家庭/工作网络或 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 隧道。这将需要一些隧道将经过的跳转/基础 EC2 实例。

关于node.js - 如何使用 Node.js 从 AWS CloudFront 连接到 AWS ElastiCache 集群?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65638520/

相关文章:

redis - 如何安装最新版本的redis?

c# - Redis MSET 相当于 SET 或 LIST?

redis - 在多线程环境中从 Redis 列表中弹出值会将重复值返回给少数线程

node.js - 如何使用 node.js 设置工作网络上的其他人可以访问的服务器?

node.js - 如何在 Sails.js 中监听来自套接字的消息?

amazon-web-services - 在 AWS 中启动 EC2 失败

javascript - AWS Pagination listObjects node.js

azure - 将 azure 托管根域指向 aws classic 负载均衡器

html - 将缓存 list 添加到 Meteor 应用程序的简单方法?

angularjs - http 将密码从 Angular 发送到 Node.js 服务器