Getting started with Redis on AWS - the easy way!
Tags: beginners, cloud, programming, tutorial
Published: 2022-05-12 06:57:26

*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](https://aws.amazon.com/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](https://docs.aws.amazon.com/memorydb/latest/devguide/what-is-memorydb-for-redis.html) 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)](https://aws.amazon.com/free/)

## 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](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) 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](https://docs.aws.amazon.com/cloud9/latest/user-guide/tutorial.html) worked as expected.

Go to the AWS console > Cloud9:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8l9ie8l1g2hlj67qx16j.png)

Just enter the name your environment:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6t5m15jcqz2lxhh6uchc.png)

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](https://docs.aws.amazon.com/vpc/latest/userguide/default-vpc.html) (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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ihicnciwa8lez817hscy.png)

**MemoryDB for Redis**

Again, [the documentation](https://docs.aws.amazon.com/memorydb/latest/devguide/getting-started.createcluster.html) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6cn230547evkygz6fmk7.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/830hscmfk1806ma8yai8.png)

Then, open the security group for your MemoryDB cluster:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mbw5xo26a3trlksg4b9x.png)

Add an *Inbound* security rule:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0qad9tszanul3ofi3lzm.png)

> 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/chic3ik7sz0o1ny38zhl.png)

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](https://redis.io/docs/manual/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](https://hub.docker.com/_/redis) - `docker pull redis`

```bash
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:

```bash
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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zff2ssicbfazhzod11g8.png)

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

```bash
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.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5mf42md466o9eaqzeh09.png)

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:

```bash
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 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wnta7smqqfdrmk52madi.png)

### Run a program to connect with MemoryDB

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

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

Set the environment variables and run the program!

```bash
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:

```bash
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 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:

```bash
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:

```bash
# 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`:

```bash
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"}
```

Works as expected - what about a key that does not exist?

```bash
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:

```bash
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:

- 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!