3
votes

I have created a Redis Elasticache cluster in AWS and would like to read and write data to the cluster using Python script which will eventually become a Lambda function. I've read that the typical way to connect to the cluster is EC2. I have setup an EC2 instance and connected to it successfully using SSH and key pair.

My questions are:

  1. Do I need an EC2 instance or can I connect directly to the cluster using Python?
  2. If I need to connect via EC2, what is the best way to do it so I can read and write the data from the Redis cluster or are there any examples? At the moment I have to go to EC2 and then Redis in an SSH session. I was thinking I would have to run the same commands in Python but njot sure how I would execute a redis command through an EC2 connection in Python.

Thanks for any help

1
Did you get a solution to connect to Redis cluster? I have the same case, am able to connect it using SSH on EC2 instance configured in same VPC. But, want to connect to the cluster from Lambda function. While trying to connect, it is giving timeout error. If you were able to connect, please post the answer how you were able to connect it through Lambda. - Avani Khabiya

1 Answers

7
votes

You do not need an EC2 instance to connect to the Redis ElastiCache cluster.

Yes, you can connect to ElastiCache using Lambda. There is a not very well documented "gotcha" to make sure that your Lambda is running in the same VPC as the ElastiCache cluster AND make sure that you keep your Lambda warm; Lambdas running inside of VPC's can have significant cold start times. Also, don't forget to set your security groups to allow traffic from Lambda to the the cluster.

You can read more about connecting to ElastiCache from Lambda here. The tutorial connects to Memcached however the same process applies to Redis:

https://docs.aws.amazon.com/lambda/latest/dg/vpc-ec.html