elasticache redis - python - 连接超时

NoI*_*his 2 python redis amazon-elasticache serverless-framework

我有一个相当简单的测试应用程序:

import redis
import os
import logging

log = logging.getLogger()
log.setLevel(logging.DEBUG)

def test_redis(event, context):
    redis_endpoint = None
    if "REDIS" in os.environ:
        redis_endpoint = os.environ["REDIS"]
        log.debug("redis: " + redis_endpoint)
    else:
        log.debug("cannot read REDIS config environment variable")
        return {
            'statusCode': 500
        }

    redis_conn = None
    try:
        redis_conn = redis.StrictRedis(host=redis_endpoint, port=6379, db=0)
        redis_conn.set("foo", "boo")
        redis_conn.get("foo")
    except:
        log.debug("failed to connect to redis")
        return {
            'statusCode': 500
        }
    finally:
        del redis_conn
        return {
            'statusCode': 200
        }
Run Code Online (Sandbox Code Playgroud)

我已将其部署为无服务器的 HTTP 端点

#
# For full config options, check the docs:
#    docs.serverless.com
#

service: XXX

plugins:
  - serverless-aws-documentation
  - serverless-python-requirements
custom:
  pythonRequirements:
    dockerizePip: true



provider:
  name: aws
  stage: dev
  region: eu-central-1
  runtime: python3.6
  environment:
    # our cache
    REDIS: xx-xx-redis-001.xxx.euc1.cache.amazonaws.com



functions:
  hello:
    handler: hello/hello_world.say_hello
    events:
      - http:
          path: hello
          method: get
          # private: true # <--  Requires clients to add API keys values in the `x-api-key` header of their request
          # authorizer:   # <--  An AWS API Gateway custom authorizer function

  testRedis:
    handler: test_redis/test_redis.test_redis
    events:
      - http:
          path: test-redis
          method: get
Run Code Online (Sandbox Code Playgroud)

当我通过 API Gateway 触发端点时,lambda 在大约 7 秒后超时。正确读取环境变量,未显示错误消息。我想连接到 redis 存在问题,但是教程非常明确 - 不确定可能是什么问题。

问题可能需要设置 NAT,不确定如何使用无服务器完成此任务

feu*_*177 6

我也遇到了这个问题。对我来说,有一些问题需要解决

  • lambda 需要VPC 权限
  • ElastiCache 安全组需要来自 Lambda 安全组的入站规则,以允许在 Redis 端口上进行通信。我认为他们可能只是在同一个安全组中。
  • 真正的问题是:我打开了传输中的加密。这意味着我需要通过redis.RedisClient(... ssl=True). 在Redis的-PY页提到,ssl_cert_reqs需要被设置为None与ElastiCache使用,但似乎并没有在我的情况是真实的。但是我确实需要通过ssl=True

ssl=True需要设置是有道理的,但连接只是超时,所以我四处寻找权限/VPC/SG 设置的问题是什么。