How to access Elasticache and Internet in AWS Lambda?

Question:

I just wrote a python script that connect to the AWS ElastiCache. And it just check the connection.

from redis import Redis

try:
    redis = Redis(host='xxx.cache.amazonaws.com',
                  port=6379,
                  db=0)

    if not redis.ping():
        raise("REDIS can't be initialized")

    return True
except Exception as e:
    print(str(e))
    return False

When the following code is ran under my EC2 it has no problem and the response is reeally fast. Then I wrapped this using AWS Lambda and invoke the function locally. The returned result is still normal. But when the AWS Lambda is deployed on cloud the function got stuck on the redis.ping() until the function went timeout after 30 seconds.

I am not sure why the behavior is totally different.

Thanks in advance.

Asked By: Winston

||

Answers: