AWS ElastiCache (memcached) with custom VPC

In this tutorial we’ll learn how to create an Elasticache Memcached Cluster using a custom VPC.

Requirements

A custom VPC is needed to follow this tutorial. You can create a VPC (10.0.0.0/16) with one subnet (10.0.1.0/24).

Creating the cache subnet

You’ll need to create a cache subnet in your VPC. Go to the ElastiCache console and click on the Cache Subnet Groups.

cache_subnet1

Click on the Create Cache Subnet Group. Add a name and description for this subnet group and select the VPC ID of your custom VPC.

cache_subnet2

Click on the Add  button below to the Subnet ID dropdown box.

cluster

Launching a cluster

Click on the Cache Cluster link from the side menu. You’ll need to select the engine you wish to use (Redis or Memcached). We’ll be using Memcached.

create1

Add a Cluster Name, select the free tier Node Type (cache.t2.micro) and set the Number of Nodes to 1.

create2

Select the previously created Cache Subnet Group and choose the VPC Security Group you’d like to use. We’ll need to add a custom TCP/IP rule to the security group.

create3

The cluster will be created in a couple of minutes.

cluster

You can see the detailed node information by clicking on “1 node”.

node

By clicking on the node you’ll be able to see metrics on how this particular node is running.

Security Group

To be able to connect from your EC2 instance to the ElastiCache cluster you’ll need to add a new inbound rule to the Security Group.

security_group

Create a new custom TCP rule. Set the port number to 11211 (Memcached default port number) and set the source to 0.0.0.0/0 (any destination).

Testing connectivity

Testing connectivity between your EC2 instance and the ElastiCache cluster is easy. SSH into your EC2 instance (an instance in your custom VPC).

Install telnet:

yum install telnet

Run the following command:

telnet eticket-cache.4xznmr.0001.euw1.cache.amazonaws.com 11211

Replace the endpoint with your node’s endpoint.

If everything works fine and the EC2 can establish a telnet connection to port 11211 of your ElastiCache node you should see something similar to this:

[root@ip-10-0-1-39 ec2-user]# telnet eticket-cache.4xznmr.0001.euw1.cache.amazonaws.com 11211
Trying 10.0.1.20...
Connected to eticket-cache.4xznmr.0001.euw1.cache.amazonaws.com.
Escape character is '^]'.

Conclusion

Creating an ElastiCache cluster is easy and fast using AWS. In my next tutorial I’ll show you how to connect to this cluster using GO. I’ll show you how ElastiCache can speed up your web app with some easy to follow labs.