AWS Lambda 读取 Elasticache Redis 集群

问题描述 投票:0回答:0

我正在尝试测试一个基本的 python lambda 函数,该函数将获取同一 VPC、子网和安全组中的 Elasticache Redis 实例中的键值。但是,每当我测试代码时,我都会收到连接错误。

我已经将 Redis python 包作为层导入到 Lambda 中。安全组指定端口 6379 上的入站 TCP 连接,其自身是源。我还为 Lambda 创建并分配了一个 IAM 角色,其中包括 AmazonElastiCacheFullAccess 和 AWSLambdaVPCAccessExecutionRole 策略。

我还有一个与 Lambda 相关联的 API 网关,因为我希望能够从网关传递变量以调用 Lambda 在 Redis 数据库上的 GET 语句中传递变量。我已经测试了传递变量并在 Lambda 中打印它们,那部分工作正常。下一步是将这些变量移交给 Redis,但是代码失败了,因为我在 Lambda 和 Redis 之间遇到了连接问题。我从 Lambda 代码中删除了 API 网关脚本,以便我可以测试一个简单的 GET 调用。

Lambda 代码现在看起来像这样:

import redis


r = redis.Redis(host='<elasticache endpoint>', port='6379')


def lambda_handler(event, context):
    
    
    key = 'name'
    
    value = r.get(key)
    
    return print(value)

我可以在我的机器上本地运行代码,但是当我测试 Lambda 时,我收到以下错误:

redis-test

Response
{
  "errorMessage": "Error -3 connecting to <'elaticache endpoint'>:6379. Temporary failure in name resolution.",
  "errorType": "ConnectionError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 13, in lambda_handler\n    value = r.get(key)\n",
    "  File \"/opt/python/redis/commands/core.py\", line 1790, in get\n    return self.execute_command(\"GET\", name)\n",
    "  File \"/opt/python/redis/client.py\", line 1255, in execute_command\n    conn = self.connection or pool.get_connection(command_name, **options)\n",
    "  File \"/opt/python/redis/connection.py\", line 1441, in get_connection\n    connection.connect()\n",
    "  File \"/opt/python/redis/connection.py\", line 704, in connect\n    raise ConnectionError(self._error_message(e))\n"
  ]
}

Function Logs
START RequestId: 0deac267-2abe-48e7-8152-6e735d5ea368 Version: $LATEST
[ERROR] ConnectionError: Error -3 connecting to <'elasticache endpoint'>:6379. Temporary failure in name resolution.
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 13, in lambda_handler
    value = r.get(key)
  File "/opt/python/redis/commands/core.py", line 1790, in get
    return self.execute_command("GET", name)
  File "/opt/python/redis/client.py", line 1255, in execute_command
    conn = self.connection or pool.get_connection(command_name, **options)
  File "/opt/python/redis/connection.py", line 1441, in get_connection
    connection.connect()
  File "/opt/python/redis/connection.py", line 704, in connect
    raise ConnectionError(self._error_message(e))
END RequestId: 0deac267-2abe-48e7-8152-6e735d5ea368
REPORT RequestId: 0deac267-2abe-48e7-8152-6e735d5ea368  Duration: 20117.09 ms   Billed Duration: 20118 ms   Memory Size: 128 MB Max Memory Used: 46 MB  Init Duration: 194.66 ms

Request ID
0deac267-2abe-48e7-8152-6e735d5ea368

我终究无法弄清楚问题是什么。我认为这与 Lambda 和 Elasticache Redis 实例之间的网络组件有关。有什么指示吗?

python amazon-web-services aws-lambda redis amazon-elasticache
© www.soinside.com 2019 - 2024. All rights reserved.