Getting Started With Redis on AWS the Easy Way

Like most of the services, Amazon MemoryDB for Redis is fully integrated with Amazon VPC and always launches your cluster in a VPC. It means that you cannot access it from outside your VPC. While initially exploring these services (MSK, Elasticache for Redis, etc.), I usually followed the documentation that involved setting up EC2, SSH-ing into the instance, installing/copying stuff (language, runtime, code, client, etc.), and then tried things out.

Most often, the first step is the hardest, and it's important for developers to have the least amount of friction as possible to "get going." I looked for simpler ways and found AWS Cloud9 to be super useful. It was quick, predictable, and had a bunch of useful tooling readily available.

In this blog, I will provide step-by-step instructions on how you can easily and quickly get started (and continue experimenting/building/developing) with Amazon MemoryDB using Cloud9.

You don't need to install anything on your local machine to work through this tutorial.

The only thing you need is an AWS account (of course!), so make sure you have one (even a free tier might work).

Setup Cloud9 and MemoryDB

If you're new to AWS in general (or MemoryDB/any other service), I would advise you to go through the setup manually using the AWS Console (as opposed to using CloudFormation or other tooling). This gives you an overview of the options available and will be helpful when you try to automate the same using AWS CLI, CDK, CloudFormation, etc.

Cloud9 Environment

This is quite simple: the documentation worked as expected.

Go to the AWS console > Cloud9:

Create environment

Just enter the name of your environment:

Name environment

You can safely choose the defaults on the second screen:

This is good enough for now.

On the last page, review your settings, click Create environment and you should be off to the races!

Creating AWS Cloud9 environment prompt

MemoryDB for Redis

Again, the documentation works as expected. there are a few config knobs, but I advise you to keep it simple:

Be patient, the cluster should be ready in a few minutes.

Cluster endpoint

Security Configuration

You need to add configuration to allow access from Cloud9 instance to your MemoryDB cluster.

First, copy the security group ID for your Cloud9 instance:

Copy the security group ID for your Cloud9 instance

Then, open the security group for your MemoryDB cluster:

Open the security group for your MemoryDB cluster

Add an Inbound security rule:

Add an Inbound security rule

The rule says to allow the instance associated with the source security group (Cloud9 in this case) to access the TCP port 6379 of instance associated with the target security group (MemoryDB in this case).

You're all set!

Navigate to Your Cloud9 IDE

Go to AWS console > Cloud9:

Open IDE

Your Cloud9 env should open up - you should see a terminal.

Connect to MemoryDB the Easy Way

The simplest way is to use redis-cli. You don't need to install it separately. Let's just use Docker since it's already pre-installed for us!

redis-cli is available in the redis container itself, so you can start it up and use it from there. Pull the Redis Docker image from DockerHub docker pull redis

Admin:~/environment $ docker pull redis

Using default tag: latest
latest: Pulling from library/redis
214ca5fb9032: Pull complete 
9eeabf2ad250: Pull complete 
b8eb79a9f3c4: Pull complete 
0ba9bf1b547e: Pull complete 
2d2e2b28e876: Pull complete 
3e45fcdfb831: Pull complete 
Digest: sha256:180582894be9a7d5f1201877744b912945a8f9a793a65cd66dc1af5ec3fff0fc
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest


Run the container:

Admin:~/environment $ docker run --rm -it redis /bin/bash
root@429f8fabaf09:/data# 


Now you are inside a terminal (in the container) of a terminal (Cloud9 IDE).

Copy the cluster endpoint of your MemoryDB cluster and set it as an environment variable:

Copy the cluster endpoint of your MemoryDB cluster and set it as an environment variable

Make sure you remove the port (:6379) from the cluster endpoint since redis-cli appends that automatically:

export MEMORYDB_CLUSTER_ENDPOINT=<memorydb cluster endpoint without the :6379 part)

redis-cli -c --user <memorydb username> --askpass -h $MEMORYDB_CLUSTER_ENDPOINT --tls --insecure


--askpass will prompt you for the password. Enter it.

Enter the password

Wohoo! You are now connected to your MemoryDB cluster from inside a Docker container in your Cloud9 instance.

Time for the customary hello world dance!

In the terminal:

SET hello world
SET foo bar


You should get an OK response from MemoryDB.

