ElastiCache and DynamoDB

274 Views Asked by At

not able to get my Redis cluster and DynamoDB working together in the same lambda, if I comment any one other work fine, but together they will give timeout, and Cloudwatch simply does not log anything,

ex dyamodb.get_item(), redis.get() keep on running if I use both until lambda timed out the snippet below

redisclient = redis.Redis(host="redisclxxxxxxxxx.use.cache.amazonaws.com", port=6379, db=0,decode_responses=True)

redisclient.get('name')

table = db.Table('xxxxxxxxxx')
table.get_item(Key={'phoneno': phone, })

            def login(event, context):
                password = json.loads(event['body'])['password']
                phone = json.loads(event['body'])['phone']
                table = db.Table('xxxxxx')
                resp = table.get_item(Key={'phoneno': phone, })
                if 'Item' in resp:
                    if resp['Item']['password'] == password:
                        payload = {'phone': phone, 'password': password}
                        response = {"statusCode": 200, "body": json.dumps({'token': getToken(payload)})}
                    else:
                        response = {"statusCode": 200, "body": json.dumps({'error': 'password not match'})}
                else:
                    response = {"statusCode": 200, "body": json.dumps({'message': 'user_not_found sign up now'})}
                return response


            def getToken(payload):
                payload['exp'] = datetime.now() + timedelta(minutes=5)
                redisclient = redis.Redis(host="redisxxxxxxxxxx.xxxxxxx.cache.amazonaws.com", port=6379, db=0,
                                          decode_responses=True)
                redisclient.get('name')
                
                token = jwt.encode(payload, "xxxxxxxxxxxxxx", algorithm="HS256")
                redisclient.set(token,token)
                print(redisclient.get(token)+'redis')
                return token
2

There are 2 best solutions below

3
On

Want to raise a point that's not discussed in the comments The timeout maybe due to the lambda function timeout configuration. Increase the lambda timeout configuration.

enter image description here

0
On

Lambda and DynamoDB are executed in the AWS Public Cloud. Both are services executed in a internet facing environment. The Elastic Cache Cluster, otherwise, is user managed service that runs on your own VPC.