Connect to ElastiCache cluster from AWS Lambda function

As of Feb 2016, AWS allows using lambda functions to connect to Elasticache. Refer to Access Resources within a VPC using AWS Lambda. Here is a link how it works - Tutorial: Configuring a Lambda Function to Access Amazon ElastiCache in an Amazon VPC


I have tried connecting lambda to memcached elasticache and it works fine. Redis should also be doable.

Couple of things to keep in mind:

  1. Lambda and Elasticache has to be in the same VPC.
  2. When lambda is run in VPC, it won't have access to internet (so access to public APIs won't work). NATGateway is required for this.

Setting up an HTTP Proxy or iptables wouldn't work for the following reasons:

Redis calls are not HTTP and will not be handled by HTTP proxies. iptables (or any port forwarding for that matter) will either won't accept a domain name as destination or is highly inefficient due to DNS resolution required every time.

The best and convenient method is to install twemproxy in an EC2 machine and route your requests through it. As a bonus, you suddenly have deployed a fantastic sharding strategy as well.