DEV Community

Abhishek Gupta for AWS

Posted on • Originally published at abhishek1987.Medium

Getting started with Redis on AWS - the easy way!

This quick-start uses AWS Cloud9 IDE to help you get up and running with MemoryDB for Redis, quickly

When I was initially exploring some of the AWS services (such as MemoryDB, MSK, Elasticache for Redis etc.), I usually followed the documentation that involved setting up EC2, SSH-ing into the instance, install/copy stuff (language, runtime, code, client etc.) and then try 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".

As I searched for simpler/faster ways, I discovered AWS Cloud9 and it turned out 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 for Redis using Cloud9.

Amazon MemoryDB for Redis is a durable, in-memory database service that is compatible with Redis, thus empowering you to build applications using the same flexible and friendly Redis data structures, APIs, and commands that they already use today. It is fully integrated with Amazon VPC and the cluster in always launched in a VPC.

You don't need to install anything 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:

Image description

Just enter the name your environment:

Image description

You can safely choose the defaults on the second screen:

  • You will have a t2.micro node type (1 GiB RAM + 1 vCPU) with Amazon Linux 2 which will be auto-hibernated after 30 mins (if not used)
  • The instance will be placed in the default VPC (any subnet in any AZ). - A security group will also be created

This is good enough for now.

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

Image description

MemoryDB for Redis

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

  • Single node cluster - select db.t4g.small node type (it's sufficient for now)
  • Place the default vpc - you will be choosing this (along with the subnets) while creating a subnet group (in MemoryDB)
  • Make sure to setup the ACL and credentials (username and password to connect to MemoryDB) as well

Be patient, the cluster should be ready in a few mins :)

Image description

Security configuration

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

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

Image description

Then, open the security group for your MemoryDB cluster:

Image description

Add an Inbound security rule:

Image description

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

You're all set!

Navigate to your Cloud9 IDE:

Go to AWS console > Cloud9:

Image description

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
Enter fullscreen mode Exit fullscreen mode

Run the container:

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

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

Image description

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
Enter fullscreen mode Exit fullscreen mode

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

Image description

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
Enter fullscreen mode Exit fullscreen mode

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 so 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 Cloud9 environment! Check this out https://docs.aws.amazon.com/cloud9/latest/user-guide/language-support.html

Image description

Run a program to connect with MemoryDB

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

git clone https://github.com/abhirockzz/memorydb-cloud9-quickstart
cd memorydb-cloud9-quickstart
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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....
Enter fullscreen mode Exit fullscreen mode

This will start a 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"}]}]
Enter fullscreen mode Exit fullscreen mode

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"}
Enter fullscreen mode Exit fullscreen mode

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"}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"}
Enter fullscreen mode Exit fullscreen mode

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:

  • Delete the MemoryDB cluster, and
  • the Cloud9 environment

That's all for this blog. I hope you were able to follow along - setup 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!

Discussion (0)