Serverless with AWS ElastiCache

AWS offers fully managed, in-memory service named ElastiCache. It supports two common engines named Redis and Memcached.

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

  1. Login to the AWS Console, and open https://console.aws.amazon.com/vpc/
  2. Choose Start VPC Wizard
  3. From navigation, list choose VPC with Public and Private subnets
  4. Keep the default options, and then choose to Create VPC

 2 – Creating the subnets

  1. From the navigation select Subnets
  2. Click on the option Create Subnets
  3. Select your VPC from the drop-down list and provide a name for the subnet.
  4. 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.

  1. Create the new security group and select your VPC from the drop-down list.
  2. Select the security group and an edit menu will be available at the bottom of the screen.
  3. 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.
  4. 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

  1. Choose to Create Endpoint option available on the top left corner of the screen.
  2. Select the AWS service from the list and your VPC.
  3. 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/

  1. From navigation, choose Subnet Groups
  2. Select Create Cache Subnet Group
  3. From the drop-down option select your VPC, provide a name for the Cache Subnet Group.
  4. 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.

  1. Open the ElastiCache console at https://console.aws.amazon.com/elasticache/
  2. Choose Redis from the navigation.
  3. Mention the name of the cluster.
  4. In the description field, mention the description for the cluster.
  5. To encrypt your data, choose Yes for the Encryption check box.
  6. For the Port, mention the port number on which you want to expose the cluster, by default it is 6379.
  7. For Number of Replicas, choose number of read plicas for the cluster.
  8. In advance settings, choose the above created subnet groups for the cluster.
  9. 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)

Step

 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

https://serverless.com/blog/serverless-python-packaging/

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 bootstrap@causecode.com

Leave a Reply

Your email address will not be published. Required fields are marked *

STAY UPDATED!

Do you want to get articles like these in your inbox?

Email *

Interested groups *
Healthtech
Business
Technical articles

Archives