Using Redis with Amazon AWS

If you’re like me and expected to simply connect to an Amazon AWS Redis instance with a local “Terminal” client, you’re wrong. It’s not so simple.

So it’s the holidays, and I’m on a long staycation with the kids while my wife attends a dance convention in Reno. As such, I’m unexpectedly burnt out on Fortnite Battle Royale, and in need of something to do with my time. So, I’ve decided to work on a prototype that has been on my mind for over a year now. Of course this prototype is super basic, but because it uses Redis for data storage, I decided to write this tutorial to share my experiences interacting with Redis on AWS.

Naturally, I first finished the prototype on my Macbook with a locally installed Redis instance, a simple NodeJS server, and a very basic web client. Now I’m ready to publish my prototype online so it can be tested by family members on different devices. But unlike other projects I’ve worked on in the past, I was confused on how to access my AWS Redis instance from my Macbook Terminal (to perform sanity checks of course). Simply put: If you’re like me and expected to simply connect to you Amazon AWS Redis instance with a local “Terminal” client, you’re wrong. It’s not so simple.

Instead, you need to remotely connect to an AWS EC2 instance through your terminal, then connect to your Redis instance from there. This means, you’ll need to do the following:

  1. Create an EC2 instance in AWS
  2. Create a Redis instance
  3. Connect to the EC2 instance remotely
  4. Install the Redis command line tools on the EC2 instance
  5. Connect to the Redis instance from the EC2 instance
  6. Interact with Redis similar to how you would from your local instance.

I know the above now, because I spent about 1 hour googling for an answer, reading stack overflow discussions, and altogether avoiding the Amazon documentation. When I finally bothered to read the Amazon documentation, it became clear: https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/GettingStarted.AuthorizeAccess.html. On this page, you’ll see the following statement: All ElastiCache clusters are designed to be accessed from an Amazon EC2 instance. (Not sure why I avoided the AWS docs initially.)

Step 1: Create an EC2 instance in AWS

I’ve already covered this step in a prior detailed blog entry, so I wont go over it again here. Please the original entry at this link: Hosting Your Own WordPress Blog – Step 2 – Create a Cloud Server Using Amazon AWS. While this was part of a series about hosting your own blog via AWS, this specific EC2 setup step is required here as well. Once you have your EC2 instance set up, move onto the next step below.

Step 2: Create a Redis instance in AWS

From the services list drop down, search for Redis and select the “ElastiCache” option.

Locating the ElastiCache (Redis) AWS service

Click “Get Started Now” in the middle of the page (assuming you have never started a Redis instance on AWS before), or click the Redis link in the left menu. Either method will result in the page below with the following sections. In the first section pictured, select Redis.

Choosing a Redis ElastiCache cluster engine.

In the next section, apply whichever settings work for you. I’ve included a screenshot of my settings. Since this only a prototype, I don’t need a lot of functionality. (Sorry in advance for the small screenshot. I wanted to fit the entire page of settings in one go)

Configuring a new Redis AWS instance

Notice that I’ve chosen Node type : cache.t2.micro (0.5 GiB). This is the smallest instance available. You may want to choose something larger. A few other settings of note, because they’re not needed for prototypes:

  • Node type: cache.t2.micro (0.5 GiB)
  • Number of replicas: 0
  • Enable automatic backups: unchecked

Once complete, click the “Create” at the bottom of the page. This will forward you to the Redis dashboard and indicate that your instance is being created.

Starting up a Redis AWS instance.

Step 3: Connect to the EC2 instance remotely

If you followed step one above to completion, you would have created an EC2 instance and connected to it via your Terminal (or other shell) application. If you’ve only created the instance but not connected yet, go ahead and do so now. Instructions for this are found in the earlier referenced post: Hosting Your Own WordPress Blog – Step 2 – Create a Cloud Server Using Amazon AWS. Once you have connected to your EC2 instance, move onto the next step.

Step 4: Install the Redis command line tools on the EC2 instance

Once connected to your EC2 instance, we can install the Redis command line tools. The command for this is: sudo apt-get install redis-tools

Installing the redis-cli command line tools.

You can test your installation by running the following command: redis-cli. You’ll know your successful when you see “Connection refused”, show below. This means the command line tools tried connecting to a local Redis server by default (which doesn’t exist), and failed. So while the connection may have failed, we’ve proven that the tools were installed! Good failure 🙂 Be sure to type exit to return back to your EC2 command prompt.

redis-cli tools, installation sanity check.

Step 5: Connect to the Redis instance from the EC2 instance

Now that we have tools installed, we need to connect to our Redis instance created in step 2. Go back to your ElastiCache Dashboard (pictured below), and see the “Endpoint” and “Port” value:

  • Endpoint: tutorial-prototype.4nigc2.0001.usw2.cache.amazonaws.com
  • Port: 6379

With these two values you can type the following command into your terminal:

  • redis-cli -c -h <endpoint> -p <port>
  • For me this is: redis-cli -c -h tutorial-prototype.4nigc2.0001.usw2.cache.amazonaws.com -p 6379
Redis endpoint and port information

Make sure you enter this command from your EC2 instance command prompt, and not the failed redis-cli command prompt that we entered into earlier. For reference, the redis-cli command prompt looks like this: [not connected>, while the EC2 command prompt looks like this: ubuntu@some-ip:~$ . Once connected, you’ll see output similar to mine in which the Redis server’s endpoint and port are listed between the [ and > brackets

Connected to the Redis server via Redis CLI tool

Once connected, issue a “ping” command to verify that you get a PONG response from your Redis instance. Continue playing ping pong with your Redis server if you’d like. Once complete, move onto some basic interaction below.

Now that we’re connected to a Redis instance on AWS, we can begin issuing commands! While “ping” is certainly a valid command, maybe try a couple easy ones like get & set:

STILL CAN’T CONNECT? Modify your Redis instance security group

Initially, I couldn’t connect to my Redis instance either. I thought perhaps it was a security group issue, tinkered with my group settings, engaged in a series of google searches, and eventually connected via my EC2 instance. I’m not sure what the default security group settings were, and am unable to reproduce a connection issue. But if you’ve followed this blog post so far and are still having issues connecting, it’s very likely to be security group related.

You can confirm your security group name by locating it in your Redis instance control panel. You’ll see the security group listed in the right column. Mine is sg-5660d932 (VPC) (active).

With this security group name located, you’ll be able to modify your own settings to enable access. My settings for this security group are very liberal, as shown below. I don’t mind, because this is a very short lived prototype, and not a production deployment of a mission critical application. If your use-case is similar to mine, feel free to copy these settings. But PLEASE don’t use these for a mission critical stuff. Instead, consult a security expert for the right configurations.

Inbound traffic rules (very liberal and insecure)
Outbound traffic rules (also very liberal and insecure)

If you have no idea how to locate and edit your security groups as seen above, please refer to the blog entry and section linked below. This goes into this part of AWS in a decent level of detail.

That’s it, we’re done. Hopefully this was helpful, and happy Redis-ing in the AWS cloud!

Posted by: Ben

Leave a Reply

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