node.jsamazon-web-servicesaws-lambdaamazon-elasticacheserverless-framework

Testing Elasticache and Serverless AWS Lambda locally


I'm using serverless to create a lambda function using the nodejs template.

  1. To connect to my Elasticache Redis I setup a VPC and added the Security Group and Subnet Ids, as well as the region to my lambda serverless.yml.
  2. I deployed it and confirmed I could set and get a key from Redis.

When I try to serverless invoke local -f functionName, it always times out, so I have to deploy it to test it.

Is it possible to test this out locally with serverless?


Solution

  • Elasticache is not directly accessible from outside AWS environment by default. According to their documentation, the service is designed to be accessed exclusively from within AWS. In your case, serverless invoke local times out because the connection itself cannot be established and the lambda function times out. So you can't run invoke locally to test this connection the way you are trying to do.

    To connect to Elasticache redis from your local machine, you can use a NAT instance in your public subnet and setup the security groups to open up the correct ports and enable IP forwarding to allow connection to your redis cache cluster. The steps are given here.

    However, I would just install redis locally and use an environment variable to change the connection string to connect to local redis on local machine and the actual Elasticache cluster when running on lambda.