Serverless with AWS ElastiCache
However, the problem is that ElastiCache clusters can only be accessed by the services that are present in the same Virtual Private Cloud – VPC as ElastiCache.
So here is a quick example explaining how to set up a serverless function and ElastiCache cluster under a VPC.
1 – Setting up a VPC
- Login to the AWS Console, and open https://console.aws.amazon.com/vpc/
- Choose Start VPC Wizard
- From navigation, list choose VPC with Public and Private subnets
- Keep the default options, and then choose to Create VPC
2 – Creating the subnets
- From the navigation select Subnets
- Click on the option Create Subnets
- Select your VPC from the drop-down list and provide a name for the subnet.
- Try to create at least one subnet per availability zone.
3 – Creating a Security Group
Note- There will be already a default security group for the VPC we have created. We can edit the inbound and outbound rules or can create the new security group.
- Create the new security group and select your VPC from the drop-down list.
- Select the security group and an edit menu will be available at the bottom of the screen.
- Edit the inbound rules and add the rules for HTTP, HTTPS and SSH if you are going to access it from a Linux machine. Kindly study the other rules too.
- Perform the above operation for the Outbound rules as well.
4 – Creating the endpoint for other AWS services.
From the navigation, select the option Endpoints
- Choose to Create Endpoint option available on the top left corner of the screen.
- Select the AWS service from the list and your VPC.
- Click on the Create endpoint button.
Note: Please check that the entry for the above-created endpoint is present in the routing table of the VPC.
Go the Route Tables options from the navigation and select the entry having your VPC id.
From the menu displayed at the bottom of the screen, select Routes and confirm your Endpoint entry.
This endpoint entry is for the AWS services to know where to redirect the request as your lambda will be behind the private cloud.
5 – Creating a Cache subnet group
Open the ElastiCache console at https://console.aws.amazon.com/elasticache/
- From navigation, choose Subnet Groups
- Select Create Cache Subnet Group
- From the drop-down option select your VPC, provide a name for the Cache Subnet Group.
- As you select the VPC, you will get all the subnets related to that VPC as per the Zones, add all the subnets from which the ElastiCache cluster will be accessed.
6 – Create ElastiCache cluster
Note: Following steps are for creating the Redis cluster.
Please read the documentation for creating the Memcached cluster.
- Open the ElastiCache console at https://console.aws.amazon.com/elasticache/
- Choose Redis from the navigation.
- Mention the name of the cluster.
- In the description field, mention the description for the cluster.
- To encrypt your data, choose Yes for the Encryption check box.
- For the Port, mention the port number on which you want to expose the cluster, by default it is 6379.
- For Number of Replicas, choose number of read plicas for the cluster.
- In advance settings, choose the above created subnet groups for the cluster.
- Choose the security group created above for the Security Groups
Note: It will take 3-4 mins for the cluster to set up.
You can view the cluster created in the ElastiCache’s Redis option from the navigation.
7 – Configuring the serverless.yml
To launch your serverless function under a VPC, add the following to your serverless.yml file
provider: name: aws runtime: python3.6 timeout: 300 vpc: securityGroupIds: - <id of the security group selected while creating the cluster > subnetIds: - <id of the subnets that are included in the Cache subnet group> - <subnet id 2> - <subnet id 3>
Note: The above configuration will be applied at the service level and for all the functions declared under the serverless.yml file, however, you can also overwrite these service level config at the function level.
functions: my-function: handler: handler.my-function vpc: securityGroupIds: - <id of the security group selected while creating the cluster > - <security Group Id 2> subnetIds: - <id of the subnets included in the cache subnet group> - <subnet id 2>
8 – Example for accessing the cluster using Python
For accessing the above created Redis cluster, there is a python package available https://pypi.python.org/pypi/redis
Note: From the ElastiCache console, click on the cluster you have created.
You will get the primary endpoint link which must be passed to `host` parameter of redis.StrictRedis() (Remove the port from the endpoint)
pip install redis
import redis cluster = redis.StrictRedis(host='primary-endpoint-without-port', port=6379, db=0) cluster.set('foo', 'bar') cluster.get('foo')
For handling your Python packaging in Lambda, please visit
I hope you find this blog useful. Thank you!
About CauseCode: We are a technology company specializing in Healthtech related Web and Mobile application development. We collaborate with passionate companies looking to change health and wellness tech for good. If you are a startup, enterprise or generally interested in digital health, we would love to hear from you! Let's connect at email@example.com