So far so good! You were able to use standard tooling (redis-cli) to connect with your freshly minted MemoryDB cluster. This is good for sanity/connectivity testing, but you can also do some "lightweight" development and run some programs to execute operations on MemoryDB. This is the next logical step, so let's do that. 

The example below shows a Go program, but you could use a language of your choice. After all, most language runtimes (like Java, Python, Node.js, Go, etc.) come pre-installed in the Cloud9 environment! 

Go example

Run a Program To Connect With MemoryDB

The code is on GitHub, so simply clone it and change it to the right folder:

git clone https://github.com/abhirockzz/memorydb-cloud9-quickstart
cd memorydb-cloud9-quickstart


Set the environment variables and run the program!

export MEMORYDB_CLUSTER_ENDPOINT=<memorydb cluster endpoint (with the port)>
export MEMORYDB_USERNAME=<memorydb username>
export MEMORYDB_PASSWORD=<memorydb password>

go run main.go


Here is the output when I ran it:

Admin:~/environment/memorydb-cloud9-quickstart (master) $ go run main.go 

go: downloading github.com/go-redis/redis/v8 v8.11.5
go: downloading github.com/gorilla/mux v1.8.0
go: downloading github.com/cespare/xxhash/v2 v2.1.2
go: downloading github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f
2022/05/12 04:53:46 connecting to cluster ****************(redacted)
2022/05/12 04:53:46 successfully connected to cluster
2022/05/12 04:53:46 started HTTP server....


This will start an HTTP server that exposes a few endpoints. Let's try them out.

Open a separate terminal in Cloud9 to run the commands below.

First, take a look at the cluster info:

curl -i http://localhost:8080/

HTTP/1.1 200 OK
Date: Thu, 12 May 2022 04:57:03 GMT
Content-Length: 354
Content-Type: text/plain; charset=utf-8

[{"Start":0,"End":16383,"Nodes":[{"ID":"3a0ef99406d4165fab450fde6c0a4eac3ee8f215","Addr":"****************.amazonaws.com:6379"},{"ID":"2b5a4663a9183f7921517c6f14195e9d26a6ca79","Addr":"****************.amazonaws.com:6379"}]}]


We got back info about the shards in our cluster along with individual nodes. The result will be different in your case.

Remember we had executed SET hello world with redis-cli before? Let's GET that value now:

# get the value for the key "hello"

Admin:~/environment $ curl -i localhost:8080/hello
HTTP/1.1 200 OK
Date: Thu, 12 May 2022 04:54:45 GMT
Content-Length: 32
Content-Type: text/plain; charset=utf-8

{"Key":"hello","Value":"world"}


Do the same for the key foo:

Admin:~/environment $ curl -i localhost:8080/foo
HTTP/1.1 200 OK
Date: Thu, 12 May 2022 04:55:44 GMT
Content-Length: 28
Content-Type: text/plain; charset=utf-8

{"Key":"foo","Value":"bar"}


It works as expected. What about a key that does not exist?

Admin:~/environment $ curl -i localhost:8080/notthere
HTTP/1.1 404 Not Found
Date: Thu, 12 May 2022 04:56:23 GMT
Content-Length: 0


HTTP 404: fair enough. Finally, you can set your own key value:

Admin:~/environment $ curl -i -X POST -d 'redis' localhost:8080/awsome
HTTP/1.1 200 OK
Date: Thu, 12 May 2022 04:59:25 GMT
Content-Length: 0

Admin:~/environment $ curl -i localhost:8080/awsome
HTTP/1.1 200 OK
Date: Thu, 12 May 2022 05:00:51 GMT
Content-Length: 33
Content-Type: text/plain; charset=utf-8

{"Key":"awsome","Value":"redis"}


Alright, everything works!

The Dockerfile for the sample app is also present in the Github repo in case you want to build a docker image and run that instead.

Clean Up

Once you're done, don't forget to:

That's all for this blog. I hope you were able to follow along with this setup of a MemoryDB cluster and a Cloud9 environment (along with a ready-to-use IDE and terminal!). After doing some initial connectivity testing using redis-cli in Docker, you also ran a test program to experiment a little more.

All this with just your browser!

I hope this was useful and you can re-use this for your particular setup, requirements, and programming language.

Happy coding!

 

 

 

 

Top