【发布时间】:2018-07-16 17:55:15
【问题描述】:
我有一个相当简单的测试应用:
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
}
我已将其部署为无服务器的 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
当我通过 API Gateway 触发端点时,lambda 会在大约 7 秒后超时。 环境变量被正确读取,没有显示错误信息。 我想连接到 redis 有问题,但是教程非常明确 - 不确定问题可能是什么。
The problem might need the need to set up a NAT,不知道如何使用无服务器来完成这项任务
【问题讨论】:
-
您是否能够从 AWS 控制台执行 Lambda 并将其连接到 redis?也许我在这里的回答会有所帮助:stackoverflow.com/questions/44741141/…
-
你搞定了吗?我也有同样的问题。
-
不,不是。我把它放在一边,但我又回来了。你在哪里卡住了?
-
我也有同样的问题。 Celery 任务经常超时...
标签: python redis serverless-framework amazon-elasticache