如何解决使用 StackExchange.Redis 从 Lambda 到 ElastiCache Redis:RedisConnectionException - 没有连接处于活动状态/可用于服务此操作
我有一个运行 .NET Core 应用程序的 Lambda 函数,我尝试从中使用 StackExchange.Redis 与 ElastiCache Redis 集群进行交互。 集群是单个节点。 Lambda 配置在与 ElastiCache Redis 集群相同的 VPC 和安全组中。安全组为 VPC 中的所有子网设置了端口 6379 的入站规则。所有这些子网都附加到 Lambda。
只要我的代码尝试连接到 Redis 集群,我就会不断收到以下错误:
{
"errorType": "RedisConnectionException","errorMessage": "No connection is active/available to service this operation: GET <REDACTED-KEY>; UnableToConnect on <REDACTED-PRIMARY-ENDPOINT-NAME>:6379/Interactive,Initializing/NotStarted,last: NONE,origin: BeginConnectAsync,outstanding: 0,last-read: 5s ago,last-write: 5s ago,keep-alive: 60s,state: Connecting,mgr: 10 of 10 available,last-heartbeat: never,global: 5s ago,v: 2.2.50.36290,mc: 1/1/0,clientName: 169,IOCP: (Busy=0,Free=1000,Min=2,Max=1000),WORKER: (Busy=0,Free=32767,Max=32767),v: 2.2.50.36290","stackTrace": [
"at StackExchange.Redis.ConnectionMultiplexer.ThrowFailed[T](TaskCompletionSource`1 source,Exception unthrownException) in /_/src/StackExchange.Redis/ConnectionMultiplexer.cs:line 2799","--- End of stack trace from previous location where exception was thrown ---","at <REDACTED-STACK-TRACE>"
"at lambda_method(Closure,Stream,LambdaContextInternal )"
],"cause": {
"errorType": "RedisConnectionException","errorMessage": "UnableToConnect on <REDACTED-PRIMARY-ENDPOINT-NAME>:6379/Interactive,v: 2.2.50.36290"
}
}
注意:abortConnect=false
是在 Redis 配置中设置的
我已经尝试过的事情:
- 将
elasticache:*
操作添加到我的 Terraform IAM 策略以获得 Lambda 权限。完整政策:
data "aws_iam_policy_document" "iam_policy_document_mylambda" {
statement {
actions = [
"ec2:DescribeNetworkInterfaces","ec2:CreateNetworkInterface","ec2:DeleteNetworkInterface","ec2:DescribeInstances","ec2:AttachNetworkInterface","elasticache:*","execute-api:ManageConnections"
]
resources = ["*"]
effect = "Allow"
}
}
- 将
resolveDns=true
添加到 Redis 配置。 Redis 集群的 IP 地址显示在上面的错误中,而不是主要端点名称,我可以确认它在为 Lambda 配置的子网之一(eu-west-1a 上的子网)的 IP 范围内. - 显式添加
ssl=false
,因为在 Redis 集群上禁用了传输加密(目前)。还尝试使用 GitHub 问题中提到的ssl=true,sslProtocols=tls12
,以确保。 - 删除
abortConnect=false
,然后它失败并显示以下错误:
{
"errorType": "RedisConnectionException","errorMessage": "It was not possible to connect to the redis server(s). Error connecting right now. To allow this multiplexer to continue retrying until it's able to connect,use abortConnect=false in your connection string or AbortOnConnectFail=false; in your code.","stackTrace": [
"at StackExchange.Redis.ConnectionMultiplexer.ConnectImpl(ConfigurationOptions configuration,TextWriter log) in /_/src/StackExchange.Redis/ConnectionMultiplexer.cs:line 1164","at StackExchange.Redis.ConnectionMultiplexer.Connect(ConfigurationOptions configuration,TextWriter log) in /_/src/StackExchange.Redis/ConnectionMultiplexer.cs:line 1032","at ...","at lambda_method(Closure,LambdaContextInternal )"
]
}
我对此不知所措,网上的每一次搜索似乎都指向同一个方向;确保您的 Lambda 和 ElastiCache Redis 集群在相同的 VPC 和安全组中运行,并在 Redis 配置上设置 abortConnect=false
,这对我的情况似乎还不够。有谁知道我还能尝试什么?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。