2

I have a rather simple test app:

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
        }

which I have deployed as a HTTP endpoint with serverless

#
# 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

When I trigger the endpoint via API Gateway, the lambda just times out after about 7 seconds. The environmental variable is read properly, no error message displayed. I suppose there's a problem connecting to the redis, but the tutorial are quite explicit - not sure what the problem could be.

The problem might need the need to set up a NAT, not sure how to accomplish this task with serverless

NoIdeaHowToFixThis
  • 4,484
  • 2
  • 34
  • 69

2 Answers2

7

I ran into this issue as well. For me, there were a few problems that had to be ironed out

  • The lambda needs VPC permissions.
  • The ElastiCache security group needs an inbound rule from the Lambda security group that allows communication on the Redis port. I thought they could just be in the same security group.
  • And the real kicker: I had turned on encryption in-transit. This meant that I needed to pass redis.RedisClient(... ssl=True). The redis-py page mentions that ssl_cert_reqs needs to be set to None for use with ElastiCache, but that didn't seem to be true in my case. I did however need to pass ssl=True.

It makes sense that ssl=True needed to be set but the connection was just timing out so I went round and round trying to figure out what the problem with the permissions/VPC/SG setup was.

feus4177
  • 1,193
  • 1
  • 11
  • 15
0

Try having the lambda in the same VPC and security group as your elastic cluster